Skip to content

DiegoVictor/central-de-herois-api

Repository files navigation

[API] Central de Heróis

AppVeyor mongoose eslint airbnb-style jest coverage MIT License PRs Welcome
Run in Insomnia}

Allow users to register and log in, to create, update and delete heroes, list monsters and change a combat status, it also receive monsters occurrences and try to allocate heroes to fight with the monster. The app uses JWT for authentication.

Table of Contents

Installing

Easy peasy lemon squeezy:

$ yarn

Or:

$ npm install

Was installed and configured the eslint and prettier to keep the code clean and patterned.

Configuring

The application use just one database: MongoDB. For the fastest setup is recommended to use docker-compose, you just need to up all services:

$ docker-compose up -d

MongoDB

Store heroes, monsters and the users utilized by the application. If for any reason you would like to create a MongoDB container instead of use docker-compose, you can do it by running the following command:

$ docker run --name central-de-herois-mongo -d -p 27017:27017 -d mongo

.env

In this file you may configure your MongoDB's database connection, JWT settings and app's port. Rename the .env.example in the root directory to .env then just update with your settings.

key description default
APP_PORT Port number where the app will run. 3333
JWT_SECRET A alphanumeric random string. Used to create signed tokens. -
JWT_EXPIRATION_TIME How long time will be the token valid. See jsonwebtoken repo for more information. 7d
MONGO_URL MongoDB's server url. mongodb:https://mongo:27017/central-de-herois-mongo
MONSTER_WEBSOCKET Socket's server url http:https://localhost:3000/central-de-herois

Usage

To start up the app run:

$ yarn dev:server

Or:

npm run dev:server

Scripts

The application is shipped with a script to run a websocket server and emit monsters occurrences randomly. Run it by:

$ node ./scripts/websocket.js

Bearer Token

Some routes expect a Bearer Token in an Authorization header.

You can see these routes in the routes section.

GET http:https://localhost:3333/heroes Authorization: Bearer <token>

To get this token you just need authenticate through the /sessions route and it will return the token key with a valid Bearer Token.

Routes

route HTTP Method params description auth method
/users POST Body with user name, email and password. Create new user.
/sessions POST Body with user email and password. Authenticates user and return a Bearer Token.
/heroes GET - Return all heroes. Bearer
/heroes POST Body with hero name, latitude, longitude, rank, status and description Create a new hero. Bearer
/heroes/:id PUT Path param with hero id. Body with hero name, latitude, longitude, rank, status and description Update an existing hero. Bearer
/heroes/:id DELETE Path param with hero id. Delete an existing hero. Bearer
/monsters GET Query param with an optional monster status. Return all monsters optionally filtered by status. Bearer
/monsters/:id/defeated PUT Path param with monster id. Body with an array of heroes with id and status (after the combat finished). Update monster and hero(es) status. Bearer

Requests

  • POST /users

Request body:

{
  "name": "John Doe",
  "email": "[email protected]",
  "password": "123456"
}
  • POST /sessions

Request body:

{
  "email": "[email protected]",
  "password": "123456"
}
  • POST /heroes

Request body:

{
  "name": "Saitama",
  "latitude": "79.1721",
  "longitude": "43.0377",
  "rank": "A",
  "status": "patrolling",
  "description": "The most powerful, but not recognized hero",
}
  • PUT /heroes/:id

Request body:

{
  "name": "Saitama",
  "latitude": "79.1721",
  "longitude": "43.0377",
  "rank": "A",
  "status": "patrolling",
  "description": "The most powerful, but not recognized hero",
}
  • PUT /monsters/:id/defeated

Request body:

{
  "heroes": [
    {
      "_id": "3f7d0f46-9349-4e20-bcea-80b7e9fdd839",
      "status": "out_of_combat"
    },
    {
      "_id": "83916f30-59e1-4a57-abb0-396d921169e5",
      "status": "resting"
    }
  ]
}

Running the tests

Jest was the choice to test the app, to run:

$ yarn test

Or:

$ npm run test

Coverage report

You can see the coverage report inside tests/coverage. They are automatically created after the tests run.