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

31 lines
838 B
TypeScript

import { Handlers } from '$fresh/server.ts'
import { TodoModel } from '@homeman/models.ts'
import { db, kv } from '@homeman/db.ts'
const Model = TodoModel.pick({ id: true })
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
}
async function markDone(id: string) {
return await setDoneAt(id, new Date())
}
async function markNotDone(id: string) {
return await setDoneAt(id, null)
}
export const handler: Handlers = {
async PUT(req, _ctx) {
const { id } = Model.parse(await req.json())
return new Response(JSON.stringify(await markDone(id)))
},
async DELETE(req, _ctx) {
const { id } = Model.parse(await req.json())
return new Response(JSON.stringify(await markNotDone(id)))
},
}