- Clone the repo to your local mashine;
- Open terminal in project's directory;
- Call
npm install
in terminal; - Create
.env
file in project's parent dir; - Insert your API key to the end after ' ':
API_KEYS='old-api-keys your-new-api-key'
and your secret token:TOKEN_SECRET='your-secret-token'
; - If testing locally, create local MySQL database (read below);
- Check database connection credencials: connection/db.js;
- Call
npm start
in terminal to run the server (If having issues with reading API_KEY from .env file, callnpm install dotenv
and re-run the server). - When sending token in Authorization header, don't forget to add prefix
Bearer
to it!
- Get random recipe(s):
/recipes/random
. Query params:number
- number of results from 0 to 100 (optional, by def. 1);tags
- comma-separated search tags (optional):/recipes/random?number=5&tags=vegetarian,dessert
; - Search recipe(s):
/recipes/search
. Query params:query
- search query string;number
- number of results to return (optional);cuisine
- comma-separated cuisines (optional);diet
- comma-separated diets (optional);intolerances
- comma-separated intolerances (optional); List of supported diets, cuisines and intolerances; - Get recipe by id:
/recipes/{id}
; - Check user's authentication:
/users/auth
with header:"Authorization": "Bearer your-secret-token"
. Returns200(OK)
with JSON-body:{ "username" : "your-username" }
or401(Unauthorized)
. Used to check if the user is logged in already. - Get saved recipes for current user:
/saved
with header:Authorization: Bearer your-secret-token
. Returns full recipe-data array in JSON:[ {...} ]
. - Get saved to calendar recipes for current user:
/calendar
with header:Authozisation: Bearer your-secret-token
. Returns JSON-body in format:[ { "recipe": {...}, "date": "yyyy-MM-dd" } ]
. Sorted by date; - Get saved user preferences (diets, intolerances and cuisines):
/preferences
with header:Authozisation: Bearer your-secret-token
. Returns200(OK)
and JSON-body in format:{ "intolerances" : [ "Gluten", "Peanut" ], "cuisine" : [ "German" ] }
. Category can be missing, if there are no preferences of it saved. Can return401
if not authorized.
- Login user:
/users/login
with JSON-body in format:{ "username": "your-username", "password": "your-password-md5-hash" }
. Returns status200(OK)
with JSON-body{ "username" : "your-username", "token" : "your-secret-token" }
, or404 (Not found)
; - Register user:
/users/register
with JSON-body in format:{ "username": "your-username", "password": "your-password-md5-hash" }
. Returns status200(OK)
with JSON-body{ "username" : "your-username", "token" : "your-secret-token" }
, or409(Conflict)
if username already exists. Returns200(OK)
or500
if there is a db error (e.g. duplicate); - Save recipe for current user:
/saved
with header:"Authorization": "Bearer your-secret-token"
and JSON-body in format:{ "recipeId": 664087 }
. Returns200(OK)
or400
with message if request body is wrong or401(Unathorized)
if token is wrong or illegal, or500
if there is a db error (e.g. duplicate); - Save recipe to calendar for the current user:
/calendar
with header:"Authorization": "Bearer your-secret-token"
and JSON-body in format:{ "recipeId" : 715383, "date" : "yyyy-MM-dd" }
. Returns200(OK)
or400
with message if request body is wrong or401(Unathorized)
if token is wrong or illegal, or500
if there is a db insertion error. - Save new preferences for current user:
/preferences
with header:Authozisation: Bearer your-secret-token
and JSON-body in format:{ "intolerances" : [ "Gluten", "Peanut" ], "cuisine" : [ "German" ] }
. Important! : in order to simplify the process of saving preferences, only the structure of sent body is checked (not empty, all properties contain not empty arrays: otherwise return400(Bad Request)
). Semantically the body won't be checked, and no errors will be thrown. If you try to save already saved preference, it won't cause a conflict. "New" preferences, that are semantically right, will be saved. Also, adding new preferences doesn't replace the old ones. Already added will remain in database, complemented by new ones. To replace all saved preferences with new ones, passed in body, use PUT method (find below).
- Replace already saved user preferences with new ones:
/preferences
with header:Authozisation: Bearer your-secret-token
and JSON-body in format:{ "intolerances" : [ "Gluten", "Peanut" ], "cuisine" : [ "German" ] }
. All previously saved preferences for current user will be firstly deleted and then new ones from body will be saved. The saving mechanism is the same as in POST method (find above).
- Update date of saved to calendar recipe for current user:
/calendar
with header:"Authorization": "Bearer your-secret-token"
and JSON-body in format:{ "recipeId" : 715383, "date" : yyyy-MM-dd", "newDate" : "yyyy-MM-dd" }
. Returns200(OK)
or400
with message if request body is wrong, or401(Unathorized)
if token is wrong or illegal, or500
if there is a db update error.
- Delete saved to calendar recipe for current user:
/calendar
with header:"Authorization": "Bearer your-secret-token"
and JSON-body in format:{ "recipeId" : 715383, "date" : "yyyy-MM-dd" }
. Returns200(OK)
or400
with message if request body is wrong, or401(Unathorized)
if token is wrong or illegal, or500
if there is a db deletion error. - Delete saved recipe for current user:
/saved
with header:"Authorization": "Bearer your-secret-token"
and JSON-body in format:{ "recipeId" : 715383 }
. Returns200(OK)
or400
with message if request body is wrong or nothing was deleted (e.g. not found), or401(Unathorized)
if token is wrong or illegal, or500
if there is a db deletion error. - Delete user:
/users
with header:"Authorization": "Bearer your-secret-token"
and password MD5 hash in JSON-body:{ "password" : "your-password-md5-hash" }
. Returns200(OK)
or400
if password is wrong, or500
if SQL error has occured. - Delete all saved preferences for current user:
/preferences
with header:"Authorization": "Bearer your-secret-token"
. Returns200(OK)
or401(Unathorized)
if token is wrong or illegal, or500
if there is a db deletion error.
Heroku add-on ClearDB is used. Accessible on Heroku dashboard. ClearDB breaks persistent connections, so we are using connection pool. No error will be thrown in case if there is no free connections right now, the request will be enqueued (small delay is possible).
- Install MySQL Server (and some tool with interface to work with DB: MySQL Workbench, DataGrip, DBeaver);
- Create local database with the name
recipe_planner_db
and run there these SQL statements. This will create a DB scheme and add a default user inuser_accout
table for early login testing; - Database connection in server project is encapsulated in connection/db.js. In other modules just call a global variable
db
. Check connection credencials before running (use your local credencials if running locally, otherwise use Heroku ClearDB credencials);
200
- OK400
- Bad request (usually with message, request body or query params are wrong)401
- Unauthorized (token missing or illegal)404
- Not found (usually when url is wrong)409
- Conflict (usually when trying to register already registered user)500
- Internal Server Error (usually SQL or external API, e.g. FK constraint failed, or Spoonacular API request failed)
- Spoonacular API
- Supported tags (cuisines, diets, intolerances) - can be used with
/random
,/search
and/preferences
endpoints.
- Kyrylo Terentiev
- Patrick Andriamaso
- Florian Göth