Add activities management
This commit is contained in:
parent
9151a88ce5
commit
dcbd5015d2
3 changed files with 91 additions and 1 deletions
|
@ -15,7 +15,7 @@ ResponseGenerator.Log = require(`./response/log.JS`);
|
|||
class ExerciseTrackerAPI {
|
||||
#paths = {
|
||||
"users": ["users"],
|
||||
"exercises": ["users/:_id/exercises"],
|
||||
"exercises": ["users/:ID/exercises"],
|
||||
"logs": ["users/:_id/logs"]
|
||||
}
|
||||
|
||||
|
@ -31,11 +31,44 @@ class ExerciseTrackerAPI {
|
|||
this.management[DATABASE] = new DBManagement[DATABASE]();
|
||||
});
|
||||
this.manageUsers();
|
||||
this.manageActivities();
|
||||
};
|
||||
|
||||
init_management();
|
||||
}
|
||||
|
||||
/*
|
||||
Manage the activities.
|
||||
*/
|
||||
manageActivities () {
|
||||
/*
|
||||
Create the new activities.
|
||||
*/
|
||||
const activity_creation = async (REQUEST, RESPONSE) => {
|
||||
try {
|
||||
if (((REQUEST.body) ? Object.keys(REQUEST.body).length : false) ? Object.keys(REQUEST.params).includes(`ID`) : false) {
|
||||
let CORRESPONDING_USERS = await this.management[`Users`].search({"ID": REQUEST.params[`ID`]});
|
||||
if (!CORRESPONDING_USERS.length || !CORRESPONDING_USERS) {
|
||||
throw new CustomErrors.Data.Incorrect(REQUEST.body);
|
||||
};
|
||||
|
||||
let RESULT = await this.management[`Activities`].create(Object.assign({}, REQUEST.body, {"username": CORRESPONDING_USERS[0].name}));
|
||||
let FORMATTED = await (new ResponseGenerator.Activities(Object.assign({},RESULT, {"ID": REQUEST.params[`ID`]}))).format();
|
||||
RESPONSE.send(FORMATTED);
|
||||
} else {
|
||||
throw new CustomErrors.Data.Missing(REQUEST.body);
|
||||
}
|
||||
} catch(ERR) {
|
||||
Messaging.exception(RESPONSE, ERR);
|
||||
}
|
||||
}
|
||||
|
||||
this.#paths[`exercises`].forEach((PATH) => {
|
||||
this[`instance`].post(`/api/${PATH}`, activity_creation)
|
||||
console.log(`Activity creation is ready on ${PATH}.`);
|
||||
});
|
||||
}
|
||||
|
||||
/*
|
||||
Manage the users.
|
||||
*/
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue