sleep-tracker-backend/README.md

7.8 KiB
Raw Blame History

Sleep Tracker helps Users track their sleep and mood every day. It gives Users a better idea of their sleep pattern and improve their health. After one month, Sleep Tracker will give recommendation of optimal sleep duration.

Target Audience

Sleep Tracker is intended for anyone interested in improving their sleep and health.

Features

  • Homepage view shows a graph of your nightly hours of sleep over time .
  • Ability to create a nights sleep entry. For each date set (1/1/19-1/2/19), user can scroll through a digital clock image (like when you set your alarm on your phone, or just type in) to enter what time they got in bed and what time they woke up. From this data, app will calculate their total time in bed.
  • Ability to edit or delete a sleep entry.
  • Ability to click one of four emoji buttons to rate how they felt right when they woke up, how they felt during the day, and how tired they felt when they went to bed. Behind the scenes, a score 1-4 is given for each emoji.
  • The app will calculate an average mood score for 1/2/19, and compare it to the time spent in bed. The app will then recommend after using for >1 month how much sleep you need. “Your mood score tends to be highest when you sleep 7.5 hours”

Possible further features

  • 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

API Documentation


Values required in bold.

All requests need to provide with a JSON Web Token for authentication, except for /api/auth/register and /api/auth/login.

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

Add a new user

POST /api/auth/register

Request

A json object with email, username and 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.

{
	"id": 1,
	"email": "gabe@ls.com",
	"username": "gabe"
}

Login with user/password

POST /api/auth/login

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
  }
]