This commit is contained in:
29
server/api/todos/[id].delete.ts
Normal file
29
server/api/todos/[id].delete.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { z } from 'zod'
|
||||
import { db, schema } from 'hub:db'
|
||||
import { and, eq } from 'drizzle-orm'
|
||||
|
||||
const ParamsSchema = z.object({
|
||||
id: z.coerce.number().int()
|
||||
})
|
||||
|
||||
export default eventHandler(async (event) => {
|
||||
const { id } = await getValidatedRouterParams(event, ParamsSchema.parse)
|
||||
const { user } = await requireUserSession(event)
|
||||
|
||||
// Delete todo for the current user
|
||||
const deletedTodos = await db.delete(schema.todos).where(
|
||||
and(
|
||||
eq(schema.todos.id, id),
|
||||
eq(schema.todos.userId, user.id)
|
||||
)
|
||||
).returning()
|
||||
|
||||
const deletedTodo = deletedTodos[0]
|
||||
if (!deletedTodo) {
|
||||
throw createError({
|
||||
statusCode: 404,
|
||||
message: 'Todo not found'
|
||||
})
|
||||
}
|
||||
return deletedTodo
|
||||
})
|
||||
34
server/api/todos/[id].patch.ts
Normal file
34
server/api/todos/[id].patch.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { z } from 'zod'
|
||||
import { db, schema } from 'hub:db'
|
||||
import { and, eq } from 'drizzle-orm'
|
||||
|
||||
const ParamsSchema = z.object({
|
||||
id: z.coerce.number().int()
|
||||
})
|
||||
|
||||
const BodySchema = z.object({
|
||||
completed: z.boolean()
|
||||
})
|
||||
|
||||
export default eventHandler(async (event) => {
|
||||
const { id } = await getValidatedRouterParams(event, ParamsSchema.parse)
|
||||
const { completed } = await readValidatedBody(event, BodySchema.parse)
|
||||
const { user } = await requireUserSession(event)
|
||||
|
||||
// Update todo for the current user
|
||||
const updatedTodos = await db.update(schema.todos).set({
|
||||
completed: completed ? 1 : 0
|
||||
}).where(and(
|
||||
eq(schema.todos.id, id),
|
||||
eq(schema.todos.userId, user.id)
|
||||
)).returning()
|
||||
|
||||
const todo = updatedTodos[0]
|
||||
if (!todo) {
|
||||
throw createError({
|
||||
statusCode: 404,
|
||||
message: 'Todo not found'
|
||||
})
|
||||
}
|
||||
return todo
|
||||
})
|
||||
11
server/api/todos/index.get.ts
Normal file
11
server/api/todos/index.get.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { db, schema } from 'hub:db'
|
||||
import { eq } from 'drizzle-orm'
|
||||
|
||||
export default eventHandler(async (event) => {
|
||||
const { user } = await requireUserSession(event)
|
||||
|
||||
// List todos for the current user
|
||||
const todos = await db.select().from(schema.todos).where(eq(schema.todos.userId, user.id))
|
||||
|
||||
return todos
|
||||
})
|
||||
20
server/api/todos/index.post.ts
Normal file
20
server/api/todos/index.post.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { z } from 'zod'
|
||||
import { db, schema } from 'hub:db'
|
||||
|
||||
const BodySchema = z.object({
|
||||
title: z.string().min(1).max(100)
|
||||
})
|
||||
|
||||
export default eventHandler(async (event) => {
|
||||
const { title } = await readValidatedBody(event, body => BodySchema.parse(body))
|
||||
const { user } = await requireUserSession(event)
|
||||
|
||||
// Insert todo for the current user
|
||||
const todos = await db.insert(schema.todos).values({
|
||||
userId: user.id,
|
||||
title,
|
||||
createdAt: new Date()
|
||||
}).returning()
|
||||
|
||||
return todos[0]
|
||||
})
|
||||
12
server/api/todos/stats.ts
Normal file
12
server/api/todos/stats.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { db, schema } from 'hub:db'
|
||||
import { sql } from 'drizzle-orm'
|
||||
|
||||
export default eventHandler(async () => {
|
||||
// Count the total number of todos
|
||||
const result = await db.select({
|
||||
todos: sql<number>`count(*)`,
|
||||
users: sql<number>`count(distinct(${schema.todos.userId}))`
|
||||
}).from(schema.todos)
|
||||
|
||||
return result[0]
|
||||
})
|
||||
Reference in New Issue
Block a user