Created a store of medieval items, such as those swords made to order for a specific person, in the format of an API, using Typescript and Sequelize.
Developed the application's Service and Controllers layers in its code, using JWT to authenticate some routes, in addition to tests to ensure their correct operation. The application will have endpoints that will support operations for creating, reading and updating information.
🐳 Docker
-
git clone [email protected]:alinesresende/medieval-store-api.git -
npm install
Run the
app-trybesmithanddbservices with the commanddocker-compose up -d --build.
-
These services will start up a container named
trybesmith_apiand another namedtrybesmith_db.Run the
npm run db:resetcommand (this command will work only after creating the requested types in the requirement) to create the database, the tables that will be used and populate them.Use the command
docker exec -it trybesmith_api bashto enter the container. -
To view the nodemon logs in your terminal use the following commands:
docker ps: to view the active containers and get theCONTAINER ID;docker logs -f <container_id>: to view your server logs with nodemon;
1 - Created an endpoint for the registration of products and tests that cover the functionalities of this endpoint
- The endpoint is accessible from the path (
/products); - The result returned to successfully register the product should be as shown below, with a status http
201:
{
"name": "Martelo de Thor",
"price": "30 peças de ouro",
"orderId": 4
}2 - Created an endpoint for listing products and tests that cover the functionalities of this endpoint
-
The endpoint is accessible from the path (
/products); -
The result returned to successfully register the product should be as shown below, with a status http
200:[ { "id": 1, "name": "Pedra Filosofal", "price": "20 gold", "orderId": null }, { "id": 2, "name": "Lança do Destino", "price": "100 diamond", "orderId": 1 } ]
3 - Created an endpoint to list all requests and tests that cover the functionalities of this endpoint
-
The endpoint is accessible from the path (
/orders). -
The result returned to successfully register the product should be as shown below, with a status http
200:[ { "id": 1, "userId": 2, "productIds": [1, 2] }, { "id": 2, "userId": 1, "productIds": [3, 4] } ]
4 - Created an endpoint for the login of users and tests that cover the functionalities of this endpoint
-
The endpoint is accessible from the path (
/login). -
The route receives the
usernameandpasswordfields, and these fields are validated against the database. -
A
JWTtoken is generated and returned if login succeeds. Your payload must contain id and username. -
The endpoint receives the following structure:
{
"username": "string",
"password": "string"
}🧪 Middlewares:
👉 If the login does not have the "username" field, the result returned should be an http_status_
400:
{ "message": "\"username\" and \"password\" are required" }- If the login does not have the "password" field, the result returned should be a status http
400:
{ "message": "\"username\" and \"password\" are required" }-If the login has a username that does not exist in the database it will be considered invalid and the result returned should be a status http 401:
{ "message": "Username or password invalid" }- If the login has a password that does not match the password saved in the database, it is considered invalid and the result returned should be a status http
401:
{ "message": "Username or password invalid" }- If the login was successful, the result should be a status http
200and should return a token in the format below (the token does not need to be exactly like this):
{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJIYWdhciIsImlhdCI6MTY4Njc1NDc1Nn0.jqAuJkcLp0RuvrOd4xKxtj_lm3Z3-73gQQ9IVmwE5gA"
}- Developed the validations related to the creation of the endpoint of challenge 1.
🧪 Middlewares:
👉 For name
- If the "name" field is not informed, the result returned should be a status http
400:
{ "message": "\"name\" is required" }- If the field "name" is not of type
string, the result returned should be a status http422:
{ "message": "\"name\" must be a string" }- If the "name" field is not a string longer than 2 characters, the result returned should be a status http
422:
{ "message": "\"name\" length must be at least 3 characters long" }👉 For price
- If the "price" field is not informed, the result returned should be a status http
400and
{ "message": "\"price\" is required" }- If the "price" field is not of type
string, the result returned should be a status http422:
{ "message": "\"price\" must be a string" }- If the "price" field is not a string of more than 2 characters, the result returned should be a status http
422:
{ "message": "\"price\" length must be at least 3 characters long" }