This project is a sample of creating a Restful Web API that supports an online storefront to showcase its great product ideas.
- Node and Express Environment
- Postgres for the database
- dotenv from npm for managing environment variables
- db-migrate from npm for migrations
- jsonwebtoken from npm for working with JWTs
- jasmine from npm for testing
- supertest from npm for testing
- Simple-node-logger package
- typescript
- cors
- body-parser
- bcrypt
- nodemon
- ts-node
- ES6
This application uses PostgreSQL database, so as prerequisite you need to set up PostgreSQL database server as per requirments shared below:
- install and configure PostgreSQL database server on your machine
- in order to use PostgreSQL you will need a user/password as well as a database. First use the following to become the postgres user, then create a user on PostgreSQL with your user name through PSQL terminal
psql postgres postgres
- create a database user and set its password
postgres=# CREATE USER donut_user WITH PASSWORD 'PASSWORD';
- create two databases while logged in as postgres user
postgres=# CREATE DATABASE donut_prod; postgres=# CREATE DATABASE donut_test;
- Grant all database privileges to the created user in both databases
postgres=# GRANT ALL PRIVILEGES ON DATABASE donut_prod TO donut_user; postgres=# GRANT ALL PRIVILEGES ON DATABASE donut_test TO donut_user;
- test the database connection using these new credentials
- install packages by running this command
npm install
oryarn
. - add a .env file in the root directory and set the missing ### environment parameters
ENV=prod PORT=3000 URL=http://localhost DB_DRIVER=pg HOST_DEV=127.0.0.1 DB_DEV=donut_dev USER_DEV=donut_user PASSWORD_DEV=PASSWORD HOST_TEST=127.0.0.1 DB_TEST=donut_test USER_TEST=donut_user PASSWORD_TEST=PASSWORD HOST_PROD=127.0.0.1 DB_PROD=donut_prod USER_PROD=donut_user PASSWORD_PROD=PASSWORD BCRYPT_PASSWORD=PASSWORD SALT_ROUNDS=10 TOKEN_SECRET=SECRET
- build the app by running
npm run build
. - batabase and backend are running on port 3000 .
- run this command to setup the database
db-migrate up -e prod
. - you can choose your preferred Port by changing its value in the .env file
- run this command
npm run start
to start the app and get access via http://127.0.0.1:3000
- Endpoint Name -
index
- Method -
GET
- URL Pattern -
/categories
- Usage
- Open BASE_URL/categories in browser
- Terminal/CURL
curl -X GET BASE_URL/categories
- Expected Response - JSON containing all categories in the database
- Endpoint Name -
show
- Method -
GET
- URL Pattern -
/categories/{id}
- Usage
- Open BASE_URL/categories/{id} in browser
- Terminal/CURL
curl -X GET BASE_URL/categories/{id}
- Expected Response - Category with the {id} in database
- Endpoint Name -
create
- Method -
POST
- URL Pattern -
/categories
- Usage - CURL OR POSTMAN ONLY
- Terminal/CURL
curl -X POST \ -d '{ "id": 0, "name": "Mobiles" }' \ BASE_URL/categories
- Expected Response - Addition successful without any error message and returning the added category.
- Endpoint Name -
update
- Method -
PUT
- URL Pattern -
/categories/{id}
- Usage - CURL OR POSTMAN ONLY
- Terminal/CURL
curl -X PUT \ -d '{ "id": 1, "name": "Computers", }' \ BASE_URL/categories/{id}
- Expected Response - Update successful without any error message and return the updated category.
- Endpoint Name -
destroy
- Method -
DELETE
- URL Pattern -
/categories/{id}
- Usage - CURL OR POSTMAN ONLY
- Terminal/CURL
curl -X DELETE \ BASE_URL/categories/{id}
- Expected Response - Deletion successful without any error message and returning the deleted category.
- Endpoint Name -
index
- Method -
GET
- URL Pattern -
/statuses
- Usage
- Open BASE_URL/statuses in browser
- Terminal/CURL
curl -X GET BASE_URL/statuses
- Expected Response - JSON containing all statuses in the database
- Endpoint Name -
show
- Method -
GET
- URL Pattern -
/statuses/{id}
- Usage
- Open BASE_URL/statuses/{id} in browser
- Terminal/CURL
curl -X GET BASE_URL/statuses/{id}
- Expected Response - Status with the {id} in database
- Endpoint Name -
create
- Method -
POST
- URL Pattern -
/statuses
- Usage - CURL OR POSTMAN ONLY
- Terminal/CURL
curl -X POST \ -d '{ "id": 0, "name": "complete", }' \ BASE_URL/statuses
- Expected Response - Addition successful without any error message and returning the added status.
- NOTE - You have to add 'active' and 'complete'
- Endpoint Name -
update
- Method -
PUT
- URL Pattern -
/statuses/{id}
- Usage - CURL OR POSTMAN ONLY
- Terminal/CURL
curl -X PUT \ -d '{ "id": 1, "name": "active", }' \ BASE_URL/statuses/{id}
- Expected Response - Update successful without any error message and return the updated status.
- Endpoint Name -
destroy
- Method -
DELETE
- URL Pattern -
/statuses/{id}
- Usage - CURL OR POSTMAN ONLY
- Terminal/CURL
curl -X DELETE \ BASE_URL/statuses/{id}
- Expected Response - Deletion successful without any error message and returning the deleted status.
- Endpoint Name -
index
- Method -
GET
- URL Pattern -
/users
- Usage
- Open BASE_URL/users in browser
- Terminal/CURL
curl -X GET BASE_URL/users -H "Authorization: Bearer <ACCESS_TOKEN>" \
- Expected Response - JSON containing all users in the database
- Endpoint Name -
show
- Method -
GET
- URL Pattern -
/users/{id}
- Usage
- Open BASE_URL/users/{id} in browser
- Terminal/CURL
curl -X GET BASE_URL/users/{id} -H "Authorization: Bearer <ACCESS_TOKEN>" \
- Expected Response - User with the {id} in database
- Endpoint Name -
create
- Method -
POST
- URL Pattern -
/users
- Usage - CURL OR POSTMAN ONLY
- Terminal/CURL
curl -X POST \ -d '{ "id": 0, "first_name": "Murad", "last_name": "Zyad", "user_name": "mrd", "password": "456" }' \ BASE_URL/users
- Expected Response - Addition successful without any error message and returning the token.
- Endpoint Name -
update
- Method -
PUT
- URL Pattern -
/users/{id}
- Usage - CURL OR POSTMAN ONLY
- Terminal/CURL
curl -X PUT \ -H "Authorization: Bearer <ACCESS_TOKEN>" \ -d '{ "id": 1, "first_name": "Gamal", "last_name": "Sad", "user_name": "gmy", "password": "123" }' \ BASE_URL/users/{id}
- Expected Response - Update successful without any error message and return the token.
- Endpoint Name -
authenticate
- Method -
POST
- URL Pattern -
/users/authenticate
- Usage - CURL OR POSTMAN ONLY
- Terminal/CURL
curl -X POST \ -d '{ "user_name": "mrd", "password": "456" }' \ BASE_URL/users/authenticate
- Expected Response - Logging successful without any error message and returning the user logged in.
- Endpoint Name -
destroy
- Method -
DELETE
- URL Pattern -
/users/{id}
- Usage - CURL OR POSTMAN ONLY
- Terminal/CURL
curl -X DELETE \ -H "Authorization: Bearer <ACCESS_TOKEN>" \ BASE_URL/users/{id}
- Expected Response - Deletion successful without any error message and returning the deleted user.
- Endpoint Name -
index
- Method -
GET
- URL Pattern -
/products
- Usage
- Open BASE_URL/products in browser
- Terminal/CURL
curl -X GET BASE_URL/products
- Expected Response - JSON containing all products in the database
- Endpoint Name -
show
- Method -
GET
- URL Pattern -
/products/{id}
- Usage
- Open BASE_URL/products/{id} in browser
- Terminal/CURL
curl -X GET BASE_URL/products/{id}
- Expected Response - Product with the {id} in database
- Endpoint Name -
create
- Method -
POST
- URL Pattern -
/products
- Usage - CURL OR POSTMAN ONLY
- Terminal/CURL
curl -X POST \ -H "Authorization: Bearer <ACCESS_TOKEN>" \ -d '{ "id": 0, "name": "NOKIA 50", "price": 2100, "category_id": 1 }' \ BASE_URL/products
- Expected Response - Addition successful without any error message and returning the added product.
- Endpoint Name -
update
- Method -
PUT
- URL Pattern -
/products/{id}
- Usage - CURL OR POSTMAN ONLY
- Terminal/CURL
curl -X PUT \ -H "Authorization: Bearer <ACCESS_TOKEN>" \ -d '{ "id": 1, "name": "OPPO XI-3200", "price": 7500, "category_id": 1 }' \ BASE_URL/products/{id}
- Expected Response - Update successful without any error message and return the updated product.
- Endpoint Name -
destroy
- Method -
DELETE
- URL Pattern -
/products/{id}
- Usage - CURL OR POSTMAN ONLY
- Terminal/CURL
curl -X DELETE \ -H "Authorization: Bearer <ACCESS_TOKEN>" \ BASE_URL/products/{id}
- Expected Response - Deletion successful without any error message and returning the deleted product.
- Endpoint Name -
index
- Method -
GET
- URL Pattern -
/orders
- Usage
- Open BASE_URL/orders in browser
- Terminal/CURL
curl -X GET BASE_URL/orders -H "Authorization: Bearer <ACCESS_TOKEN>" \
- Expected Response - JSON containing all orders in the database
- Endpoint Name -
show
- Method -
GET
- URL Pattern -
/orders/{id}
- Usage
- Open BASE_URL/orders/{id} in browser
- Terminal/CURL
curl -X GET BASE_URL/orders/{id} -H "Authorization: Bearer <ACCESS_TOKEN>" \
- Expected Response - Order with the {id} in database
- Endpoint Name -
create
- Method -
POST
- URL Pattern -
/orders
- Usage - CURL OR POSTMAN ONLY
- Terminal/CURL
curl -X POST \ -H "Authorization: Bearer <ACCESS_TOKEN>" \ -d '{ "id": 0, "status_id": 1, "user_id": 1 }' \ BASE_URL/orders
- Expected Response - Addition successful without any error message and returning the added order.
- Endpoint Name -
update
- Method -
PUT
- URL Pattern -
/orders/{id}
- Usage - CURL OR POSTMAN ONLY
- Terminal/CURL
curl -X PUT \ -H "Authorization: Bearer <ACCESS_TOKEN>" \ -d '{ "id": 1, "status_id": 2, "user_id": 1 }' \ BASE_URL/orders/{id}
- Expected Response - Update successful without any error message and return the updated order.
- Endpoint Name -
destroy
- Method -
DELETE
- URL Pattern -
/orders/{id}
- Usage - CURL OR POSTMAN ONLY
- Terminal/CURL
curl -X DELETE \ -H "Authorization: Bearer <ACCESS_TOKEN>" \ BASE_URL/orders/{id}
- Expected Response - Deletion successful without any error message and returning the deleted order.
- Endpoint Name -
addProduct
- Method -
POST
- URL Pattern -
/orders/{id}/products
- Usage - CURL OR POSTMAN ONLY
- Terminal/CURL
curl -X PUT \ -H "Authorization: Bearer <ACCESS_TOKEN>" \ -d '{ "quantity": 15, "productId": 1 }' \ BASE_URL/orders/{id}/products
- Expected Response - Addition successful without any error message and returning the added product.
- Endpoint Name -
showProducts
- Method -
GET
- URL Pattern -
/users/{userID}/orders/{orderID}/products
- Usage
- Open BASE_URL/users/{userID}/orders/{orderID}/products in browser
- Terminal/CURL
curl -X GET BASE_URL/users/{userID}/orders/{orderID}/products -H "Authorization: Bearer <ACCESS_TOKEN>" \
- Expected Response - Products of the order with the {orderID} in database
- Endpoint Name -
getTop5Products
- Method -
GET
- URL Pattern -
/products-top-5
- Usage
- Open BASE_URL/products-top-5 in browser
- Terminal/CURL
curl -X GET BASE_URL/products-top-5
- Expected Response - List of the 5 top most popular products in the database
- Endpoint Name -
getProductsByCat
- Method -
GET
- URL Pattern -
/products-by-cat
- Usage - CURL OR POSTMAN ONLY
- Terminal/CURL
curl -X GET BASE_URL/products-by-cat -d '{ "category": "Mobiles" }' \
- Expected Response - List of products that belong to the selected category in the database
- Endpoint Name -
getActOrderByUsr
- Method -
GET
- URL Pattern -
/active-order
- Usage - CURL OR POSTMAN ONLY
- Terminal/CURL
curl -X GET BASE_URL/active-order -H "Authorization: Bearer <ACCESS_TOKEN>" \ -d '{ "user_id": 1 }' \
- Expected Response - Current Order by user in the database
- Endpoint Name -
getCmpOrdersByUsr
- Method -
GET
- URL Pattern -
/complete-orders
- Usage - CURL OR POSTMAN ONLY
- Terminal/CURL
curl -X GET BASE_URL/complete-orders -H "Authorization: Bearer <ACCESS_TOKEN>" \ -d '{ "user_id": 1 }' \
- Expected Response - Complete Orders by user in the database
Run prettier
npm run prettier
Run eslint
npm run lint
Build the project
npm run build
Run the application
npm run start
Clone the project
git clone https://github.com/Mahmoud-Elgharably/Store-Donut.git
Go to the project directory
cd Store-Donut
Install dependencies - (then follow the above Instructions)
npm install
Run the application
npm run start
To run tests, run the following command
npm run test