Skip to content

Locations Endpoint

Diogo Correia edited this page Dec 14, 2020 · 2 revisions

This endpoint handles interactions with the locations table in the database.

All the endpoints on this page require authentication using the Authorization header.
A JWT can be obtained through the Auth Endpoint.
The header sent must have the following format:

Authorization: Bearer <jwt here>

The routes are located in the backend/src/api/locations/routes.js file and are all prefixed by /locations.

GET /locations

Required role: user

Fetch all locations in the database.

Example successful response (200):

[
    {
        "id": 1,
        "name": "Location 1",
        "description": "Testing location 1"
    },
    {
        "id": 2,
        "name": "Location 2",
        "description": "Testing location 2"
    }
]

Response codes

  • 200 Success -> Response returned successfully
  • 401 Unauthorized -> Token is either invalid, empty or the user does not have access to this resource
  • 500 Internal Server Error -> Something failed in the backend

GET /location/:id

Required role: user

Fetch location in the database with the given id.

Example request: /location/1
Example successful response (200):

{
    "id": 1,
    "name": "Location 1",
    "description": "Testing location 1"
}

Response codes

  • 200 Success -> Response returned successfully
  • 400 Bad Request -> The given :id is not a positive integer
  • 401 Unauthorized -> Token is either invalid, empty or the user does not have access to this resource
  • 404 Not Found -> The location with id :id does not exist in the database
  • 500 Internal Server Error -> Something failed in the backend

POST /location

Required role: admin

Add a location to the database with the payload.

Example payload:

{
    "name": "Location 3", // Required, cannot be an empty string
    "description": "Testing location 3"
}

Example successful response (200):

{
    "id": 3,
    "name": "Location 3",
    "description": "Testing location 3"
}

Response codes

  • 200 Success -> Response returned successfully
  • 400 Bad Request -> The given payload is not valid
  • 401 Unauthorized -> Token is either invalid, empty or the user does not have access to this resource
  • 500 Internal Server Error -> Something failed in the backend

DELETE /location/:id

Required role: admin

Deletes a location from the database with the given id.

Example request: /location/3

Response codes

  • 204 No Content -> Location deleted successfully
  • 400 Bad Request -> The given :id is not a positive integer
  • 401 Unauthorized -> Token is either invalid, empty or the user does not have access to this resource
  • 403 Forbidden -> The location with id :id has materials linked to it, and therefore can't be deleted
  • 404 Not Found -> The location with id :id does not exist in the database
  • 500 Internal Server Error -> Something failed in the backend

POST /location/:id

Required role: admin

Updates a location on the database with the (partial) payload.
Only the fields in the payload are updated, all other fields are left untouched.

Example request: /location/2 Example payload:

{
    "description": "This description has been edited"
}

Example successful response (200):

{
    "id": 2,
    "name": "Location 2",
    "description": "This description has been edited"
}

Response codes

  • 200 Success -> Response returned successfully
  • 400 Bad Request -> The given payload is not valid
  • 401 Unauthorized -> Token is either invalid, empty or the user does not have access to this resource
  • 404 Not Found -> The location with id :id does not exist in the database
  • 500 Internal Server Error -> Something failed in the backend