-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexerciseInProgress.js
77 lines (70 loc) · 3.16 KB
/
exerciseInProgress.js
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
var functions = require("./util.js");
const service = "exerciseInProgress";
class exerciseInProgress {
constructor(store) {
this.store = store;
this.service = service;
this.exerciseInProgress = this.store.getTable("ExerciseInProgress");
}
async manageGet(queryString) { }
async managePost(body) {
let idUser = this.store.getIdUser(body.idSession);
if (body.method == "insertExerciseInProgress") {
let ok = true;
let result = [];
let add = (d) => {
if (d.idExercise != undefined && this.store.searchKey("Exercise", "idExercise", d.idExercise) != undefined &&
functions.searchObjectInArray(this.exerciseInProgress, d.idExercise, "idExercise") == undefined) {
result.push({ idUser: idUser, idExercise: d.idExercise, progression: 0, numStep: 1, lastEdit: new Date() });
} else {
ok = false;
}
};
if (Array.isArray(body.data)) {
body.data.forEach(add);
} else {
add(body.data)
}
if (!ok) {
return { code: 400, response: '{message: "bad request. param was wrong"}' };
}
this.exerciseInProgress = this.exerciseInProgress.concat(result);
this.store.saveData("ExerciseInProgress", this.exerciseInProgress);
return { code: 200, response: '{message: "ok"}' };
} else if (body.method == "updateExerciseInProgress") {
let ok = true;
let update = (e) => {
let i = functions.getNumItemInArray(this.exerciseInProgress, ["idUser", "idExercise"], [idUser, e.idExercise]);
if (e.progression != undefined) {
this.exerciseInProgress[i].progression = parseInt(e.progression);
}
if (e.numStep != undefined) {
this.exerciseInProgress[i].numStep = parseInt(e.numStep);
}
this.exerciseInProgress[i].lastEdit = new Date();
};
if (Array.isArray(body.data)) {
body.data.forEach(update);
} else {
update(body.data)
}
if (!ok) {
return { code: 400, response: '{message: "bad request. param was wrong"}' };
}
this.store.saveData("ExerciseInProgress", this.exerciseInProgress);
return { code: 200, response: '{message: "ok"}' };
} else if (body.method == "deleteExerciseInProgress") {
let del = (e) => {
this.exerciseInProgress = this.exerciseInProgress.filter(a => a.idUser != idUser || a.idExercise == e.idExercise);
};
if (Array.isArray(body.data)) {
body.data.forEach(del);
} else {
del(body.data)
}
this.store.saveData("ExerciseInProgress", this.exerciseInProgress);
return { code: 200, response: '{message: "ok"}' };
}
}
}
module.exports = exerciseInProgress