30 lines
806 B
TypeScript
30 lines
806 B
TypeScript
import { Handlers } from '$fresh/server.ts'
|
|
import { db, kv, TodoModel } from '@homeman/models.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)))
|
|
},
|
|
}
|