GithubHelp home page GithubHelp logo

ricoputrap / e-commerce-api Goto Github PK

View Code? Open in Web Editor NEW

This project forked from leoantony72/e-commerce-api

0.0 0.0 0.0 546 KB

E-Commerce API Made With NodeJs,Typescript,PostgreSQL,Redis And Kafka

License: MIT License

JavaScript 6.88% TypeScript 82.26% CSS 6.48% Dockerfile 0.26% EJS 4.13%

e-commerce-api's Introduction

Introduction

E-commerce API made with typescript, express ,PostgreSQL,Redis & Kafka with all the basic features and more !!

Technologies

  • Nodejs
  • Expressjs
  • Typescript
  • PostgreSQL
  • Redis
  • Kafka

 

File Structure

GitHub Logo

Basic commands

git clone https://github.com/leoantony72/E-commerce-Api

 

Environment Variables

 

PORT = 4000
DB_USER = PGUSER
DB_PASSWORD = PGPASSWORD
DB_HOST = postgresdb
DB_PORT = 5432
DB_DATABASE = e_commerce
SESSION_SECRET = Session Secret
SESSION_MAXAGE = 3600000 * 60 * 10
GMAIL_USER = email // eg(test123)
GMAIL_PASS = Gmail password
STRIPE_PUBLIC_KEY = Stripe public Key
STRIPE_SECRET_KEY = Stripe Private Key

 

Docker build

docker-compose up // in the root dir

 

GitHub Logo

 

Setup Postgres,Redis & Kafka

 

docker exec -it postgresdb psql -U username -W e_commerce

⭕Copy and Paste DB Model

CREATE TABLE users(
    userid VARCHAR(11) NOT NULL PRIMARY KEY,
    username VARCHAR(255) NOT NULL UNIQUE,
    email VARCHAR(255) NOT NULL UNIQUE,
    user_role VARCHAR(25) DEFAULT 'USER' NOT NULL,
    passwordhash TEXT NOT NULL,
    registered_at TIMESTAMP NOT NULL,
    user_ip VARCHAR(50),
    active BOOLEAN DEFAULT FALSE NOT NULL,
    token TEXT UNIQUE,
    expiry TEXT
);

CREATE INDEX idx_userid ON users(userid);

CREATE TABLE user_address(
    id VARCHAR(11) NOT NULL PRIMARY KEY,
    userid VARCHAR(11) NOT NULL  REFERENCES users(userid) ON DELETE CASCADE ON UPDATE CASCADE,
    address_line1 VARCHAR(255),
    address_line2 VARCHAR(255),
    city VARCHAR(255),
    postal_code VARCHAR(45),
    country VARCHAR(100),
    telephone VARCHAR(50),
    mobile VARCHAR(50)
);

CREATE INDEX idx_user_add_id ON user_address(userid);

CREATE TABLE user_payment(
    id VARCHAR(11) NOT NULL PRIMARY KEY,
    userid VARCHAR(11) NOT NULL REFERENCES users(userid)
    ON DELETE CASCADE
    ON UPDATE CASCADE,
    payment_type VARCHAR(50) NOT NULL
);
CREATE INDEX idx_user_pay_id ON user_payment(userid);

/*product DB Design*/

CREATE TABLE products(
    pid VARCHAR(11) NOT NULL PRIMARY KEY,
    title VARCHAR(280) NOT NULL,
    image VARCHAR NOT NULL,
    created_at TIMESTAMP NOT NULL,
    summary VARCHAR(325) NOT NULL,
    price DECIMAL(12,2) NOT NULL
);
CREATE INDEX idx_pid ON products(pid);

CREATE TABLE inventory(
    id VARCHAR(11) NOT NULL PRIMARY KEY REFERENCES products(pid) ON DELETE CASCADE ON UPDATE CASCADE,
    quantity INT
);
CREATE INDEX idx_inv_id ON inventory(id);

CREATE TABLE product_category(
    id VARCHAR(11) NOT NULL PRIMARY KEY REFERENCES products(pid) ON DELETE CASCADE ON UPDATE CASCADE,
    name VARCHAR(40) NOT NULL
);
CREATE INDEX idx_cat_id ON product_category(id);

CREATE TABLE discount(
    id VARCHAR(11) NOT NULL PRIMARY KEY REFERENCES products(pid) ON DELETE CASCADE ON UPDATE CASCADE,
    coupon VARCHAR(100) NOT NULL,
    description VARCHAR(300),
    discount_percent DECIMAL(12,2),
    active BOOLEAN DEFAULT FALSE NOT NULL,
    created_at TIMESTAMP NOT NULL
);
CREATE INDEX idx_dis_id ON discount(id);

CREATE TABLE product_rating(
    pratingid VARCHAR(11) NOT NULL PRIMARY KEY,
    userid VARCHAR(11) NOT NULL REFERENCES users(userid)
    ON DELETE CASCADE
    ON UPDATE CASCADE,
    pid VARCHAR(11) NOT NULL REFERENCES products(pid)
    ON DELETE CASCADE
    ON UPDATE CASCADE,
    rating INT NOT NULL,
    date_created TIMESTAMP NOT NULL
);
CREATE INDEX idx_pr_id ON product_rating(pid);


CREATE TABLE ratings(
    rid VARCHAR(12) NOT NULL PRIMARY KEY,
    pid VARCHAR(11) REFERENCES products(pid),
    userid VARCHAR(11) REFERENCES users(userid),
    rating_number INT NOT NULL,
    comment VARCHAR(300) NOT NULL,
    submitted TIMESTAMP NOT NULL
);

CREATE TABLE tokens(
    userid VARCHAR(11) NOT NULL REFERENCES users(userid)
    ON DELETE CASCADE
    ON UPDATE CASCADE,
    token TEXT NOT NULL UNIQUE,
    expiry TEXT
);

CREATE TABLE cart(
    cart_id VARCHAR(13) NOT NULL PRIMARY KEY,
    userid VARCHAR(11) NOT NULL REFERENCES users(userid)
    ON DELETE CASCADE
    ON UPDATE CASCADE,
    pid VARCHAR(11) NOT NULL REFERENCES products(pid)
    ON DELETE CASCADE
    ON UPDATE CASCADE,
    quantity SMALLINT NOT NULL DEFAULT 1,
    date_created TIMESTAMP NOT NULL
);

CREATE INDEX cart_user_id ON cart(userid);

CREATE TABLE orders(
    order_id TEXT PRIMARY KEY NOT NULL,
    customer_id VARCHAR(11) NOT NULL REFERENCES users(userid),
    total DECIMAL(12,2) NOT NULL,
    billing_address_id VARCHAR(11) NOT NULL,
    order_status VARCHAR(100),
    payment_type VARCHAR(40) NOT NULL,
    date_created TIMESTAMP NOT NULL
);

CREATE INDEX orderidx_id ON orders(order_id);
CREATE INDEX order_cust_id ON orders(customer_id);

CREATE TABLE order_items(
    order_item_id VARCHAR(12) PRIMARY KEY,
    order_id TEXT NOT NULL REFERENCES orders(order_id)
    ON DELETE CASCADE
    ON UPDATE CASCADE,
    item_id VARCHAR(11) NOT NULL REFERENCES products(pid)
    ON DELETE CASCADE
    ON UPDATE CASCADE,
    item_quantity numeric NOT NULL
);

CREATE INDEX orderidx_item_id ON order_items(order_item_id);
CREATE INDEX ordItemidx_id ON order_items(item_id);

⭕Exit the PG cli

 

⭕Redis Setup

docker exec -it redis-e-commerce bash -c redis-cli

⭕Redis Bloom Filter

BF.RESERVE usernames 0.00001 10000

⭕Exit Redis Cli

 

⭕Kafka Setup

docker exec -it broker bash
kafka-topics --create --bootstrap-server localhost:9092 --replication-factor 1  --partitions 1  --topic orders

API-ROUTES

 

⭕ YOU CAN ACCESS THE SERVER IN localhost:4000 or localhost:80 (NGINX PROXY)

http://localhost

 

Routes Method Description
/api/store GET Access the Store
/api/auth/register POST Register a User
/api/auth/login POST Login A user
/api/auth/logout POST Logout A User
/api/auth/forgotpassword POST Req For Password Reset
/api/auth/reset-password/ POST Resets The Password
/api/verify GET Verify The Email
/api/admin/product POST Insert A Product
/api/admin/product/:id PUT Update Product
/api/admin/product/:id DELETE Delete A Product
/api/admin/stock/:id PUT Update Stock
/api/admin/discount/:pid GET Get Discount of a Product
/api/admin/add_discount/:id POST Add Discount to a product
/api/admin/activate/:id GET Activate Discount
/api/manager/orders GET Gets Order
/api/manager/order/:orderid GET Get Individual Order Detailes
/api/shipper/updateorder/:oid POST Sents Confirm Order Request
/api/order/confirmdelivery POST User Confirms Delivery
/api/products GET Get Products
/api/products/:pid GET Get Individual product
/api/checkusername POST Checks If Username is Taken
/api/ratings/:pid GET Get Rating
/api/ratings/:pid POST Add Rating To Product
/api/ratings/:pid PUT Update Rating
/api/ratings/:pid DELETE Delete Rating
/api/cart GET Get Cart Items
/api/addItem/:pid POST Add Item To Cart
/api/removeItem/:pid DELETE DEL Item In Cart
/api/purchase POST Purachse Item
/api/userAddress POST Add User Address
/api/userAddress DELETE DEL User Address
/api/userAddress PUT Update User Address

 

Lot Of The Endpoints are GET or POST Where They Just Have To Provided With /:id or /:oid etc.. ,So If You Provide It With The Required Params It Wil Work ...

Request Payload

 

/api/auth/register

{
    "username": "USERNAME",
    "email":"EMAIL",
    "password":"PASS",
    "confirmPassword":"PASS"
}

 

/api/auth/login

{
    "username": "USERNAME",
    "password":"PASS",
}

 

/api/auth/logout

Login In

 

/api/auth/forgotpassword

{
    "email":"EMAIL"
}

 

/api/auth/reset-password/?rec=

{
    "password": "PASS",
    "confirmPassword": "PASS"
}

 

/api/verify/?vif=

Provide the verification token

 

/api/admin/product

 

METHOD: POST

 

❗ FORM DATA

GitHub Logo

👉The tool used here is Postman, Used to test API endpoints...
 

❗The keys in the Image are the data we should provide to make a POST req, Missing any of the keys will result in an error

 

/api/admin/product/:id

 

METHOD: PUT

 

❗ FORM DATA

GitHub Logo 👉 The tool used here is Postman, Used to test API endpoints...
 

❗ The keys in the image are same as that of POST req but make sure to change POST to PUT req.

❗ The keys in the Image are the data we should provide to make a POST req, Missing any of the keys will result in an error

 

/api/admin/product/:id

 

METHOD: DELETE

Administrative Privileges

 

/api/admin/stock/:id

 

METHOD: PUT

{
    "stock":"140"
}

 

/api/admin/add_discount/:id

 

METHOD: POST

{
    "discount": "20.00",
    "description": "bruh"
}

 

/api/admin/activate/:id

 

METHOD: PUT

Activates Discount if you have Admin role

 

/api/manager/orders?

 

METHOD: GET

http://localhost:80/api/manager/orders?status=succeeded

http://localhost:80/api/manager/orders?status=fulfilled

 

/api/shipper/updateorder/:oid

 

METHOD: GET

Provide Order ID

 

/api/order/confirmdelivery?uid=&token=&oid=

 

METHOD: GET

Provide userid,token&orderid

 

/api/checkusername

 

METHOD: POST

{
    "username": "USERNAME"
}

 

/api/ratings/:pid

 

METHOD: POST

{
    "rating": 4,
    "comment": "Awesome Product"
}

 

/api/ratings/:pid

 

METHOD: PUT

{
    "rating": 4,
    "comment": "Awesome Product"
}

 

/api/purchase

 

METHOD: POST

{
    stripeTokenId: token.id,
    items: [items],
}

 

/api/userAddress

 

METHOD: POST

{
    "address_line1": "ADDR",
    "address_line2":"ADDR2",
    "city":"test",
    "postalCode":"1245678",
    "country":"test",
    "mobile":"test"
}

 

/api/userAddress

 

METHOD: PUT

{
    "address_line1": "ADDR",
    "address_line2":"ADDR2",
    "city":"test",
    "postalCode":"1245678",
    "country":"test",
    "mobile":"test"
}

 

/api/userAddress

 

METHOD: DELETE

Deletes Automatically when requested

 

⭕ NOTE

You should also setup Order-service (Documentation provided 🔥)
https://github.com/leoantony72/order-service

e-commerce-api's People

Contributors

leoantony72 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.