Return DB results directly in todo router handlers

This commit is contained in:
Aman Varshney
2025-07-15 15:55:55 +05:30
parent b47671c24d
commit b9e929235d
3 changed files with 10 additions and 10 deletions

View File

@@ -0,0 +1,5 @@
---
"create-better-t-stack": patch
---
Return DB results directly in todo router handlers

View File

@@ -13,29 +13,26 @@ export const todoRouter = {
create: publicProcedure create: publicProcedure
.input(z.object({ text: z.string().min(1) })) .input(z.object({ text: z.string().min(1) }))
.handler(async ({ input }) => { .handler(async ({ input }) => {
const result = await db return await db
.insert(todo) .insert(todo)
.values({ .values({
text: input.text, text: input.text,
}); });
return result[0];
}), }),
toggle: publicProcedure toggle: publicProcedure
.input(z.object({ id: z.number(), completed: z.boolean() })) .input(z.object({ id: z.number(), completed: z.boolean() }))
.handler(async ({ input }) => { .handler(async ({ input }) => {
await db return await db
.update(todo) .update(todo)
.set({ completed: input.completed }) .set({ completed: input.completed })
.where(eq(todo.id, input.id)); .where(eq(todo.id, input.id));
return { success: true };
}), }),
delete: publicProcedure delete: publicProcedure
.input(z.object({ id: z.number() })) .input(z.object({ id: z.number() }))
.handler(async ({ input }) => { .handler(async ({ input }) => {
await db.delete(todo).where(eq(todo.id, input.id)); return await db.delete(todo).where(eq(todo.id, input.id));
return { success: true };
}), }),
}; };
{{/if}} {{/if}}

View File

@@ -29,11 +29,10 @@ export const todoRouter = {
.input(z.object({ id: z.number(), completed: z.boolean() })) .input(z.object({ id: z.number(), completed: z.boolean() }))
{{/if}} {{/if}}
.handler(async ({ input }) => { .handler(async ({ input }) => {
await prisma.todo.update({ return await prisma.todo.update({
where: { id: input.id }, where: { id: input.id },
data: { completed: input.completed }, data: { completed: input.completed },
}); });
return { success: true };
}), }),
delete: publicProcedure delete: publicProcedure
@@ -43,10 +42,9 @@ export const todoRouter = {
.input(z.object({ id: z.number() })) .input(z.object({ id: z.number() }))
{{/if}} {{/if}}
.handler(async ({ input }) => { .handler(async ({ input }) => {
await prisma.todo.delete({ return await prisma.todo.delete({
where: { id: input.id }, where: { id: input.id },
}); });
return { success: true };
}), }),
}; };
{{/if}} {{/if}}