In this session. we will implement an API to retrieve fire detection logs with pagination.
First. we will create the `get_detection_log` domain and add `router`. `schema`. and `crud` files within it.
The `router` file assembles the service logic and sequentially calls the contents of `crud` and `schema`.
When calling this API. parameters for pagination are required.
`page` – The page number to retrieve .
The number of records to display per page
A filter to retrieve logs that meet specific conditions.
Now. let’s take a look at the `router` file.
The received parameters are passed to the `get_detection_logs` function in `crud`. which retrieves the log data.
The `get_total_detection_logs_count` function is called to calculate the total number of pages.
Finally. the retrieved log list and total count are returned.
Looking at the `crud` file. the overall flow is as follows.
A query object is created to search for data in the `DetectionLog` table
So that conditions. sorting. and pagination can be applied later
Allowing for efficient retrieval of the desired data.
Sorting is done in descending order based on `created_at`.
offset is used for pagination.
limit is set to restrict the number of records per page.
The `detections` field in the `DetectionLog` object retrieved from the database is stored as a JSON string.
We need to convert this string into a Python object. To make it easier to work with in subsequent code.
Additionally. the `get_total_detection_logs_count` function simply retrieves the total number of logs.
Finally. the processed logs are returned.
The `schema` file defines the return type of the API.
Additionally. since the `DetectionLog` model needs to be shared.
It is defined in the share schema file.
With all the files created. we will now proceed with testing.
We use the SQLite Extension to select the database and check the log data.
The fire detection API is executed to insert test data in advance.
Next. we will test the API using Postman.
We verify the pagination functionality by adjusting the `page` and `page_size` parameters.
This concludes the FastAPI implementation.
In the next session. we will build the frontend using Next.js.