From dcbd5015d2779ecc56f45455f234d699fa6bedee Mon Sep 17 00:00:00 2001 From: buzz-lightsnack-2007 <73412182+buzz-lightsnack-2007@users.noreply.github.com> Date: Thu, 10 Apr 2025 22:50:20 +0800 Subject: [PATCH] Add activities management --- scripts/API.JS | 35 +++++++++++++++- scripts/database/entry/activity.JS | 17 ++++++++ .../management/management_activity.JS | 40 +++++++++++++++++++ 3 files changed, 91 insertions(+), 1 deletion(-) diff --git a/scripts/API.JS b/scripts/API.JS index 498a757..c3ad29f 100644 --- a/scripts/API.JS +++ b/scripts/API.JS @@ -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. */ diff --git a/scripts/database/entry/activity.JS b/scripts/database/entry/activity.JS index 1071f81..b6cc58d 100644 --- a/scripts/database/entry/activity.JS +++ b/scripts/database/entry/activity.JS @@ -11,8 +11,25 @@ class Activity extends Entry { constructor(PROPERTIES) { super(PROPERTIES); + + // Set a default date value. + this.date = this.date ? this.date : new Date(Date.now()); + this.set_date(this.date); }; + /* + Store the date with the corresponding ECMA value. If not, the existing date value is converted. + + @param {Date} DATE the date + @return {Number} the date in ECMA + */ + set_date(DATE) { + if (DATE || this.date) { + this.date = parseInt((new Date(DATE || this.date)).getTime()); + return (this.date); + } + } + /* Generate a schema for MongoDB. diff --git a/scripts/database/management/management_activity.JS b/scripts/database/management/management_activity.JS index 8bfffd9..c79b99c 100644 --- a/scripts/database/management/management_activity.JS +++ b/scripts/database/management/management_activity.JS @@ -1,2 +1,42 @@ const DBManagement = require(`./management.JS`); +const Activity = require(`../entry/activity.JS`); + +class ActivityManagement extends DBManagement { + constructor() { + super(`localhost:27017`, `Activity-Tracker`); + this.configure(`Activities`, Activity.generateTemplate()); + }; + + /* + Create a user. The ID will be automatically generated. + + @param {string} DETAILS the activity's details + @param {object} OPTIONS the account options + @return {object} the user data + */ + async create(DETAILS, OPTIONS, done) { + await this.state; + + /* + Create an activity. + */ + const createActivity = async () => { + let CREATED = new Activity(DETAILS); + + let ENTRY = new this.model(CREATED); + try { + let RESULT = await ENTRY.save(); + } catch(ERROR) { + throw new CustomErrors.DBProblem(ERROR.message); + } + + return (CREATED); + } + + let CURRENT = await createActivity(); + return (CURRENT); + }; +} + +module.exports = ActivityManagement; \ No newline at end of file