refactor: 重构待办事项模块,统一路由与数据操作逻辑

- 将客户端导出改为私有常量,避免外部直接访问。
- 添加待办事项的完整 CRUD 操作,包括列表查询、创建、更新和删除功能,并使用 Zod 进行输入输出验证和 Drizzle ORM 操作数据库。
- 导出客户端和路由器模块的公共接口
- 添加路由配置,将 todo 处理程序注册到路由系统中。
- 删除已废弃的路由定义文件
- 删除待办事项相关路由和接口定义
- 删除未使用的 TodoSchema 模式定义以清理代码库。
- 将 Todo 路由重命名为复数形式并迁移数据获取与操作逻辑至 Orpc 客户端调用
- 将路由名称和路径从 `/todo` 更新为 `/todos`,并同步更新相关类型定义和引用。
This commit is contained in:
2026-01-18 01:42:53 +08:00
parent e0369d3271
commit f2481c31c7
9 changed files with 114 additions and 132 deletions

83
src/orpc/handlers/todo.ts Normal file
View File

@@ -0,0 +1,83 @@
import { ORPCError, os } from '@orpc/server'
import { eq } from 'drizzle-orm'
import {
createInsertSchema,
createSelectSchema,
createUpdateSchema,
} from 'drizzle-zod'
import { z } from 'zod'
import { db } from '@/db'
import { todoTable } from '@/db/schema'
const selectSchema = createSelectSchema(todoTable)
const insertSchema = createInsertSchema(todoTable).omit({
id: true,
createdAt: true,
updatedAt: true,
})
const updateSchema = createUpdateSchema(todoTable).omit({
id: true,
createdAt: true,
updatedAt: true,
})
export const list = os
.input(z.void())
.output(z.array(selectSchema))
.handler(async () => {
const todos = await db.query.todoTable.findMany({
orderBy: (todos, { desc }) => [desc(todos.createdAt)],
})
return todos
})
export const create = os
.input(insertSchema)
.output(selectSchema)
.handler(async ({ input }) => {
const [newTodo] = await db
.insert(todoTable)
.values({ title: input.title })
.returning()
if (!newTodo) {
throw new ORPCError('NOT_FOUND')
}
return newTodo
})
export const update = os
.input(
z.object({
id: z.uuid(),
data: updateSchema,
}),
)
.output(selectSchema)
.handler(async ({ input }) => {
const [updatedTodo] = await db
.update(todoTable)
.set(input.data)
.where(eq(todoTable.id, input.id))
.returning()
if (!updatedTodo) {
throw new ORPCError('NOT_FOUND')
}
return updatedTodo
})
export const remove = os
.input(
z.object({
id: z.uuid(),
}),
)
.output(z.void())
.handler(async ({ input }) => {
await db.delete(todoTable).where(eq(todoTable.id, input.id))
})