Build a Finance SaaS Platform -13 (Category Page)

Modify DB Schema

Modify db/schema.ts

1
2
3
4
5
6
7
8
9
10
...
export const categories = pgTable("categories", {
id: text("id").primaryKey(),
plaidId: text("plaid_id"),
name: text("name").notNull(),
userId: text("user_id").notNull(),
})

export const insertCategoriesSchema = createInsertSchema(categories);
...

run script:

1
2
3
npm run db:generate
npm run db:migrate
npm run db:studio

Add Server API

Add app/api/[[...route]]/categories.ts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import { z } from "zod"
import { Hono } from "hono";
import { zValidator } from "@hono/zod-validator";
import { createId } from "@paralleldrive/cuid2";
import { clerkMiddleware, getAuth } from "@hono/clerk-auth";

import { db } from "@/db/drizzle";
import { categories, insertCategoriesSchema} from "@/db/schema";
import { and, eq, inArray } from "drizzle-orm";

const app = new Hono()
.get("/", clerkMiddleware(), async (c) => {
const auth = getAuth(c);

if( !auth?.userId ) {
return c.json({ error:"Unauthorized!"}, 401);
}

const data = await db.select({
id: categories.id,
name: categories.name,
}).from(categories)
.where(eq(categories.userId, auth.userId));

return c.json({ data });
})
.get(
"/:id",
zValidator("param",z.object({
id: z.string().optional(),
})),
clerkMiddleware(),
async (c) => {
const auth = getAuth(c);
const { id } = c.req.valid("param");

if (!id) {
return c.json({ error: "Missing id!" }, 400);
}

if (!auth?.userId) {
return c.json({ error: "Unauthorized!" }, 401);
}

const [data] = await db
.select({
id: categories.id,
name: categories.name,
})
.from(categories)
.where(
and(
eq(categories.userId, auth.userId),
eq(categories.id, id)
),
);

if (!data) {
return c.json({ error: "Not Found!"}, 404);
};

return c.json({ data });
}
)
.post(
"/",
clerkMiddleware(),
zValidator('json',insertCategoriesSchema.pick({
name:true,
})),
async(c) => {
const auth = getAuth(c);
const values = c.req.valid("json");

if(!auth?.userId){
return c.json({ error:"Unauthorized!"}, 401);
}

const data = await db.insert(categories).values({
id: createId(),
userId: auth.userId,
...values,
}).returning();

return c.json({ data });
})
.post(
"/bulk-delete",
clerkMiddleware(),
zValidator(
"json",
z.object({
ids: z.array(z.string())
}),
),
async (c) =>{
const auth = getAuth(c);
const values = c.req.valid("json");

if (!auth?.userId) {
return c.json({ error: "Unauthorized"}, 401 );
}

const data = await db
.delete(categories)
.where(
and(
eq(categories.userId, auth.userId),
inArray(categories.id, values.ids)
)
)
.returning({
id: categories.id,
});

return c.json({ data });
},
)
.patch(
"/:id",
clerkMiddleware(),
zValidator(
"param",
z.object({
id: z.string().optional(),
})
),
zValidator(
"json",
insertCategoriesSchema.pick({
name: true,
})
),
async (c) => {
const auth = getAuth(c);
const { id } = c.req.valid("param");
const values = c.req.valid("json");

if (!id){
return c.json({ error: "Missing id"}, 401);
}

if (!auth?.userId) {
return c.json({ error: "Unauthorized"},401);
}

const [data] = await db
.update(categories)
.set(values)
.where(
and(
eq(categories.userId, auth.userId),
eq(categories.id, id),
)
)
.returning();

if (!data) {
return c.json({ error: "Not Found"}, 404);
}

return c.json({ data });
}
)
.delete(
"/:id",
clerkMiddleware(),
zValidator(
"param",
z.object({
id: z.string().optional(),
})
),
async (c) => {
const auth = getAuth(c);
const { id } = c.req.valid("param");

if (!id){
return c.json({ error: "Missing id"}, 401);
}

if (!auth?.userId) {
return c.json({ error: "Unauthorized"},401);
}

const [data] = await db
.delete(categories)
.where(
and(
eq(categories.userId, auth.userId),
eq(categories.id, id),
)
)
.returning({
id: categories.id,
});

if (!data) {
return c.json({ error: "Not Found"}, 404);
}

return c.json({ data });
}
);
export default app;

Modify app/api/[[...route]]/route.ts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

import { Hono } from 'hono';
import { handle } from 'hono/vercel';

import accounts from "./accounts";
import categories from "./categories";

export const runtime = 'edge'

const app = new Hono().basePath('/api')

const routes = app
.route("/accounts", accounts)
.route("/categories", categories);

export const GET = handle(app);
export const POST = handle(app);
export const PATCH = handle(app);
export const DELETE = handle(app);

export type AppType = typeof routes;

Other New Files:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
app/(dashboard)/categories/actions.tsx
app/(dashboard)/categories/columns.tsx
app/(dashboard)/categories/page.tsx
app/api/[[...route]]/categories.ts
features/categories/api/use-bulk-delete-categories.ts
features/categories/api/use-create-category.ts
features/categories/api/use-delete-category.ts
features/categories/api/use-edit-categorty.ts
features/categories/api/use-get-categories.ts
features/categories/api/use-get-category.ts
features/categories/components/category-form.tsx
features/categories/components/edit-category-sheet.tsx
features/categories/components/new-category-sheet.tsx
features/categories/hooks/use-new-category.ts
features/categories/hooks/use-open-category.ts

请我喝杯咖啡吧~

支付宝
微信