This repository contains the homeworks 2 through 7. They consist in creating a REST service with CRUD operations for a User and Group entity. This is an ExpressJS application and on subsequent homeworks several functionalities were added, like the use of a Postgres database, sequelize, logging and error handling, jwt authorization and some more.
- Clone repository.
- Install dependencies with
npm install
. - Create the
.env
file with the .env.example file. - Run
npm run dev
to start the appliction.
Postman or a similar program is required.
- Try creating your first User:
POST http://localhost:3000/api/users/
Body example:
{
"login": "[email protected]",
"password": "3e4e",
"age": "22"
}
Response example:
{
"id": "9198bbd8-a2fd-4ffb-a4ff-4fb02e2e8512",
"isDeleted": false,
"login": "[email protected]",
"password": "3e4e",
"age": 22
}
- Create your login token.
POST http://localhost:3000/api/users/login/
Body example:
{
"login": "[email protected]",
"password": "3e4e"
}
Response example:
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI5MTk4YmJkOC1hMmZkLTRmZmItYTRmZi00ZmIwMmUyZTg1MTIiLCJsb2dpbiI6IlJvYjQ1QGhvdG1haWwuY29tIiwiaWF0IjoxNjc5NTA1NzM4LCJleHAiOjE2Nzk1MDYzMzh9.R_qYPMKc4hPwXg_-R9EVDqnBEH6UMZaGwS_WHrNkdSk"
This token will be used as the value for x-access-token
in the headers for other methods authentication.
- Create a Group entity.
POST http://localhost:3000/api/groups/
Body example:
{
"name": "Javascript Group",
"permissions": ["WRITE", "READ"]
}
Permissions available: "READ" | "WRITE" | "DELETE" | "SHARE" | "UPLOAD_FILES"; The name of the group should be unique.
Response example:
{
"id": "4eb6c770-7304-4e6b-b201-604b1b965061",
"name": "Javascript Group",
"permissions": [
"WRITE",
"READ"
]
}
- Assign a User to a Group with their IDs.
POST http://localhost:3000/api/users/addUserToGroup/
Body example:
{
"userId": "9198bbd8-a2fd-4ffb-a4ff-4fb02e2e8512",
"groupId": "4eb6c770-7304-4e6b-b201-604b1b965061"
}
Response example:
{
"userId": "9198bbd8-a2fd-4ffb-a4ff-4fb02e2e8512",
"groupId": "4eb6c770-7304-4e6b-b201-604b1b965061",
"updatedAt": "2023-03-22T17:47:35.367Z",
"createdAt": "2023-03-22T17:47:35.367Z"
}
Returns a list of all users. Needs authorization token.
Returns one user with the corresponding id given in the URL. Needs authorization token. Example: GET http://localhost:3000/api/users/9198bbd8-a2fd-4ffb-a4ff-4fb02e2e8512
Returns a suggestion of users that begin with the substring. Needs authorization token. Example: http://localhost:3000/api/users/suggestions/Rob
Example response:
[
{
"id": "9198bbd8-a2fd-4ffb-a4ff-4fb02e2e8512",
"login": "[email protected]",
"password": "3e4e",
"age": 22,
"isDeleted": false
},
{
"id": "c5791836-2f9a-4d8f-96c2-32d8f78b27ef",
"login": "[email protected]",
"password": "3e4e",
"age": 22,
"isDeleted": false
}
]
Update an User that matches the id given in the URL. Needs authorization token.
Example: PATCH http://localhost:3000/api/users/9198bbd8-a2fd-4ffb-a4ff-4fb02e2e8512
Body example:
{
"login": "[email protected]",
"password": "345e6",
"age": "35"
}
Response example:
{
"id": "9198bbd8-a2fd-4ffb-a4ff-4fb02e2e8512",
"login": "[email protected]",
"password": "345e6",
"age": 35,
"isDeleted": false
}
Creates an User entity.
Creates a login jwt token to be used in the header as x-access-token
.
Adds certain User to a Group.
Example: http://localhost:3000/api/users/addUserToGroup/
Body example:
{
"userId": "9198bbd8-a2fd-4ffb-a4ff-4fb02e2e8512",
"groupId": "4eb6c770-7304-4e6b-b201-604b1b965061"
}
Response example:
{
"userId": "9198bbd8-a2fd-4ffb-a4ff-4fb02e2e8512",
"groupId": "4eb6c770-7304-4e6b-b201-604b1b965061",
"updatedAt": "2023-03-22T17:47:35.367Z",
"createdAt": "2023-03-22T17:47:35.367Z"
}
Deletes a user that matches the id given in the URL.
Returns a list of all groups. Needs authorization token.
Returns one group with the corresponding id given in the URL. Needs authorization token. Example: GET http://localhost:3000/api/users/9198bbd8-a2fd-4ffb-a4ff-4fb02e2e8512
Creates a group. Needs authorization token.
Updates a group that matches the id given in the URL.
Deletes a group that matches the id given in the URL. Example: DELETE http://localhost:3000/api/groups/d03946af-7ea9-41c5-bd99-6041252604fd