From 0c70ee87712b0ba6f73b082a16983a7ff4d82c1f Mon Sep 17 00:00:00 2001 From: ruihildt Date: Thu, 1 Aug 2019 22:31:44 +0200 Subject: [PATCH] Add documentation and correct typo --- README.md | 294 ++++++++++++++++--- data/sleep-tracker.db3 | Bin 36864 -> 36864 bytes services/dailyAverages/dailyAveragesModel.js | 2 +- services/sessions/sessionsRouter.js | 4 +- services/users/usersRouter.js | 4 +- 5 files changed, 262 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index d76598e..a9c8f76 100644 --- a/README.md +++ b/README.md @@ -16,48 +16,268 @@ Sleep Tracker is intended for anyone interested in improving their sleep and hea - Make recommendations not only based on sleep time, but also on bed time (some reasearch will need to be done in that area) - Connect to third party sleep tracking services -## Database Schemas +## API Documentation +___ +> Values required in **`bold`**. -### Users Schema `users` -| field | data type | metadata | -| :--------| :--------------- | :-------------------------------------------------- | -| id | unsigned integer | primary key, auto-increments, generated by database | -| email | string | required | -| username | string | required | -| password | string | required | +> All requests need to provide with a JSON Web Token for authentication, except for **`/api/auth/register`** and **`/api/auth/login`**. -### Sleep Sessions Schema `sessions` -| field | data type | metadata | -| :------------ | :--------------- | :-------------------------------------------------- | -| id | unsigned integer | primary key, auto-increments, generated by database | -| user_id | unsigned integer | foreign key referencing `users.id`, required | -| bed_time | unsigned integer | timestamp with date, required | -| wake_time | unsigned integer | timestamp with date, required | -| bed_tiredness | unsigned integer | | -| wake_mood | unsigned integer | | -| day_mood | unsigned integer | | +### USERS SCHEMA | **`users`** +| field | data type | metadata | +| :--------| :---------------- | :-------------------------------------------------- | +| id | unsigned integer | primary key, auto-increments, generated by database | +| email | **`string`** | required | +| username | **`string`** | required | +| password | **`string`** | required | -### Average Mood per Sleep Session Schema `dailyAverages` -| field | data type | metadata | -| :------------- | :--------------- | :-------------------------------------------------- | -| id | unsigned integer | primary key, auto-increments, generated by database | -| session_id | unsigned integer | foreign key referencing `sessions.id` | -| user_id | unsigned integer | foreign key referencing `users.id` | -| sleep_duration | unsigned integer | | -| average_mood | float | | +#### Add a new user +**`POST /api/auth/register`** -## API Endpoints +##### Request +A json object with **`email`**, **`username`** and **`password`**. -- `POST /api/auth/register` - add a new user -- `POST /api/auth/login` - login with user/password +``` +{ + "email": "gabe@ls.com", + "username": "gabe", + "password": "long-and-complicated-random-password" +} +``` +##### Response +A json object with the the newly created user with `id`. -- `GET /api/users/:id` - fetch a user -- `PUT /api/users/:id` - update a user -- `DELETE /api/users/:id` - delete a user +``` +{ + "id": 1, + "email": "gabe@ls.com", + "username": "gabe" +} +``` -- `GET /api/users/:id/sessions` - fetch list of all sessions by user id -- `POST /api/users/sessions` - add a session -- `PUT /api/users/sessions/:id` - update a session by id -- `DELETE /api/users/sessions/:id` - delete a session by id +#### Login with user/password +**`POST /api/auth/login`** -- `GET /api/users/:id/averages` - fetch a list of last 30 daily averages by user id \ No newline at end of file +##### Request +A json object with **`username`** and **`password`**. + +``` +{ + "username": "gabe", + "password": "long-and-complicated-random-password" +} +``` +##### Response +A json object with a `message` and a JSON Web `token`. + +``` +{ + "message": "Welcome gabe!", + "token": "JWT Token" +} +``` + +#### Fetch a user +**`GET /api/users/:id`** +> User **id** as params in the URL. + +##### Response +A json object with the user `id`, `email` and `username`. + +``` +{ + "id": 1, + "email": "gabe@ls.com", + "username": "gabe" +} +``` + +#### Update a user +**`PUT /api/users/:id`** +> User **id** as params in the URL. + +##### Request +A json object with `email` and/or `username`. + +``` +{ + "email": "gabe@ls.com", + "username": "gabe" +} +``` + +##### Response +A json object with the updated user `id`, `email` and `username`. + +``` +{ + "id": 1, + "email": "gabe@ls.com", + "username": "gabe" +} +``` + +#### delete a user +**`DELETE /api/users/:id`** +> User **id** as params in the URL. + +##### Response +A json object with a confirmation `message`. + +``` +{ + "message": 'The user was successfully deleted.' +} +``` +___ +### SESSIONS SCHEMA | **`sessions`** +| field | data type | metadata | +| :-------------- | :--------------- | :-------------------------------------------------- | +| id | unsigned integer | primary key, auto-increments, generated by database | +| **`user_id`** | unsigned integer | foreign key referencing `users.id`, required | +| **`bed_time`** | unsigned integer | timestamp with date, required | +| **`wake_time`** | unsigned integer | timestamp with date, required | +| bed_tiredness | unsigned integer | | +| wake_mood | unsigned integer | | +| day_mood | unsigned integer | | + +#### Fetch list of all sessions by user id +**`GET /api/users/:id/sessions`** + +> User **id** as params in the URL. + +##### Response +An array of json objects containing sessions related to the user with the provided `id`. +``` +[ + { + "id": 1, + "user_id": 1, + "bed_time": "2019-07-05 22:10:25", + "wake_time": "2019-07-06 07:10:25", + "wake_mood": 3, + "day_mood": 3, + "bed_tiredness": 3 + }, + { + "id": 2, + "user_id": 1, + "bed_time": "2019-07-05 22:10:25", + "wake_time": "2019-07-06 07:10:25", + "wake_mood": 5, + "day_mood": 5, + "bed_tiredness": 5 + } +] +``` + +#### Add a session +**`POST /api/users/sessions`** + +##### Request +A json object with **`user_id`**, **`bed_time`**, **`wake_time`** and optionnally `wake_mood`, `day_mood`, `bed_tiredness`. + +> NB: **`wake_mood`**, **`day_mood`**, and **`bed_tiredness`** are required for calculating daily average mood and sleep duration, but can be added later by updating the session. + +``` +{ + "user_id": 1, + "bed_time": "2019-07-05 22:10:25", + "wake_time": "2019-07-06 07:10:25", + "wake_mood": 3, + "day_mood": 3, + "bed_tiredness": 3 +} +``` +##### Response +A json object with the newly created session. + +``` +{ + "id": 1, + "user_id": 1, + "bed_time": "2019-07-05 22:10:25", + "wake_time": "2019-07-06 07:10:25", + "wake_mood": 3, + "day_mood": 3, + "bed_tiredness": 3 +} +``` + +#### Update a session by id +**`PUT /api/users/sessions/:id`** + +> Session **id** as params in the URL. + +##### Request +A json object with **`user_id`**, **`bed_time`**, **`wake_time`** and optionnally `wake_mood`, `day_mood`, `bed_tiredness`. + +``` +{ + "user_id": 1, + "bed_time": "2019-07-05 22:10:25", + "wake_time": "2019-07-06 07:10:25", + "wake_mood": 5, + "day_mood": 5, + "bed_tiredness": 5 +} +``` + +##### Response +A json object with the updated session with `id`. + +``` +{ + "id": 1, + "user_id": 1, + "bed_time": "2019-07-05 22:10:25", + "wake_time": "2019-07-06 07:10:25", + "wake_mood": 5, + "day_mood": 5, + "bed_tiredness": 5 +} +``` + +#### Delete a session by id +**`DELETE /api/users/sessions/:id`** + +> Session **id** as params in the URL. + +##### Response +A json object with a confirmation `message`. + +``` +{ + "message": 'The session has been successfully deleted.' +} +``` + +___ +### DAILY AVERAGES SCHEMA | **`dailyAverages`** +| field | data type | metadata | +| :------------------- | :--------------- | :-------------------------------------------------- | +| id | unsigned integer | primary key, auto-increments, generated by database | +| **`session_id`** | unsigned integer | foreign key referencing `sessions.id`, required | +| **`user_id`** | unsigned integer | foreign key referencing `users.id`, required | +| **`sleep_duration`** | unsigned integer | required | +| **`average_mood`** | float | required | + +> Daily averages are automatically created by the backend server when all values of a session are filled in. + +#### Fetch a list of last 30 (if possible) daily averages by user id +**`GET /api/users/:id/averages`** + +> User **id** as params in the URL. + +##### Response +An array of json objects containing sessions related to the user with the provided `id`. +``` +[ + { + "id": 1, + "session_id": 18, + "user_id": 1, + "sleep_duration": 8.58, + "average_mood": 2.67 + } +] +``` \ No newline at end of file diff --git a/data/sleep-tracker.db3 b/data/sleep-tracker.db3 index ad22d6f9600e54a71bc1f6da6eab10e5c26de422..559357f1ed9dad86e9842faa618e85cd62f191f9 100644 GIT binary patch delta 437 zcmZozz|^pSX@WGP-b5K^M!k&*k@`}+JS@zzjK!(N#hLke#qylY%;Jot#i>QbESoRu z>k07lGcz#ptz+O{%eM{$Hwy~PQ>{!97!&oeOb?_=QK$Nv!ofgV}MKlwpChZoEPEI_47 zAYZ^NX9fxgf&`49u3_S1R^|i=K>1w!Pk?6LV&MP5e+vXR3mTl_pZqSLL%;-NmQbOq(z3 z>j`i(FfcIjM=Q1v5z7EYi=lRxDL0Zo0)xmmyJBP)<~h;Q?w HJ~>7J#@#R> diff --git a/services/dailyAverages/dailyAveragesModel.js b/services/dailyAverages/dailyAveragesModel.js index 11ab16f..89edeb8 100644 --- a/services/dailyAverages/dailyAveragesModel.js +++ b/services/dailyAverages/dailyAveragesModel.js @@ -5,6 +5,6 @@ module.exports = { } function getDailyAverages(id) { - return db('sessions') + return db('dailyAverages') .where({ id }); } \ No newline at end of file diff --git a/services/sessions/sessionsRouter.js b/services/sessions/sessionsRouter.js index 6d10efc..fa3a7fd 100644 --- a/services/sessions/sessionsRouter.js +++ b/services/sessions/sessionsRouter.js @@ -27,7 +27,7 @@ router.put('/sessions/:id', async (req, res) => { if (session) { res.status(200).json(session); } else { - res.status(404).json({ message: 'The session could not be found' }); + res.status(404).json({ message: 'The session could not be found.' }); } }) .catch(err => { @@ -37,7 +37,7 @@ router.put('/sessions/:id', async (req, res) => { router.delete("/sessions/:id", async (req, res) => { Sessions.removeSession(req.params.id) - .then(() => res.status(200).json({ message: 'The session has been successfully deleted' })) + .then(() => res.status(200).json({ message: 'The session has been successfully deleted.' })) .catch(err => res.json(err)); }); diff --git a/services/users/usersRouter.js b/services/users/usersRouter.js index 64f96e2..e1240ee 100644 --- a/services/users/usersRouter.js +++ b/services/users/usersRouter.js @@ -19,12 +19,12 @@ router.put('/:id', async (req, res) => { res.status(404).json({ message: 'The user could not be found' }); } }) - .catch(err => res.status(500).json({ message: 'Error updating the user' })); + .catch(() => res.status(500).json({ message: 'Error updating the user' })); }); router.delete("/:id", (req, res) => { Users.removeUser(req.params.id) - .then(data => res.status(200).json(data)) + .then(() => res.status(200).json({ message: 'The user was successfully deleted.'})) .catch(err => res.json(err)); });