homeman-deno/routes/api/todo/done.ts

26 lines
752 B
TypeScript
Raw Normal View History

2024-01-11 18:57:29 -06:00
import { Handlers } from '$fresh/server.ts'
import { db, TodoModel } from '@homeman/models.ts'
const Model = TodoModel.pick({ id: true })
2024-01-16 16:15:10 -06:00
async function markDone(id: string) {
const todo = await db.todos.findFirst({ where: { id } })
todo.doneAt = new Date()
return await db.todos.update({ where: { id }, data: todo })
}
async function markNotDone(id: string) {
return await db.todos.update({ where: { id }, data: { doneAt: null } })
}
2024-01-11 18:57:29 -06:00
export const handler: Handlers = {
2024-01-16 16:15:10 -06:00
async PUT(req, _ctx) {
const { id } = Model.parse(await req.json())
return new Response(JSON.stringify(await markDone(id)))
},
async DELETE(req, _ctx) {
2024-01-11 18:57:29 -06:00
const { id } = Model.parse(await req.json())
2024-01-16 16:15:10 -06:00
return new Response(JSON.stringify(await markNotDone(id)))
2024-01-11 18:57:29 -06:00
},
}