Skip to content

mBahrawy/api-with-postgresql-and-express-project

Repository files navigation

Store front Backend API (Node, Express, PostgresSQL)

This project for Udacity EgFwd Fullstack Nanodegree project

Download Postman API collection:

How to run Image the Store front API project:

First, open terminal inside project directory, and run these commands

  • npm i for installing needed dependencies.
  • npm start for running development environment (through port will be 4000).
  • npm run build for building the production version.
  • npm run serve for running the production version (through port will be 8000).
  • npm run test for running implemneted unit tests (through port will be 6000).

Used ports:

Port Usage
4000 running app in development mode
6000 running app tests
8000 running app in production mode
5432 Postgres Database

Users and roles

Role Description
Admin An administrator user acccount for the app, It will be create once at the first time you start the app. You can change login info for the .env file. The info inside .env can be modifed once per app installation, or edit mamually inside database.
Regular Normal user, or clint

Application API documentation:

  • Used Authentication type is Bearer by JWT plugin

Not authorized/public APIs:

Endpoint Method Auth Role/Constrain Usage Body sample
/login POST No login user, works with for all user roles {  "username": "user", "password": "user123"}
/register POST No create new user {

 "firstname": "user",
 "lastname": "user",
 "username": "user",
 "email": "user@user.com",
 "password": "user123"
}
---- ---- ---- ---- ----
/products/:id GET No Getting a single product with product id
/products/category/:id GET No Getting all products related to category id
/products POST Yes Admin only Will create a new product {

 "name": "USB cable 3",
 "price": 10,
 "stock": 250
}
/products/:id DELETE Yes Admin only Will delete a product with id
---- ---- ---- ---- ----
/orders GET Yes Admin only Getting all orders
/orders/:id GET Yes Admin
or order_owner
Get order details with
/orders POST Yes Any role Create new order, Empty or with products {
  "products": [
   { "id": 11, "quantity": 2 },
   { "id": 12, "quantity": 3 }
 ]
}
/orders/:id/products PUT Yes Admin
or order_owner
Add a product to order, must be with status is 'open'
/orders/:id/complete PUT Yes order_owner only Change order status from 'open' to 'completed',
It must be an order with products and its previous status is 'opne',
User can add a 0-5 star rate and leave a text feedback.
{
 "service_rating": 2.5,
 "feedback": "good service."
}
---- ---- ---- ---- ----
/orders/categories GET NO Getting all added categories
/orders/categories/:d GET NO Getting a single category information by category id
/orders/categories POST Yes Admin only Will create a new category {
 "name": "Mobiles",
 "description": "All about mobiles"
}
/orders/categories/:d DELETE Yes Admin only Will delete a category
---- ---- ---- ---- ----
/orders/reviews GET Yes Admin only Get all reviews, please note that review id is the same crosponsind order id
/orders/reviews/:id GET Yes Admin only Get a single review with order id
/orders/reviews POST Yes order_owner only Will leave a review for an order, It must be completed, have products, and wasn't already reviwed {

 "id": 13,
 "service_rating": 1.5,
 "feedback": "good service."
}
---- ---- ---- ---- ----
/orders/users GET Yes Admin only Get all users in databse
/orders/users/:id GET Yes Admin only Get a single user with user id
/orders/users/:id DELETE Yes Admin only Delete a user with user id

Application environment file:

Create .env in app directory

# Auto genrated admin account on first time loading the application
ADMIN_NAME=first_admin
ADMIN_EMAIL=first_admin@admin
ADMIN_PASSWORD=Admin123

# Application ports
APP_BACKEND_PORT_DEVELOPMENT=4000
APP_BACKEND_PORT_PRODUCTION=5000
APP_BACKEND_PORT_TEST=6000

# Application base url/domains
APP_BACKEND_BASE_URL_development=http://localhost
APP_BACKEND_BASE_URL_production=http://localhost
APP_BACKEND_BASE_URL_test=http://localhost

# Application databases
POSTGRES_DATABASE_development=store_front
POSTGRES_DATABASE_production=store_front_dev
POSTGRES_DATABASE_test=store_front_test

# Postgres databse connection
POSTGRES_HOST=127.0.0.1
POSTGRES_USERNAME=root
POSTGRES_PASSWORD=root
POSTGRES_PORT=5432

# JWD securing tokens
BCRYPT_PASSWORD=kds7ys.8G5$%tfs
SALT_ROUNDS=12
TOKEN_SECRET=JWTTOKENSERCRET

Database setup and configration:

1. Create the needed databases

1- Open cmd or terminal 2- Conenct to postgres by typing: psql -h 127.0.0.1 -U postgres postgres 2- Enter your password (use postgres) 3- Create a root user if doesnt exists CREATE USER root WITH PASSWORD 'root'; 3- Exit psql terminal by \q and login again with root account: psql -h 127.0.0.1 -U root postgres 4- Type paswword root 5- Create 3 databases 1- CREATE DATABASE store_front; 2- CREATE DATABASE store_front_dev; 3- CREATE DATABASE store_front_test;

2. Create databse config file

Create database.json file in add directory

{
    "test": {
        "driver": "pg",
        "host": "127.0.0.1",
        "database": "store_front_test",
        "user": "root",
        "password": "root"
    },
    "development": {
        "driver": "pg",
        "host": "127.0.0.1",
        "database": "store_front_dev",
        "user": "root",
        "password": "root"
    },
        "production": {
        "driver": "pg",
        "host": "127.0.0.1",
        "database": "store_front",
        "user": "root",
        "password": "root"
    },
    "sql-file" : true
}

Database tables

  • migrations
  • users
  • categories
  • products
  • orders
  • order_products
  • reviews

Database tables schema

1. users table

Name Data type Length constrains
id integer PRI SERIAL KEY
firstname varchar 150 NOT NULL
lastname --------- 150 NOT NULL
username --------- 150 NOT NULL
email --------- 150 NOT NULL
password_digist --------- 250 NOT NULL
role --------- 150 NOT NULL

2. orders table

Name Data type Length constrains
id integer PRI SERIAL KEY
status varchar 64 NOT NULL
total float NOT NULL
user_id integer NOT NULL

3. products table

Name Data type Length constrains
id integer PRI SERIAL KEY
quantity integer NOT NULL
order_id integer NOT NULL
product_id integer NOT NULL

4. order_products table

Name Data type Length constrains
id integer PRI SERIAL KEY
name varchar 150 NOT NULL
stock integer NOT NULL
price float NOT NULL
user_id integer NOT NULL
category_id integer

5. review table

Name Data type Length constrains
id integer NOT NULL
service_rating float NOT NULL
feedback text NOT NULL

6. categories table

Name Data type Length constrains
id integer PRI SERIAL KEY
name varchar 150 NOT NULL
description varchar 255

7. migration table (auto created by db-migrate)

Name Data type Length constrains
id integer PRI SERIAL KEY
name varchar 255 NOT NULL
run_on timestamp 255 NOT NULL

ERD Diagram

Store front app ERD

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors