Skip to content

Materials Endpoint

Diogo Correia edited this page Dec 14, 2020 · 1 revision

This endpoint handles interactions with the materials 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/materials/routes.js file and are all prefixed by /materials.

Material object

  • name (required): The name of the material
  • description
  • stock: Number of item in stock
  • state: good, damaged or retired
  • type: item, tool or other
  • value: approximate cost of the item/tool - float (in EUR)
  • location: a location object (or location id when creating/editing)

GET /materials

Required role: user

Fetch all materials in the database.

Example successful response (200):

[
    {
        "id": 1,
        "name": "Test material",
        "description": "Some description!",
        "stock": 1,
        "state": "good",
        "type": "tool",
        "value": "0.00",
        "location": {
            "id": 1,
            "name": "Location 1",
            "description": "Testing location 1"
        }
    },
    {
        "id": 2,
        "name": "Test material 2",
        "description": "Another description",
        "stock": 1,
        "state": "damaged",
        "type": "item",
        "value": "4.00",
        "location": {
            "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 /material/:id

Required role: user

Fetch material in the database with the given id.

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

{
    "id": 1,
    "name": "Test material",
    "description": "Some description!",
    "stock": 1,
    "state": "good",
    "type": "tool",
    "value": "0.00",
    "location": {
        "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 material with id :id does not exist in the database
  • 500 Internal Server Error -> Something failed in the backend

POST /material

Required role: admin

Add a material to the database with the payload.

Example payload:

{
    "name": "Test material", // Required, cannot be an empty string
    "description": "Some description!",
    "stock": 1, // Defaults to 1
    "state": "good", // Defaults to 'good'
    "type": "tool", // Defaults to 'item'
    "value": "0.00",
    "locationId": 1 // Required
}

Example successful response (200):

{
    "id": 1,
    "name": "Test material",
    "description": "Some description!",
    "stock": 1,
    "state": "good",
    "type": "tool",
    "value": "0.00",
    "location": {
        "id": 1,
        "name": "Location 1",
        "description": "Testing location 1"
    }
}

Response codes

  • 200 Success -> Response returned successfully
  • 400 Bad Request -> The given payload is not valid or location does not exist
  • 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 /material/:id

Required role: admin

Deletes a location from the database with the given id.

Example request: /material/3

Response codes

  • 204 No Content -> Material 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 material with id :id has requisitions linked to it, and therefore can't be deleted
  • 404 Not Found -> The material with id :id does not exist in the database
  • 500 Internal Server Error -> Something failed in the backend

POST /material/:id

Required role: admin

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

Example request: /material/2 Example payload:

{
    "stock": 4,
    "type": "other",
}

Example successful response (200):

{
    "id": 1,
    "name": "Test material",
    "description": "Some description!",
    "stock": 4,
    "state": "good",
    "type": "other",
    "value": "0.00",
    "location": {
        "id": 1,
        "name": "Location 1",
        "description": "Testing location 1"
    }
}

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 material with id :id does not exist in the database
  • 500 Internal Server Error -> Something failed in the backend