Backend Routes: Blocks - OtagoPolytechnic/Air-Quality-Monitoring-System GitHub Wiki

About

Documentation for the Co2 Buildings(Blocks), schema, and controllers.

Schema

model Block {
  id        Int      @id @default(autoincrement())
  blockName String   @unique
  createdAt DateTime @default(now())
  updatedAt DateTime @default(now()) @updatedAt
  device    Device[]
}
  • id: Default ID when created
  • blockName: Building block eg(D-block, C-block)
  • createdAt: When the device was created
  • updatedAt: When the device was last updated
  • device: List of devices that are connected to each block

Device Endpoints

GET: 'api/v1/blocks'
GET: 'api/v1/blocks/{[Letter]-Block}'
PUT: 'api/v1/blocks/{[Letter]-Block}'
GET: 'api/v1/blocks/latest/{[Letter]-Block}'
POST: 'api/v1/blocks/createBlock'

Get all blocks

Endpoint: /api/v1/blocks
Returns a list of all blocks currently in the database.

EXAMPLE RETURNED JSON

{
    "statusCode": 200,
    "data": [
        {
            "id": 4,
            "blockName": "A-Block",
            "createdAt": "2024-05-23T02:46:27.971Z",
            "updatedAt": "2024-05-23T02:46:27.971Z"
        },
        {
            "id": 3,
            "blockName": "B-Block",
            "createdAt": "2024-05-23T02:46:27.967Z",
            "updatedAt": "2024-05-23T02:46:27.967Z"
        }
    ],
    "nextPage": null
}

Get all devices for a block

Endpoint: /api/v1/blocks/{[Letter]-Block}
Returns a list of all devices related to a block.

EXAMPLE RETURNED JSON

{
    "statusCode": 200,
    "data": {
        "id": 1,
        "blockName": "D-Block",
        "createdAt": "2024-05-23T02:46:27.957Z",
        "updatedAt": "2024-05-23T02:46:27.957Z",
        "device": [
            {
                "room_number": null,
                "deviceId": "eui-1000024b080301f5",
                "dev_eui": "1000024b080301f5"
            },
            {
                "room_number": null,
                "deviceId": "eui-3000024b080301f5",
                "dev_eui": "3000024b080301f5"
            }
        ]
    }
}

Update Block name

Endpoint: api/v1/blocks/{[Letter]-block}
Updates a Blocks name

EXAMPLE UPDATE

Body Message:

"blockName": "D-Block"

Controller will check if the block name being updated is correct

{
    "statusCode": 404,
    "message": "0-Block not found on the server"
}

Then checks if the block already exists

{
    "statusCode": 409,
    "message": "D-block already exists in the database"
}

If no blockName is passed will return an error

{
    "statusCode": 400,
    "message": "New block name is required"
}

Block names cannot start with a space

{
    "statusCode": 400,
    "message": "Block name cannot start with a space"
}

Blocks must also follow an Uppercase letter -Block format

{
    "statusCode": 400,
    "message": "Block name must be in the format [Uppercase Letter]-Block"
}

Successfully updating a block will return the following

{
    "statusCode": 200,
    "message": "Block name updated successfully",
    "data": {
        "id": 1,
        "blockName": "X-Block",
        "createdAt": "2024-05-23T02:46:27.957Z",
        "updatedAt": "2024-05-23T03:13:56.932Z"
    }
}

Get all recent sensor information in a block

Endpoint: /api/v1/blocks/latest/{[X]-Block}
Returns a list of all devices from the selected block-
and shows each devices last updated sensor information.

EXAMPLE RETURNED JSON

{
    "statusCode": 200,
    "data": {
        "id": 1,
        "blockName": "D-Block",
        "createdAt": "2024-05-29T11:07:29.419Z",
        "updatedAt": "2024-05-29T11:07:29.419Z",
        "device": [
            {
                "room_number": "D207",
                "deviceId": "eui-1000024b080301f5",
                "dev_eui": "1000024b080301f5",
                "sensorData": [
                    {
                        "co2": "999",
                        "temperature": "30"
                    }
                ]
            },
            {
                "room_number": "D202",
                "deviceId": "eui-3000024b080301f5",
                "dev_eui": "3000024b080301f5",
                "sensorData": [
                    {
                        "co2": "400",
                        "temperature": "21"
                    }
                ]
            }
        ]
    }
}

Create a new block

Endpoint: /api/v1/blocks/createBlock
Creates a new block in the database.

EXAMPLE CREATE BLOCK

Creating will check if a name has been passed through

{
    "statusCode": 400,
    "message": "Block name is required"
}

Block names cannot start with a space

{
    "statusCode": 400,
    "message": "Block name cannot start with a space"
}

Blocks must follow a Uppercase letter -Block format D-block

{
    "statusCode": 400,
    "message": "Block name must be in the format [Uppercase Letter]-Block"
}

Controller will then check if that block name is already taken

{
    "statusCode": 409,
    "message": "X-Block already exists"
}

Create will return the following, for a successful block create.

{
    "statusCode": 201,
    "message": "U-Block created successfully",
    "data": {
        "id": 5,
        "blockName": "U-Block",
        "createdAt": "2024-05-23T03:18:32.214Z",
        "updatedAt": "2024-05-23T03:18:32.214Z"
    }
}