Skip to content

mahmoud-elgharably/Store-Donut

Repository files navigation

Store Donut

Overview | Description

This project is a sample of creating a Restful Web API that supports an online storefront to showcase its great product ideas.

Table of Contents

Technicals:

  1. Node and Express Environment
  2. Postgres for the database
  3. dotenv from npm for managing environment variables
  4. db-migrate from npm for migrations
  5. jsonwebtoken from npm for working with JWTs
  6. jasmine from npm for testing
  7. supertest from npm for testing
  8. Simple-node-logger package
  9. typescript
  10. cors
  11. body-parser
  12. bcrypt
  13. nodemon
  14. ts-node
  15. ES6

Prerequisite

This application uses PostgreSQL database, so as prerequisite you need to set up PostgreSQL database server as per requirments shared below:

  1. install and configure PostgreSQL database server on your machine
  2. 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
  3. create a database user and set its password
    postgres=# CREATE USER donut_user WITH PASSWORD 'PASSWORD';
  4. create two databases while logged in as postgres user
    postgres=# CREATE DATABASE donut_prod;
    postgres=# CREATE DATABASE donut_test;
  5. 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;
  6. test the database connection using these new credentials

Instructions

  1. install packages by running this command npm install or yarn.
  2. 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
  3. build the app by running npm run build.
  4. batabase and backend are running on port 3000 .
  5. run this command to setup the database db-migrate up -e prod.
  6. you can choose your preferred Port by changing its value in the .env file
  7. run this command npm run start to start the app and get access via http://127.0.0.1:3000

API Reference

1. View Categories

  • 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

2. View Single Category

  • 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

3. Add Category

  • 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.

4. Update 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.

5. Delete 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.

6. View Statuses

  • 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

7. View Single Status

  • 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

8. Add Status

  • 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'

9. Update Status

  • 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.

10. Delete 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.

11. View Users

  • 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

12. View Single User

  • 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

13. Add User

  • 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.

14. Update User

  • 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.

15. Authenticate User

  • 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.

16. Delete User

  • 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.

17. View Products

  • 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

18. View Single Product

  • 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

19. Add Product

  • 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.

20. Update 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.

21. Delete 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.

22. View Orders

  • 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

23. View Single Order

  • 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

24. Add Order

  • 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.

25. Update 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.

26. Delete 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.

27. Add products to the current 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.

28. Display the products of the selected order belonging to the selected user

  • 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

29. Display top 5 most popular products

  • 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

30. Display Products by category

  • 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

31. Display active order by user

  • 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

32. Display complete orders by user

  • 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

Scripts

Run prettier

  npm run prettier

Run eslint

  npm run lint

Build the project

  npm run build

Run the application

  npm run start

Run Locally

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

Running Tests

To run tests, run the following command

  npm run test

Author

Mahmoud Elgharably

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published