import { ORPCError } from '@orpc/server' import { eq } from 'drizzle-orm' import { todoTable } from '@/server/db/schema' import { db } from '../middlewares' import { os } from '../server' export const list = os.todo.list.use(db).handler(async ({ context }) => { const todos = await context.db.query.todoTable.findMany({ orderBy: { createdAt: 'desc' }, }) return todos }) export const create = os.todo.create .use(db) .handler(async ({ context, input }) => { const [newTodo] = await context.db .insert(todoTable) .values(input) .returning() if (!newTodo) { throw new ORPCError('NOT_FOUND') } return newTodo }) export const update = os.todo.update .use(db) .handler(async ({ context, input }) => { const [updatedTodo] = await context.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 .use(db) .handler(async ({ context, input }) => { await context.db.delete(todoTable).where(eq(todoTable.id, input.id)) })