-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
200 lines (181 loc) · 6.96 KB
/
api.js
File metadata and controls
200 lines (181 loc) · 6.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
const fs = require("fs");
class Api {
/** constructeur */
constructor() {
try {
const data = fs.readFileSync("./bdd.json", 'utf-8') // On doit d'abord lire enregistrer la bdd avant de pouvoir interragir avec.
this.bdd = JSON.parse(data) // on la transforme en objet js
} catch (e) {
console.log("erreur d'initialisation, contacter admin.")
}
}
/**
* Fonction qui retourne un tableau des élèments regroupé a partir d'une partie du corps.
* @param {string} pPartie - Partie du corps
* @return [{JSON}] - Tableau des exercices pour cette partie du corps
*/
getParPartieCorps(pPartie) {
try {
let tab = []
const parm = pPartie.normalize('NFD').replace(/[\u0300-\u036f]/g, "").toLowerCase()
for (let i = 0; i < this.bdd.length; i++) {
const str = this.bdd[i].partie.normalize('NFD').replace(/[\u0300-\u036f]/g, "").toLowerCase()
if (str === parm) {
// forcement un string car une seul partie par exo
tab.push(this.bdd[i])
}
}
return tab
} catch (e) {
return []
}
}
/**
* Fonction qui retourne un tableau des élèments ciblant le muscle donné.
* @param {string} pCible - Muscle ciblé
* @return [{JSON}] - Tableau des exercices qui cible ce muscle
*/
getParCible(pCible) {
try {
let tab = []
const parm = pCible.normalize('NFD').replace(/[\u0300-\u036f]/g, "").toLowerCase()
for (let i = 0; i < this.bdd.length; i++) {
// Si cible === string, il n'y a qu'une seul cible
if (typeof this.bdd[i].cible !== 'string') {
for (let index in this.bdd[i].cible) {
// on recupere l'index de la valeur dans l'object de type { 0: 'deltoïdes', 1: 'biceps'}
const str = this.bdd[i].cible[index].normalize('NFD').replace(/[\u0300-\u036f]/g, "").toLowerCase()
if (str === parm) {
tab.push(this.bdd[i])
break
}
}
} else {
const str = this.bdd[i].cible.normalize('NFD').replace(/[\u0300-\u036f]/g, "").toLowerCase()
if (str === parm) {
tab.push(this.bdd[i])
}
}
}
return tab
} catch (e) {
return []
}
}
/**
* Fonction qui retourne un tableau des élèments possédant l'equipement donnée.
* @param {string} pEquipement - Equipement
* @return [{JSON}] - Tableau des exercices avec cette equipement
*/
getParEquipement(pEquipement) {
let tab = []
const paramNormalizer = pEquipement.normalize('NFD').replace(/[\u0300-\u036f]/g, "").toLowerCase()
for (let i = 0; i < this.bdd.length; i++) {
// Si equipement === string, il n'y a qu'un seul equipement
if (typeof this.bdd[i].equipement !== 'string') {
for (let index in this.bdd[i].equipement) {
// on recupere l'index de la valeur dans l'object de type { 0: 'barre', 1: 'haltere'}
const str = this.bdd[i].equipement[index].normalize('NFD').replace(/[\u0300-\u036f]/g, "").toLowerCase()
if (str === paramNormalizer) {
tab.push(this.bdd[i])
break
}
}
} else {
const str = this.bdd[i].equipement.normalize('NFD').replace(/[\u0300-\u036f]/g, "").toLowerCase()
if (str === paramNormalizer) {
tab.push(this.bdd[i])
}
}
}
return tab
}
/**
* Fonction qui retourne la totalité des exercices sous forme de tableau d'element JSON.
* @return [{JSON}] - La base de donnée
*/
getTous() {
return this.bdd
}
/**
* Fonction qui retourne un tableau des élèments correspondant à l'id de l'exercice.
* @param {string} id - id de l'exercice
* @return [{JSON}] - Tableau des exercices avec le même id
*/
getParId(id) {
try {
if (isNaN(parseInt(id))) {
return []
}
for (let i = 0; i < this.bdd.length; i++) {
if (parseInt(this.bdd[i].id) === parseInt(id)) {
return [this.bdd[i]]
}
}
return []
} catch (e) {
return []
}
}
/**
* Fonction qui retourne un tableau des élèments correspondant au nom de l'exercice.
* @param {string} pNom - Nom de l'exercice
* @return [{JSON}] - Tableau des exercices avec le même nom
*/
getParNom(pNom) {
try {
const parm = pNom.normalize('NFD').replace(/[\u0300-\u036f]/g, "").toLowerCase()
for (let i = 0; i < this.bdd.length; i++) {
const str = this.bdd[i].nom.normalize('NFD').replace(/[\u0300-\u036f]/g, "").toLowerCase()
if (str === parm) {
return [this.bdd[i]]
}
}
return []
} catch (e) {
return []
}
}
getTousEquipement() {
var tabFinal = []
for (let i = 0; i < this.bdd.length; i++) {
const str = this.bdd[i].equipement
if (Array.isArray(str)) {
for (let j = 0; j < str.length; j++) {
this.checkSiDataEstDoublon(str[j], tabFinal)
}
} else {
this.checkSiDataEstDoublon(str, tabFinal)
}
}
return tabFinal
}
getTousPartieDuCorps() {
var tabFinal = []
for (let i = 0; i < this.bdd.length; i++) {
this.checkSiDataEstDoublon(this.bdd[i].partie, tabFinal)
}
return tabFinal
}
getTousMuscles() {
var tabFinal = []
for (let i = 0; i < this.bdd.length; i++) {
const cibleTableauOuVariable = this.bdd[i].cible
if (Array.isArray(cibleTableauOuVariable)) {
cibleTableauOuVariable.forEach(element => {
this.checkSiDataEstDoublon(element, tabFinal)
})
} else {
this.checkSiDataEstDoublon(cibleTableauOuVariable, tabFinal)
}
}
return tabFinal
}
checkSiDataEstDoublon(data, tableau) {
let donnee = data.normalize('NFD').replace(/[\u0300-\u036f]/g, "").toLowerCase()
if (!tableau.includes(donnee)) {
tableau.push(donnee)
}
}
}
module.exports = Api