Skip to content

lilithrv/diseno-api-rest

Repository files navigation

Desafio Latam: REST API Design

A jewelry store needs to develop a REST API of a client application.

In this challenge, a REST API is created that allows:

  1. Limit resources
  2. Filter resources by fields
  3. Paginate
  4. HATEOAS data structure

The server provides the following routes:

  • GET: api/joyas : Returns data with HATEOAS data structure and allows sorting, limiting and pagination of information.
  • GET: api/joyas/:id : Returns a specific jewel from the database
  • GET: api/joyas/filtros : Filter the information by price_max, price_min, category and metal

Environment variables

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 query.sql file.

.env

PGUSER=postgres 
PGHOST=localhost
PGPASSWORD=
PGDATABASE=joyas
PGPORT=5432

Using Thunder Client for VS Code or Postman as a client application


To get the complete inventory:

METHOD: GET 
ENDPOINT: localhost:3000/api/joyas/

To sort (asc/desc), limit and pagination:

  • sort[id]
  • sort[nombre]
  • sort[categoria]
  • sort[metal]
  • sort[stock]
  • sort[precio]
  • limit
  • page
METHOD: GET 
ENDPOINT: localhost:3000/api/joyas/

Example:

localhost:3000/api/joyas/?limit=5&sort[metal]=asc&page=2
localhost:3000/api/joyas/?sort[id]=desc
localhost:3000/api/joyas/?limit=6
localhost:3000/api/joyas/?limit=6&page=4

To get a specific jewel:
METHOD: GET
ENDPOINT: localhost:3000/api/joyas/{id}

To filter by:

  • precio_min
  • precio_max
  • categoria
  • metal
METHOD: GET
ENDPOINT: localhost:3000/api/joyas/filtros

Examples:

localhost:3000/api/joyas/filtros?precio_max=20000
localhost:3000/api/joyas/filtros?precio_max=20000&metal=plata
localhost:3000/api/joyas/filtros?precio_max=20000&metal=plata&categoria=collar

Database

Backend

Dependencies

  • 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
  • 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