Giter VIP home page Giter VIP logo

creating-an-api-with-postgresql-and-express-project's Introduction

Storefront Backend Project

Getting Started

  1. Install dependencies:
npm install
  1. Create user Postgres user:
su postgres
psql postgres
CREATE USER full_stack_user WITH PASSWORD 'password123';
  1. We create the database:
CREATE DATABASE products_ecommerce;
CREATE DATABASE products_ecommerce_test;
  1. You have to fill the file: .env, there is an example in the .env.example:
ENV=test

POSTGRES_HOST=127.0.0.1

POSTGRES_DB=products_ecommerce
POSTGRES_DB_TEST=products_ecommerce_test

POSTGRES_USER=full_stack_user
POSTGRES_PASSWORD=password123

BCRYPT_PASSWORD=
SALT_ROUNDS=
TOKEN_SECRET=
  1. The command to activate the project
npm run start
  1. The command for testing
npm run test

Database logic:

I made a table for the products, where null values are not accepted:

CREATE TABLE products (
     id SERIAL PRIMARY KEY,
     name VARCHAR NOT NULL,
     price integer NOT NULL
);

I made a table for users, where null values are not accepted:

CREATE TABLE users (
     id SERIAL PRIMARY KEY,
     firstName VARCHAR NOT NULL,
     lastName VARCHAR NOT NULL,
     password VARCHAR NOT NULL
);

I made a table for orders, where null values are not accepted and we refer to the user. In the case of products, there is a Middleware that is in charge of checking that the product exists.

CREATE TABLE orders (
     id SERIAL PRIMARY KEY,
     id_product integer [] NOT NULL,
     user_id bigint REFERENCES users (id) NOT NULL,
     quantity integer [] NOT NULL,
     status_order boolean NOT NULL
);

Routes:

Products:

  • The route to see all your products:

`` GET http:/localhost:8080/products`

  • The path to view a particular product:

``GET http:/localhost:8080/products/:id`

  • The path to create a product, but it has a middleware that verifies the JWT:

``POST http:/localhost:8080/products/:id`

How to send body Json information:

{
  "name": "test 3",
  "price": 100
}
  • The path to delete a product, but it has a middleware that checks the JWT:

``DELETE http:/localhost:8080/products/:id`

Users:

All routes have a middleware that verifies the JWT:

  • The path to see all users:
GET http:/localhost:8080/users
  • The path to view a particular user:
GET http:/localhost:8080/users/:id
  • The path to create a user,
POST http:/localhost:8080/users/:id

How to send body Json information:

{
  "firstName": "name-2",
  "lastName": "last-2",
  "password": "password123"
}
  • The path to authenticate
POST http:/localhost:8080/users/authenticate
  • The path to modify a user,
POST http:/localhost:8080/users/:id

Orders:

All routes have a middleware that verifies the JWT:

  • The path to see all orders:
GET http:/localhost:8080/orders
  • The path to view a particular order:
GET http:/localhost:8080/orders/:id
  • The path to create an order,
POST http:/localhost:8080/orders/:id

How to send body Json information:

{
  "user_id": 1,
  "status_order": false
}
  • The path to see all ordersProducts:
GET http:/localhost:8080/orders/products
  • The path to view a particular ordersProducts:
GET http:/localhost:8080/orders/products/:id
  • The path to create an ordersProducts,
POST http:/localhost:8080/orders/products/:id

How to send body Json information:

POST http:/localhost:8080/orders/products/1
{
  "productId": 1,
  "quantity": 10
}

creating-an-api-with-postgresql-and-express-project's People

Contributors

diego-luna avatar

Watchers

 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.