Add documentation and correct typo

This commit is contained in:
ruihildt 2019-08-01 22:31:44 +02:00
parent 31bbcb52c7
commit 0c70ee8771
5 changed files with 262 additions and 42 deletions

294
README.md
View File

@ -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
##### 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
}
]
```

Binary file not shown.

View File

@ -5,6 +5,6 @@ module.exports = {
}
function getDailyAverages(id) {
return db('sessions')
return db('dailyAverages')
.where({ id });
}

View File

@ -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));
});

View File

@ -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));
});