Browse Source

[feat] Add category filtering

Sergio Mattei 2 years ago
parent
commit
e1afcd9516
1 changed files with 14 additions and 2 deletions
  1. 14
    2
      pages/api/getTopicList.ts

+ 14
- 2
pages/api/getTopicList.ts View File

7
 function getTopicsInDatabase(notionResp: any): Topic[] {
7
 function getTopicsInDatabase(notionResp: any): Topic[] {
8
   return notionResp.results.map((block: any) => ({
8
   return notionResp.results.map((block: any) => ({
9
     blockId: block.id,
9
     blockId: block.id,
10
-    name: block.properties.Name.title[0].plain_text,
10
+    name: block.properties.Name?.title?.[0]?.plain_text ?? "Untitled",
11
   }));
11
   }));
12
 }
12
 }
13
 
13
 
22
   });
22
   });
23
 
23
 
24
   const {
24
   const {
25
-    query: { type },
25
+    query: { type, category },
26
   } = req;
26
   } = req;
27
   const blockId = getDatabaseId(type as DatabaseType);
27
   const blockId = getDatabaseId(type as DatabaseType);
28
   if (!blockId) {
28
   if (!blockId) {
29
     res.status(404).json({ status: "Not found." });
29
     res.status(404).json({ status: "Not found." });
30
   } else {
30
   } else {
31
+    const categoryFilter = category
32
+      ? {
33
+          filter: {
34
+            property: "Category",
35
+            select: {
36
+              equals: category,
37
+            },
38
+          },
39
+        }
40
+      : {};
31
     const resp = await notion.databases.query({
41
     const resp = await notion.databases.query({
32
       database_id: blockId,
42
       database_id: blockId,
33
       sorts: [
43
       sorts: [
36
           direction: "ascending",
46
           direction: "ascending",
37
         },
47
         },
38
       ],
48
       ],
49
+      ...categoryFilter,
39
     });
50
     });
51
+    console.log(resp);
40
     res.status(200).json(getTopicsInDatabase(resp));
52
     res.status(200).json(getTopicsInDatabase(resp));
41
   }
53
   }
42
 }
54
 }