refactor(todo): 移除 Todo API 示例
This commit is contained in:
@@ -1,20 +0,0 @@
|
||||
import { describe, expect, test } from 'bun:test'
|
||||
import { createInsertSchema } from 'drizzle-zod'
|
||||
import { generatedFieldKeys } from '@/server/db/fields'
|
||||
import { todoTable } from '@/server/db/schema'
|
||||
|
||||
describe('todo insert schema', () => {
|
||||
const insertSchema = createInsertSchema(todoTable).omit(generatedFieldKeys)
|
||||
|
||||
test('accepts a minimal valid input', () => {
|
||||
expect(insertSchema.safeParse({ title: 'buy milk' }).success).toBe(true)
|
||||
})
|
||||
|
||||
test('rejects missing title', () => {
|
||||
expect(insertSchema.safeParse({}).success).toBe(false)
|
||||
})
|
||||
|
||||
test('rejects non-string title', () => {
|
||||
expect(insertSchema.safeParse({ title: 42 }).success).toBe(false)
|
||||
})
|
||||
})
|
||||
@@ -1,34 +0,0 @@
|
||||
import { oc } from '@orpc/contract'
|
||||
import { createInsertSchema, createSelectSchema, createUpdateSchema } from 'drizzle-zod'
|
||||
import { z } from 'zod'
|
||||
import { generatedFieldKeys } from '@/server/db/fields'
|
||||
import { todoTable } from '@/server/db/schema'
|
||||
|
||||
const selectSchema = createSelectSchema(todoTable)
|
||||
|
||||
const insertSchema = createInsertSchema(todoTable).omit(generatedFieldKeys)
|
||||
|
||||
const updateSchema = createUpdateSchema(todoTable)
|
||||
.omit(generatedFieldKeys)
|
||||
.refine((data) => Object.keys(data).length > 0, { message: 'At least one field is required' })
|
||||
|
||||
export const list = oc.input(z.void()).output(z.array(selectSchema))
|
||||
|
||||
export const create = oc.input(insertSchema).output(selectSchema)
|
||||
|
||||
export const update = oc
|
||||
.input(
|
||||
z.object({
|
||||
id: z.uuid(),
|
||||
data: updateSchema,
|
||||
}),
|
||||
)
|
||||
.output(selectSchema)
|
||||
|
||||
export const remove = oc
|
||||
.input(
|
||||
z.object({
|
||||
id: z.uuid(),
|
||||
}),
|
||||
)
|
||||
.output(z.void())
|
||||
@@ -1,39 +0,0 @@
|
||||
import { ORPCError } from '@orpc/server'
|
||||
import { eq } from 'drizzle-orm'
|
||||
import { os } from '@/server/api/server'
|
||||
import { db } from '@/server/db'
|
||||
import { todoTable } from '@/server/db/schema'
|
||||
|
||||
export const list = os.todo.list.handler(async () => {
|
||||
return db.query.todoTable.findMany({
|
||||
orderBy: (table, { desc }) => desc(table.createdAt),
|
||||
})
|
||||
})
|
||||
|
||||
export const create = os.todo.create.handler(async ({ input }) => {
|
||||
const [newTodo] = await db.insert(todoTable).values(input).returning()
|
||||
|
||||
if (!newTodo) {
|
||||
throw new ORPCError('INTERNAL_SERVER_ERROR', { message: 'Failed to create todo' })
|
||||
}
|
||||
|
||||
return newTodo
|
||||
})
|
||||
|
||||
export const update = os.todo.update.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.todo.remove.handler(async ({ input }) => {
|
||||
const [deleted] = await db.delete(todoTable).where(eq(todoTable.id, input.id)).returning({ id: todoTable.id })
|
||||
|
||||
if (!deleted) {
|
||||
throw new ORPCError('NOT_FOUND')
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user