WEBVTT

﻿1
00:00:00.417 --> 00:00:07.007
In this session. we will implement an API to retrieve fire detection logs with pagination. 

2
00:00:08.133 --> 00:00:15.515
First. we will create the `get_detection_log` domain and add `router`. `schema`. and `crud` files within it.  

3
00:00:16.683 --> 00:00:24.065
The `router` file assembles the service logic and sequentially calls the contents of `crud` and `schema`.  

4
00:00:24.733 --> 00:00:29.237
When calling this API. parameters for pagination are required.  

5
00:00:30.030 --> 00:00:31.990
`page` – The page number to retrieve .

6
00:00:32.574 --> 00:00:35.285
The number of records to display per page  

7
00:00:35.994 --> 00:00:40.081
A filter to retrieve logs that meet specific conditions.

8
00:00:40.749 --> 00:00:43.376
Now. let’s take a look at the `router` file.  

9
00:00:43.626 --> 00:00:51.593
The received parameters are passed to the `get_detection_logs` function in `crud`. which retrieves the log data. 

10
00:00:52.385 --> 00:00:59.184
The `get_total_detection_logs_count` function is called to calculate the total number of pages.  

11
00:00:59.893 --> 00:01:04.355
Finally. the retrieved log list and total count are returned.  

12
00:01:06.149 --> 00:01:09.986
Looking at the `crud` file. the overall flow is as follows.

13
00:01:10.570 --> 00:01:15.742
A query object is created to search for data in the `DetectionLog` table  

14
00:01:16.785 --> 00:01:21.539
So that conditions. sorting. and pagination can be applied later  

15
00:01:22.415 --> 00:01:26.086
Allowing for efficient retrieval of the desired data. 

16
00:01:26.711 --> 00:01:30.507
Sorting is done in descending order based on `created_at`. 

17
00:01:31.132 --> 00:01:33.551
offset is used for pagination.  

18
00:01:34.302 --> 00:01:37.514
limit is set to restrict the number of records per page.  

19
00:01:38.640 --> 00:01:45.939
The `detections` field in the `DetectionLog` object retrieved from the database is stored as a JSON string.  

20
00:01:46.689 --> 00:01:54.239
We need to convert this string into a Python object. 
To make it easier to work with in subsequent code. 

21
00:01:54.989 --> 00:02:02.122
Additionally. the `get_total_detection_logs_count` function simply retrieves the total number of logs.  

22
00:02:03.123 --> 00:02:06.209
Finally. the processed logs are returned.  

23
00:02:07.752 --> 00:02:11.464
The `schema` file defines the return type of the API.  

24
00:02:13.299 --> 00:02:18.054
Additionally. since the `DetectionLog` model needs to be shared.  

25
00:02:18.054 --> 00:02:20.849
It is defined in the share schema file.  

26
00:02:21.516 --> 00:02:25.687
With all the files created. we will now proceed with testing.  

27
00:02:26.813 --> 00:02:32.569
We use the SQLite Extension to select the database and check the log data.  

28
00:02:40.451 --> 00:02:45.290
The fire detection API is executed to insert test data in advance.  

29
00:02:47.125 --> 00:02:50.086
Next. we will test the API using Postman.  

30
00:02:52.088 --> 00:02:58.386
We verify the pagination functionality by adjusting the `page` and `page_size` parameters.  

31
00:02:59.971 --> 00:03:02.932
This concludes the FastAPI implementation.  

32
00:03:05.185 --> 00:03:09.856
In the next session. we will build the frontend using Next.js. 
