Members:
- Rodrigo González
- Matias Cuadros
- Leslie Reyes
The third milestone consists of the backend development of the project. For this:
- The different routes for the operation of the project are created.
- Run the REST API capable of managing data from the PostgreSQL database
- Implement user authentication and authorization with JWT.
- Supertest package is used to test different REST API routes.
- Frontend repository here
- To view the client application deployed, please visit Mundo Libros
- Server deployed in this Link
The server provides the following routes:
POST: /api/v1/register
: Allows registration of new usersPOST: /api/v1/login
: Receives a user's credentials and returns a token generated with JWT. The email address of the registered user is included in the token payloadGET: /api/v1/user/profile
: Allows to view user profile dataPUT: /api/v1/user/profile
: Allows to update a user's profile informationGET: /api/v1/addresses
: Returns all communes availableGET: /api/v1/user/:id/addresses
: Returns all address from an userPOST: /api/v1/user/:id/addresses
: Allows to add a new address from the userPUT: /api/v1/user/:id/addresses
: Allows to modify an address from the userGET: /api/v1/user/favorites
: Allows to view the user's favorite booksPOST: /api/v1/user/favorites
: Allows to add favorite books per userDELETE: /api/v1/user/favorites
: Allows to delete of favorite books per userGET: /api/v1/books
: Returns books data and allows sorting, limiting and pagination of information. Also filter by category and authorGET: /api/v1/books/:id
: Returns a specific book from the databaseGET: /api/v1/books/latest
: Returns the last 10 books added, it's for a carousel of booksGET: /api/v1/books/popular
: Returns the 10 most buyed books, it's for a carousel of booksPOST: /api/v1/books
: Allows you to add a new bookGET: /api/v1/authors
: Returns list of all authors belonging to the model booksPOST: /api/v1/authors
: Allows you to add a new authorGET: /api/v1/categories
: Returns list of all categories belonging to the model booksPOST: /api/v1/categories
: Allows you to add a new categoryPOST: /api/v1/user/purchase
: Allows to add the purchase and its detail to the databaseGET: /api/v1/user/carts
: Allows to view the user's purchase historyGET: /api/v1/user/carts/:cart_id
: Allows you to view the details of a purchase
Connects node.js to the PostgreSQL server. To specify which database to connect to, create an .env
file with the following structure, also available in the .env.example
file.
To create the database follow the instruction of the queryCreate.sql
file.
.env
PGUSER=postgres
PGHOST=localhost
PGPASSWORD=
PGDATABASE=bookstore
PGPORT=5432
JWTPASSWORD=
Using Thunder Client for VS Code or Postman as a client application
To register:
METHOD: POST ENDPOINT: localhost:3000/api/v1/register
BODY JSON
{
"name": "",
"lastname": "",
"email": "",
"username": "",
"birthday": "YYYY/MM/DD",
"password": ""
}
To login:
METHOD: POST
ENDPOINT: localhost:3000/api/v1/login
BODY JSON
{
"email": "",
"password": ""
}
To get user profile:
METHOD: GET
ENDPOINT: localhost:3000/api/v1/user/profile
AUTHORIZATION: Type Bearer Token
To update a profile:
METHOD: UPDATE
ENDPOINT: localhost:3000/api/v1/user/profile
AUTHORIZATION: Type Bearer Token
BODY JSON
{
"name": "",
"lastname": "",
"username": "",
"password": ""
}
To view the user's favorite books:
METHOD: GET
ENDPOINT:localhost:3000/api/v1/user/favorites
AUTHORIZATION: Type Bearer Token
To add book to favorites:
METHOD: POST
ENDPOINT:localhost:3000/api/v1/user/favorites
AUTHORIZATION: Type Bearer Token
BODY JSON
{
"book_id":
}
To remove a book from favorites:
METHOD: DELETE
ENDPOINT:localhost:3000/api/v1/user/favorites
AUTHORIZATION: Type Bearer Token
BODY JSON
{
"book_id":
}
To get all books:
METHOD: GET
ENDPOINT: localhost:3000/api/v1/books
To sort (asc/desc), limit and pagination:
- sort[title]
- sort[authors.name]
- sort[price]
- limit
- page
To filter by:
- category_id
- author_id
Examples:
localhost:3000/api/v1/books/?sort[title]=desc
localhost:3000/api/v1/books/?sort[authors.name]=desc
localhost:3000/api/v1/books/?limit=12&page=5
localhost:3000/api/v1/books/?author_id=7
localhost:3000/api/v1/books/?category_id=1&limit=5
To get a specific book:
METHOD: GET
ENDPOINT: localhost:3000/api/v1/books/:id
To get the last 10 books added:
METHOD: GET
ENDPOINT: localhost:3000/api/v1/books/latest
To get the top 10 most purchased books:
METHOD: GET
ENDPOINT: localhost:3000/api/v1/books/popular
To add a new book:
METHOD: POST
ENDPOINT: localhost:3000/api/v1/books/
AUTHORIZATION: Type Bearer Token
ROL: admin
BODY JSON
{
"title": "",
"image": "",
"description": "",
"price": ,
"stock": ,
"category_id": ,
"author_id":
}
Example:
{
"title": "El Aleph",
"image": "https://www.antartica.cl/media/catalog/product/9/7/9789875666481_1.png?quality=80&bg-color=255,255,255&fit=bounds&height=700&width=700&canvas=700:700&format=jpeg",
"description": "",
"price": 10990,
"stock": 25,
"category_id": 10,
"author_id": 32
}
To get the list of authors:
METHOD: GET
ENDPOINT: localhost:3000/api/v1/authors
To add an author:
METHOD: POST
ENDPOINT: localhost:3000/api/v1/authors
AUTHORIZATION: Type Bearer Token
ROL: admin
BODY JSON
{
"name": ""
}
To get the list of categories:
METHOD: GET
ENDPOINT: localhost:3000/api/v1/categories
To add a category:
METHOD: POST
ENDPOINT: localhost:3000/api/v1/categories
AUTHORIZATION: Type Bearer Token
ROL: admin
BODY JSON
{
"name": ""
}
To add a purchase and it's detail:
METHOD: POST
ENDPOINT: localhost:3000/api/v1/user/purchase
AUTHORIZATION: Type Bearer Token
BODY JSON
{
"address_id": ,
"cart_details": [ {"quantity": , "book_id": }, {"quantity": , "book_id": }]
}
To view purchase history:
METHOD: GET
ENDPOINT: localhost:3000/api/v1/user/carts
AUTHORIZATION: Type Bearer Token
To view the details of a purchase:
METHOD: GET
ENDPOINT: localhost:3000/api/v1/user/carts/:cart_id
AUTHORIZATION: Type Bearer Token
- Framework Express
- CORS : For providing a Connect/Express middleware that can be used to enable CORS with various options
- node-postgres: pg : Collection of node.js modules to interact with PostgreSQL database
- To safely create dynamic SQL queries: pg-format
- Environment variables dotenv
- JSON Web Token (JWT) is an open standard (RFC 7519) that defines a compact and self-contained way for securely transmitting information between parties as a JSON object
- bcrypt.js: To hash and salt passwords securely
- Morgan: HTTP request logger middleware for node.js
- To install dependencies run:
npm install
- devDependencies Nodemon for run server and automatically restarting the node application when file changes, in the terminal run:
npm run dev