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!
- Readability
- README documentation
- JWT
- Authentication route
- Protected routes (token bearer with 1h deadline)
- Username must be unique in the base.
- The car plate must be unique in the base.
- The car must have a client.
- The ticket needs a parking space.
- The ticket needs a parking space type.
- The ticket needs a car.
- The same parking space cannot be used in the same period of time.
- The parking space type needs a description.
- The parking space type needs an hourly cost.
- The client needs a unique CPF (Brazilian ID number) in the base.
- 📜 Node.JS
- 🍃 PostgreSQL
You will need to have Node.JS installed on your machine, as well as a PostgreSQL Server.
- Check if you have the listed pre-requisites.
- Clone the repository to your local machine with the command:
$ git clone https://github.com/mateusseiboth/node-postgres.git
- Install project dependencies with the command:
$ npm install
- Edit a
.env
file at the project root. - Start the server with the command:
$ npm start
- The server will be running at
localhost:port
.
- Node.JS: JavaScript development platform.
- Express: Web application framework.
- PostgreSQL: Object-relational database.
- JsonWebToken: Library for generating JSON web tokens for authentication and authorization.
- Bcrypt: Hashing library for password encryption.
- Dotenv: Library to load env variables from .env file for easier environment configuration.
- Nodemon: Utility that monitors code changes and auto-restarts the server.
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.
- Main Route:
/api/v1
Method | Endpoint | Protected | Description |
---|---|---|---|
POST |
/user/auth | false | Authenticate a user |
Note This route is for generating or renewing the authentication token.
{
"email": "joazinho@email.com",
"password": "123456"
}
{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjY0MzMxMjk2MDM5ZDk2MDg1NmQxZDZkYSIsImlhdCI6MTY4MTA2ODkwMiwiZXhwIjoxNjgxMTEyMTAyfQ.D5zEzXoHSvQZNKdW48Ip_HVq0jlVo9Wxuf_Rf4AhFdg"
}
Method | Endpoint | Protected | Description |
---|---|---|---|
POST |
/user/new | false | Register a new user |
GET |
/user/me | true | Return token details |
{
"username": "mateus",
"password": "123456"
}
{
"message": "User created successfully"
}
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 |
[
{
"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"
}
]
{
"id": "5",
"placa": "HRO7J76",
"cliente_id": 1
}
{
"message": "ok"
}
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 |
{
"carro": "1",
"vaga": "2",
"tipo": "1"
}
{
"message": "Ticket criado"
}
[
{
"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"
}
]
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 |
{
"id": "5",
"preco": "14.90",
"descr": "Via API"
}
{
"message": "Ok"
}
[
{
"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"
}
]
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 |
[
{
"id": 3,
"estado": true
},
{
"id": 4,
"estado": true
},
{
"id": 6,
"estado": true
}
]
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 |
{
"nome":"Junior Ben10 mil",
"cpf":"09889076545",
"telefone": "999999999"
}
{
"message": "Ok"
}
{
"id": "6",
"nome":"Junior Ben10 mil milhoes",
"cpf":"09889076545",
"telefone": "999999999"
}
{
"message": "Ok"
}
[
{
"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"
}
]