User search and creation
This commit is contained in:
parent
5fa9e6efd6
commit
0458e11267
7 changed files with 109 additions and 42 deletions
|
@ -1,9 +1,88 @@
|
|||
const bodyParser = require(`body-parser`);
|
||||
|
||||
const CustomErrors = require(`./utilities/errors.JS`);
|
||||
const Messaging = require(`./utilities/messaging.JS`);
|
||||
|
||||
class DBManagement {};
|
||||
DBManagement.Activities = require(`./database/management/management_activity.JS`);
|
||||
DBManagement.Users = require(`./database/management/management_user.JS`);
|
||||
|
||||
class ResponseGenerator {};
|
||||
ResponseGenerator.Activities = require(`./response/activity.JS`);
|
||||
ResponseGenerator.Users = require(`./response/user.JS`);
|
||||
ResponseGenerator.Log = require(`./response/log.JS`);
|
||||
|
||||
class ExerciseTrackerAPI {
|
||||
constructor() {
|
||||
#paths = {
|
||||
"users": ["users"],
|
||||
"exercises": ["users/:_id/exercises"],
|
||||
"logs": ["users/:_id/logs"]
|
||||
}
|
||||
|
||||
constructor(INSTANCE) {
|
||||
this[`instance`] = INSTANCE;
|
||||
this[`instance`].use(bodyParser.json());
|
||||
this[`instance`].use(bodyParser.urlencoded({extended: true}));
|
||||
|
||||
const init_management = () => {
|
||||
this[`management`] = {};
|
||||
|
||||
Object.keys(DBManagement).forEach((DATABASE) => {
|
||||
this.management[DATABASE] = new DBManagement[DATABASE]();
|
||||
});
|
||||
this.manageUsers();
|
||||
};
|
||||
|
||||
init_management();
|
||||
}
|
||||
|
||||
/*
|
||||
Manage the users.
|
||||
*/
|
||||
manageUsers () {
|
||||
/*
|
||||
Create new users.
|
||||
*/
|
||||
const users_creation = async (REQUEST, RESPONSE) => {
|
||||
try {
|
||||
if (((REQUEST.body) ? Object.keys(REQUEST.body).length : false) ? Object.keys(REQUEST.body).includes(`username`) : false) {
|
||||
this.management[`Users`].create(REQUEST.body[`username`]).then((RESULT) => {
|
||||
let FORMATTED = (new ResponseGenerator.Users(RESULT)).format();
|
||||
RESPONSE.send(FORMATTED);
|
||||
})
|
||||
} else {
|
||||
throw new CustomErrors.Data.Missing(REQUEST.body);
|
||||
}
|
||||
} catch(ERR) {
|
||||
Messaging.exception(RESPONSE, ERR);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Search for all users.
|
||||
*/
|
||||
const users_display = (REQUEST, RESPONSE) => {
|
||||
try {
|
||||
let CRITERIA = (((REQUEST.query) ? Object.keys(REQUEST.query).length : false) || ((REQUEST.body) ? Object.keys(REQUEST.body).length : false)) ? (((REQUEST.body) ? Object.keys(REQUEST.body).length : false) ? REQUEST.body : REQUEST.query) : {};
|
||||
|
||||
this.management[`Users`].search(CRITERIA).then((RESULT) => {
|
||||
let FORMATTED = (new ResponseGenerator.Users(RESULT)).format();
|
||||
RESPONSE.send(FORMATTED);
|
||||
})
|
||||
} catch(ERR) {
|
||||
Messaging.exception(RESPONSE, ERR);
|
||||
}
|
||||
}
|
||||
|
||||
this.#paths[`users`].forEach((PATH) => {
|
||||
this[`instance`].get(`/api/${PATH}`, users_display)
|
||||
console.log(`User search is ready on ${PATH}.`);
|
||||
});
|
||||
|
||||
this.#paths[`users`].forEach((PATH) => {
|
||||
this[`instance`].post(`/api/${PATH}`, users_creation)
|
||||
console.log(`User creation is ready on ${PATH}.`);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue