Skip to content
Source
Sign inSign up
fuko/trytanstackdb.com
Open History to see the last commit

api.todo-items.ts

119 lines3.3 KB

1import { eq } from "drizzle-orm";
2import z from "zod";
3import { db } from "@/db";
4import { type TodoItemRecord, todoItemsTable } from "@/db/schema";
5import { type APIRouteHandler, json } from "./helpers";
6​
7const todoItemCreateData = z.object({
8 id: z.string().min(1),
9 boardId: z.string(),
10 priority: z.number().min(0).max(3).int().optional().nullable(),
11 title: z.string(),
12 description: z.string().optional().nullable(),
13 position: z.string(),
14});
15​
16export type TodoItemCreateDataType = z.infer<typeof todoItemCreateData>;
17​
18export const todoItemUpdateData = z.object({
19 id: z.string(),
20 boardId: z.string().optional(),
21 priority: z.number().nullable().optional(),
22 title: z.string().optional(),
23 description: z.string().nullable().optional(),
24 position: z.string().optional(),
25});
26​
27export default {
28 GET: async () => {
29 const results = await db.select().from(todoItemsTable);
30​
31 return json(results);
32 },
33 POST: async ({ request }) => {
34 // Create new todo item
35 let newTodoItemData: Omit<z.infer<typeof todoItemCreateData>, "projectId">;
36 // biome-ignore lint/suspicious/noExplicitAny: it can be any here
37 let bodyObj: any;
38​
39 try {
40 bodyObj = await request.json();
41 } catch (e) {
42 console.error("Error parsing JSON body:", e);
43 return new Response(`Invalid JSON body`, { status: 400 });
44 }
45​
46 try {
47 const todoItemData = todoItemCreateData.parse(bodyObj);
48 newTodoItemData = todoItemData;
49 } catch (e) {
50 console.error("Validation error:", e);
51 if (e instanceof z.ZodError) {
52 return new Response(`Invalid request data: ${z.prettifyError(e)}`, {
53 status: 400,
54 });
55 }
56 console.error("Bad format", e);
57 return new Response(`Validation error`, { status: 400 });
58 }
59​
60 const { description } = newTodoItemData;
61​
62 if (!description) {
63 return new Response(`Description is required`, { status: 400 });
64 }
65​
66 await db.insert(todoItemsTable).values({
67 ...newTodoItemData,
68 description,
69 createdAt: new Date(),
70 priority: 0,
71 } satisfies TodoItemRecord);
72​
73 return json(
74 { created: "ok" },
75 {
76 status: 201,
77 },
78 );
79 },
80 PATCH: async ({ request }) => {
81 let updatedData: z.infer<typeof todoItemUpdateData>;
82​
83 // biome-ignore lint/suspicious/noExplicitAny: it can be any here
84 let bodyObj: any;
85​
86 try {
87 bodyObj = await request.json();
88 } catch (e) {
89 console.error("Error parsing JSON body:", e);
90 return new Response(`Invalid JSON body`, { status: 400 });
91 }
92​
93 try {
94 updatedData = todoItemUpdateData.parse(bodyObj);
95 } catch (e) {
96 console.error("Validation error:", e);
97 if (e instanceof z.ZodError) {
98 return new Response(`Invalid request data: ${z.prettifyError(e)}`, {
99 status: 400,
100 });
101 }
102 console.error("Bad format", e);
103 return new Response(`Validation error`, { status: 400 });
104 }
105​
106 if (
107 Object.keys(updatedData).length === 1 // there aren't keys other than id
108 ) {
109 return new Response(`No columns to update`, { status: 400 });
110 }
111​
112 const results = await db
113 .update(todoItemsTable)
114 .set(updatedData)
115 .where(eq(todoItemsTable.id, updatedData.id));
116​
117 return json(results);
118 },
119} satisfies APIRouteHandler;