From 6529cb3810b4a6b7c3f984e11534c9fd0b6deeb0 Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Sun, 21 Jan 2024 18:11:25 -0600 Subject: [PATCH] Unborked? --- islands/Routine.tsx | 4 ++-- routes/api/tasks.ts | 20 ++++++++++++++------ routes/routine.tsx | 7 +++++-- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/islands/Routine.tsx b/islands/Routine.tsx index 37821b5..b831018 100644 --- a/islands/Routine.tsx +++ b/islands/Routine.tsx @@ -55,7 +55,7 @@ export function Routine(
diff --git a/routes/api/tasks.ts b/routes/api/tasks.ts index 2c786a2..7f0f9e3 100644 --- a/routes/api/tasks.ts +++ b/routes/api/tasks.ts @@ -57,6 +57,14 @@ export const handler: Handlers = { return Response.redirect(url, 303) } }, + async PUT(req, _ctx) { + if (req.headers.get('content-type')?.includes('json')) { + const result = await createOrUpdate( + TaskPayload.parse(await req.json()), + ) + return new Response(JSON.stringify(result)) + } + }, async DELETE(req, _ctx) { // task: form or query params or json let data @@ -67,7 +75,7 @@ export const handler: Handlers = { } console.log('delete task data:', data) const taskData = TaskModel.pick({ id: true }).parse(data) - const result = await db.tasks.delete({ where: taskData }) + const result = await kv.delete(['task', taskData.id]) await kv.set(['last_task_updated'], taskData.id) return new Response(JSON.stringify(result)) }, @@ -95,9 +103,7 @@ export const handler: Handlers = { continue } - const task = await db.tasks.findFirst({ - where: { id: entry.value }, - }) + const task = (await kv.get(['task', entry.value])).value const chunk = `data: ${ JSON.stringify({ id: entry.value, @@ -133,10 +139,12 @@ export const handler: Handlers = { const taskData = TaskModel.pick({ id: true }).safeParse(data) if (taskData.success) { return new Response( - JSON.stringify(await db.tasks.findFirst({ where: taskData.data })), + JSON.stringify((await kv.get(['task', taskData.data.id])).value), ) } else { - return new Response(JSON.stringify(await db.tasks.findMany({}))) + return new Response( + JSON.stringify(Array.fromAsync(kv.list({ prefix: ['task'] }))), + ) } }, } diff --git a/routes/routine.tsx b/routes/routine.tsx index be87cc5..4437056 100644 --- a/routes/routine.tsx +++ b/routes/routine.tsx @@ -1,7 +1,7 @@ import { Handlers, PageProps } from '$fresh/server.ts' import { Task } from '@homeman/models.ts' import { Routine } from '@homeman/islands/Routine.tsx' -import { db } from '@homeman/db.ts' +import { kv } from '@homeman/db.ts' // import { db, kv, Todo, UserWithTodos } from '@homeman/models.ts' interface Data { @@ -10,7 +10,10 @@ interface Data { export const handler: Handlers = { async GET(_req, ctx) { - const tasks = await db.tasks.findMany({}) + const tasks = (await Array.fromAsync(kv.list({ prefix: ['task'] }))).map( + (r) => r.value + ) + console.log({ tasks }) return ctx.render({ tasks }) }, }