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

31 lines
838 B
TypeScript
Raw Normal View History

2024-01-11 18:57:29 -06:00
import { Handlers } from '$fresh/server.ts'
import { TodoModel } from '@homeman/models.ts'
import { db, kv } from '@homeman/db.ts'
2024-01-11 18:57:29 -06:00
const Model = TodoModel.pick({ id: true })
2024-01-17 15:10:28 -06:00
async function setDoneAt(id: string, doneAt: Date | null) {
const result = await db.todos.update({ where: { id }, data: { doneAt } })
await kv.set(['last_todo_updated'], id)
return result
}
2024-01-16 16:15:10 -06:00
async function markDone(id: string) {
2024-01-17 15:10:28 -06:00
return await setDoneAt(id, new Date())
2024-01-16 16:15:10 -06:00
}
async function markNotDone(id: string) {
2024-01-17 15:10:28 -06:00
return await setDoneAt(id, null)
2024-01-16 16:15:10 -06:00
}
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
},
}