Eyuelb / Car-parking-api

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

🚗 CutuCar API

Welcome to my project! 👋

I created this project as part of a web course assignment. The main goal of the project is to simulate requests to a REST API in a parking management application. This includes handling authentication, authorization, and CRUD operations for users, cars, clients, tickets, parking spaces, and parking space types.

Through this project, I have honed my skills in programming and web development.

Thank you for taking the time to check out my project!

divBar

🔖 Table of Contents

divBar

🚀 Mandatory requirements

  • Readability
  • README documentation
  • JWT
  • Authentication route
  • Protected routes (token bearer with 1h deadline)

divBar

🌟 Business Logic

Users

  • Username must be unique in the base.

Cars

  • The car plate must be unique in the base.
  • The car must have a client.

Ticket

  • The ticket needs a parking space.
  • The ticket needs a parking space type.
  • The ticket needs a car.

Parking space

  • The same parking space cannot be used in the same period of time.

Parking space type

  • The parking space type needs a description.
  • The parking space type needs an hourly cost.

Client

  • The client needs a unique CPF (Brazilian ID number) in the base.

divBar

💻 Usage

Local hosting

Pre-requisites

You will need to have Node.JS installed on your machine, as well as a PostgreSQL Server.

Step by step

  1. Check if you have the listed pre-requisites.
  2. Clone the repository to your local machine with the command:
$ git clone https://github.com/mateusseiboth/node-postgres.git
  1. Install project dependencies with the command:
$ npm install
  1. Edit a .env file at the project root.
  2. Start the server with the command:
$ npm start
  1. The server will be running at localhost:port.

divBar

🧰 Technologies

My Skills

Main Stack

Security and authentication

  • JsonWebToken: Library for generating JSON web tokens for authentication and authorization.
  • Bcrypt: Hashing library for password encryption.

Utilitary and Middleware

  • Dotenv: Library to load env variables from .env file for easier environment configuration.
  • Nodemon: Utility that monitors code changes and auto-restarts the server.

divBar

🧩 Project Architecture

The src/api folder contains all the application's source code and is organized as follows:

  • 📂 models: Contains the data models used by the application, such as Car, User and Ticket.
  • 📂 controllers: Contains the controller logic of the application, organized by features or pages.
  • 📂 middlewares: Contains middleware functions used by the application, such as authentication and rate limit.
  • 📂 routes: Contains the API routes.
  • 📂 utils: Contains utility functions used by the application, such as jwt token creation.
  • 📂 database: Contains the creation of the database connection pool
  • 📄 app.js: Main file that configures the server and check connection with database.
  • 📄 server.js: Instantiates the server and listens for HTTP requests.

divBar

🛣 Endpoints

  • Main Route: /api/v1

Authentication Routes

Method Endpoint Protected Description
POST /user/auth false Authenticate a user

Note This route is for generating or renewing the authentication token.

Example

Generate Token (POST)

Request

{
  "email": "joazinho@email.com",
  "password": "123456"
}

Response

{
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjY0MzMxMjk2MDM5ZDk2MDg1NmQxZDZkYSIsImlhdCI6MTY4MTA2ODkwMiwiZXhwIjoxNjgxMTEyMTAyfQ.D5zEzXoHSvQZNKdW48Ip_HVq0jlVo9Wxuf_Rf4AhFdg"
}

User Routes

Method Endpoint Protected Description
POST /user/new false Register a new user
GET /user/me true Return token details

Examples

Create (POST)

Request

{
  "username": "mateus",
  "password": "123456"
}

Response

{
  "message": "User created successfully"
}

Me (GET)

Response

{
  "user": {
      "id": 3,
      "iat": 1681500982,
      "exp": 1681504582
  }
}

Car Routes

Method Endpoint Protected Description
GET /car/list true List all cars
POST /car/new true Register a new car
DELETE /car/:id true Remove a car by ID
PUT /car/update true Update a car by ID

Examples

Create (POST)

Request

{
  "placa": "HRT7J76",
  "cliente_id": 2
}

Response

{
  "message": "Car criado"
}

Read (GET)

Response

[
  {
      "id": 1,
      "placa": "FRT0876",
      "cliente_id": 1,
      "cliente_nome": "Robson"
  },
  {
      "id": 2,
      "placa": "FRT0806",
      "cliente_id": 2,
      "cliente_nome": "JuninhoBen10"
  },
  {
      "id": 3,
      "placa": "FTR0934",
      "cliente_id": 1,
      "cliente_nome": "Robson"
  },
  {
      "id": 5,
      "placa": "HRO7J76",
      "cliente_id": 1,
      "cliente_nome": "Robson"
  },
  {
      "id": 6,
      "placa": "HRT7J76",
      "cliente_id": 2,
      "cliente_nome": "JuninhoBen10"
  },
  {
      "id": 8,
      "placa": "HRy7J76",
      "cliente_id": 2,
      "cliente_nome": "JuninhoBen10"
  },
  {
      "id": null,
      "placa": null,
      "cliente_id": null,
      "cliente_nome": "Sem Cliente"
  }
]

Update (PUT)

Request (PUT car)

{
  "id": "5",
  "placa": "HRO7J76",
  "cliente_id": 1
}

Response (PUT car)

{
  "message": "ok"
}

Delete (DELETE)

Response (DELETE car)

  {
    "message": "ok"
  }

Ticket Routes

Method Endpoint Protected Description
POST /ticket/new true Register a new ticket
GET /ticket/listAll true List all tickets
GET /ticket/listAtivo true List all active tickets
PUT /ticket/:id true Close the ticket with the sent ID

Examples

Create (POST)

Request

{
  "carro": "1",
  "vaga": "2",
  "tipo": "1"
}

Response

{
  "message": "Ticket criado"
}

Read (GET)

Response

[
  {
      "hora_entrada": "11:09:53",
      "nome_cliente": "Robson",
      "nome_placa": "FRT0876",
      "tipo_vaga": "Preço por hora Carro",
      "preco_tipo": "3.79",
      "id_vaga": 2,
      "ticket_id": 15,
      "hora_saida": "Não recuperado",
      "custo": "0.0"
  },
  {
      "hora_entrada": "13:59:10",
      "nome_cliente": "Robson",
      "nome_placa": "FTR0934",
      "tipo_vaga": "Preço por hora Moto",
      "preco_tipo": "3.80",
      "id_vaga": 10,
      "ticket_id": 16,
      "hora_saida": "Não recuperado",
      "custo": "0.0"
  },
  {
      "hora_entrada": "15:44:14",
      "nome_cliente": "Robson",
      "nome_placa": "FRT0876",
      "tipo_vaga": "Preço por hora Carro",
      "preco_tipo": "3.79",
      "id_vaga": 2,
      "ticket_id": 17,
      "hora_saida": "Não recuperado",
      "custo": "0.0"
  }
]

Close ticket (PUT)

Response

{
  "message": "ok"
}

Type Routes

Method Endpoint Protected Description
POST /tipo/new true Register a new type
DELETE /tipo/:id true Delete type by ID
GET /tipo/list true List all types
PUT /tipo/update true Update type by ID

Examples

Create (POST)

Request

{
  "preco": "12.89",
  "descr": "Hora carro"
}

Response

{
  "message": "Ok"
}

Update (PUT)

Request

{
  "id": "5",
  "preco": "14.90",
  "descr": "Via API"
}

Response

{
  "message": "Ok"
}

Delete (DELETE)

Response

{
  "message": "Ok"
}

List (GET)

Response

[
  {
      "id": 1,
      "preco": "3.79",
      "descr": "Preço por hora Carro"
  },
  {
      "id": 3,
      "preco": "3.80",
      "descr": "Preço por hora Moto"
  },
  {
      "id": 4,
      "preco": "444.00",
      "descr": "Teste"
  },
  {
      "id": 6,
      "preco": "12.87",
      "descr": "Teste"
  }
]

Parking space Routes

Method Endpoint Protected Description
GET /vaga/new true Register a new Parking space
DELETE /vaga/:id true Delete Parking space by ID
GET /vaga/list true List all Parking space

Examples

Delete (DELETE)

Response

{
  "message": "Ok"
}

List (GET)

Response

[
  {
      "id": 3,
      "estado": true
  },
  {
      "id": 4,
      "estado": true
  },
  {
      "id": 6,
      "estado": true
  }
]

Create (GET)

Response

  {
    "message": "Ok"
  }

Client Routes

Method Endpoint Protected Description
POST /client/new true Register a new client
DELETE /client/:id true Delete client by ID
GET /client/list true List all clients
PUT /client/update true Update client by ID

Examples

Create (POST)

Request

{
  "nome":"Junior Ben10 mil",
  "cpf":"09889076545",
  "telefone": "999999999"
}

Response

{
  "message": "Ok"
}

Update (PUT)

Request

{
  "id": "6",
  "nome":"Junior Ben10 mil milhoes",
  "cpf":"09889076545",
  "telefone": "999999999"
}

Response

{
  "message": "Ok"
}

Delete (DELETE)

Response

{
  "message": "Ok"
}

List (GET)

Response

[
  {
      "id": 1,
      "nome": "Robson",
      "cpf": "03045623412",
      "telefone": "67920002212"
  },
  {
      "id": 2,
      "nome": "JuninhoBen10",
      "cpf": "03045623432",
      "telefone": "67920002212"
  },
  {
      "id": 0,
      "nome": "Sem Cliente",
      "cpf": "00000000000",
      "telefone": "00000000"
  },
  {
      "id": 8,
      "nome": "Junior Ben10 mil",
      "cpf": "09889076545",
      "telefone": "999999999"
  }
]

divBar

About

License:MIT License


Languages

Language:JavaScript 100.0%