This project aims to develop a Rest API for querying products, buyers and managing buyers' favorite products.
These instructions will provide you a copy of the project that can be run on your local machine for development and testing purposes. Consult deployment item for notes on how to deploy the project on a live system.
This package was created with go1.16 and all you need is the standard go library.
To make the application easier to run, the project has a .env
file to declare the environment variables
- HOST - Host address the application will use
- PORT - Port number where the application will listen to http requests
- DEBUG - Variable to define whether an API will produce logs for stdout
- SECRET - Secret word using to decode JWT
- STORAGE - Database address
This is what you need to install the application from the source code:
git clone https://github.com/paraizofelipe/luizalabs-challenge
To build the docker version you can use the Makefile
:
make dk-build
Until I finish this README there is not so much Unit tests written.
But I will try to coverage unless 80% of unit tests for this code as soon as possible.
You can run tests like this:
make test
To run this code locally for test purposes use:
Example:
STORAGE=postgres://luiza:labs@localhost:5432/luizalabs?sslmode=disable DEBUG=true HOST=0.0.0.0 SECRET=magalu PORT=3000 make start
This codebase is cloud-native by design so you can use lots of environments to make this run anywhere you want.
But to make this even easier to you the codebase also provides a Dockerfile.
Deploy with docker:
make dk-deploy
The API uses basic authentication with permissions based on read and write. To make testing easier, you can use the following tokens in the request header.
Note: These tokes only work with the environment variable SECRET
set to magalu.
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyZWFkIjp0cnVlLCJ3cml0ZSI6ZmFsc2UsImlzcyI6InRlc3QifQ.h1Dxo7xRffGyaF_Ee8qaWoCGrK-fJxqidr2lpZAydB0
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyZWFkIjpmYWxzZSwid3JpdGUiOnRydWUsImlzcyI6InRlc3QifQ.gm3iLFB66n11WaLrWwqD24a28cMLwcruj3TNB8FSrZ0
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyZWFkIjp0cnVlLCJ3cml0ZSI6dHJ1ZSwiaXNzIjoidGVzdCJ9.GYcqB4JeGR32cuD4H2lOgRDO8MS0YPJo3DA9gIZiygM
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyZWFkIjpmYWxzZSwid3JpdGUiOmZhbHNlLCJpc3MiOiJ0ZXN0In0.N_OC0cfd4HShueBZjGiNkUcIxWZgHWhCzd-CTmYvacU
- POST /api/product
Add a product
curl -i -X POST "http://localhost:3000/api/product/" \
-H "Authorization: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
-H "Content-Type: application/json" \
-d '{"title": "Smart TV xpto", "brand": "Paraizo", "price": 100.50, "review_score": 4.5, "image": "http://brand.com/image.png" }'
- GET /api/product/<id>
Fetch a product by ID
curl -i -X GET "http://localhost:3000/api/product/925fa490-7fa5-4fee-8035-3b691af02cb8" \
-H "Authorization: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
- GET /api/product/?page=<page number>
List a page of products
curl -i -X GET "http://localhost:3000/api/product/?page=10" \
-H "Authorization: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
- PATCH /api/product/<id>
Updates product's information
curl -i -X PATCH "http://localhost:3000/api/product/925fa490-7fa5-4fee-8035-3b691af02cb8" \
-H "Authorization: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
-H "Content-Type: application/json" \
-d '{"title": "Smart TV xpto", "brand": "Hell", "price": 100.50, "review_score": 4.5, "image": "http://brand.com/image.png" }'
- DELETE /api/product/<id>
Remove a product of database
curl -i -X DELETE "http://localhost:300/api/product/925fa490-7fa5-4fee-8035-3b691af02cb8" \
-H "Authorization: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
- POST /api/buyer
Create a buyer
curl -i -X POST "http://localhost:3000/api/buyer/" \
-H "Authorization: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
-H "Content-Type: application/json" \
-d '{"title": "Smart TV xpto", "brand": "Paraizo", "price": 100.50, "review_score": 4.5, "image": "http://brand.com/image.png" }'
Add favorite product to buyer
curl -i -X POST "http://localhost:3000/api/buyer/925fa490-7fa5-4fee-8035-3b691af02cb8/product/f4cacf02-4e71-42ad-9fd8-ace6d27a4c87" \
-H "Authorization: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
- GET /api/buyer/<id>
Fetch a buyer by id
curl -i -X GET "http://localhost:3000/api/buyer/925fa490-7fa5-4fee-8035-3b691af02cb8" \
-H "Authorization: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
- PATCH /api/buyer/<id>
Updates buyer's information
curl -i -X PATCH "http://localhost:3000/api/buyer/925fa490-7fa5-4fee-8035-3b691af02cb8" \
-H "Authorization: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
-H "Content-Type: application/json" \
-d '{"title": "Smart TV xpto", "brand": "Hell", "price": 100.50, "review_score": 4.5, "image": "http://brand.com/image.png" }'
- DELETE /api/buyer/<id>
Remove buyer of database
curl -i -X DELETE "http://localhost:3000/api/buyer/925fa490-7fa5-4fee-8035-3b691af02cb8" \
-H "Authorization: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"