/Express-backend-project

Primary LanguageTypeScriptOtherNOASSERTION

Setup

  • Clone https://github.com/MeshalAl/Express-backend-project.git
  • npm install to install dependencies.
  • Create a local PSQL database.
  • Create a .env file, edit the brackets <> with your values:
DB_USER = <your db user>
DB_PASSWORD = <your db password>

DB_HOST = <localhost | your db host>
DB_PORT = <5432 | your db port>

DB_PRODUCTION = production_db
DB_TEST = test_db
DB_DEV = dev_db

ENV = 'dev'

SALT = <Int>
PEPPER = <String>
TOKEN_SECRET = <String>

Database setup

CREATE USER <your_db_user> WITH PASSWORD <'your password'>;

Create Databases:
CREATE DATABASE test_db;
CREATE DATABASE dev_db;

Granting access to users:

GRANT ALL PRIVILEGES ON DATABASE test_db TO <your_db_user>;
GRANT ALL PRIVILEGES ON DATABASE dev_db TO <your_db_user>;

Ports used: Database: 5432
Backend: 3000

Commands:

start nodemon:
npm run start

start jasmine tests & db-migrates on windows:

npm run test-windows for windows.
npm run test-linux for linux.

Endpoints:

Users:

Create:
post on localhost:3000/api/users Body format:

{
    "firstname": "value",
    "lastname": "value",
    "password": "value"
}

Returns

{
    "user_id": 1,
    "firstname": "value",
    "lastname": "value",
    "password": "hashed_password"
}

Authenticate:
post on localhost:3000/api/users/auth

Body format:

{
    "user_id": 1,
    "password": "value"
}

Returns: "TOKEN"

User index:
get on localhost:3000/api/users
Autherization header: "bearer TOKEN"
Returns:

[
    {
        "user_id": 1,
        "firstname": "User firtname",
        "lastname": "User lastname"
    },
    ...
]

User by id:
get on localhost:3000/api/users/[user id: int]
Autherization header: "bearer TOKEN"
Returns:

{
    "user_id": 1,
    "firstname": "User firtname",
    "lastname": "User lastname"
}

Products:

Create:
post on localhost:3000/api/products
Autherization header: "bearer TOKEN"

Body format:

{
    "product_name": "product name",
    "price": 29.59,
    "category": "category name: optional"
}

Returns:

{
    "product_id": 1,
    "product_name": "product name",
    "price": 29.59,
    "category": "category name: optional"
}

Index of products:
get on localhost:3000/api/products

Returns:

[
    {
      "product_id": 1,
      "product_name": "product name",
      "price": 29.59,
      "category": "category name: optional"
    },
    ...
]

Product by id:
get on localhost:3000/api/products/[product_id]

Returns:

{
  "product_id": 1,
  "product_name": "product name",
  "price": 29.59,
  "category": "category name: optional"
}

Orders:

Create Order:
post on localhost:3000/api/orders
Autherization header: "bearer TOKEN"
Body format:

{
    "Products": [ 
        { "product_id": 1, "quantity": 4},
        ...
    ]
}

Returns:

[
    {
        "order_id": 1,
        "user_id": 1,
        "product_name": "test product",
        "quantity": 4,
        "price": 29.59,
        "status": "active",
        "order_date": "2023-01-02T19:33:32.388Z"
    },
    ...
]

index of current user's order:
get on localhost:3000/api/orders
Autherization header: "bearer TOKEN"

Returns:

[
    {
        "order_id": 1,
        "user_id": 1,
        "product_name": "test product",
        "quantity": 4,
        "price": 29.59,
        "status": "active",
        "order_date": "2023-01-02T19:33:32.388Z"
    },
    ...
]

get current user's order by id:
get on localhost:3000/api/orders/[order_id]
Autherization header: "bearer TOKEN"

Returns:

[
    {
        "order_id": 1,
        "user_id": 1,
        "product_name": "test product",
        "quantity": 4,
        "price": 29.59,
        "status": "active",
        "order_date": "2023-01-02T19:33:32.388Z"
    },
    ...
]

Complete user's order:
post on localhost:3000/api/orders/complete/
Autherization header: "bearer TOKEN"

Body format:

{
  "order_id": 1,
  "all" [Optional]: true | false
}

Returns:

[
    {
        "order_id": 1,
        "user_id": "1",
        "status": "completed",
        "order_date": "2023-01-02T19:33:32.388Z"
    },
    ...
]

Order history:
get on localhost:3000/api/orders/history
Autherization header: "bearer TOKEN"

Returns:

[
    {
        "order_id": 1,
        "user_id": 1,
        "product_name": "test product",
        "quantity": 4,
        "price": 29.59,
        "status": "completed",
        "order_date": "2023-01-06T19:33:32.388Z"
    },
    ...
]