This is a REST API to register users and their favorite products.
This app uses Docker
to build and Postgres
Database image.
- docker
- postgres
- jest
- yarn v1.22.5
- node v15.14.0
In the root directory of the app, run this command:
yarn install
or just:
yarn
then run commands to start docker and configure database:
To start docker database images run:
yarn docker:up
and then run migrations:
yarn migrate
so dev environment is ready to start!
In the root project run:
yarn start
This command run server in localhost on the port 3000.
To run tests run the following commands:
yarn docker:test:up
this command updates docker instance with database for tests
and this command create test database:
yarn db:create:test
and to run migrations:
yarn migrate:test
and finally run:
yarn test
to start testing!
Also if you want to se app coverage, run:
yarn test:c
PS: this test generate a directory named coverage
on the root of the project. You can check a more detailed status of the code coverage on coverave/lcov-report/index.html
.
After that you can simply run
yarn start
oryarn test
, so you don't need to re-run config commands again.
GET /api/users
GET /api/users/<id>
POST /api/users
PUT /api/users/<id>
DELETE /api/users/<id>
GET /api/products
GET /api/products/<id>
POST /api/products
POST /api/auth/login
return all users, needs authentication. headers: { 'x-access-token': }
return user by id, needs authentication. headers: { 'x-access-token': }
create new user, don't need authentication. body: { name: required, email: required, password: required }
update user, have to be authentication. headers: { 'x-access-token': } body: { name: required, email: required, password: required }
delete user by id, have to be authentication. headers: { 'x-access-token': }
get all products, needs authentication. headers: { 'x-access-token': } query: { page: }
example: /api/products?page=1
get product by id, needs authentication. headers: { 'x-access-token': }
favorite product, needs authentication. headers: { 'x-access-token': } body: { userId: required, sku: required, }
return access token to include on private endpoints, don't need authentication. body: { email: required, password: required }
First of all create a user on route POST /api/users so you can authenticate using the email and password on POST /api/auth/login. After this, you should get product list on GET /api/products and get the id of this product, then on the POST /api/products you should pass your userId and the id of the product as a sku code to favorite this product.
It contains code architecture commonly used by the community, with some modifications:
src/app
: app module containing the API.controllers
: controller module containing all controllersmiddlewares
: middlewares module that includes auth middleware.models
: models module containing sequelize modules and init models.policies
: this module includes favorite product logic (business rules).repository
: module layer that comunicates with database.routes
: module to register all app routes.services
: module layer that contains API calls.utils
: module that contains helpers/validations, etc.
src/config
: here are centralized the API config, database, etc.src/database
: module that start database and store migrations.
This project uses AirBnb StyleGuide with some modifications in the rules:
...,
rules: {
'arrow-parens': ['error', 'as-needed'],
},
...