WIP
This commit is contained in:
parent
beb488b3d6
commit
4275d51faf
5
db.ts
5
db.ts
|
@ -2,7 +2,7 @@ import {
|
|||
createPentagon,
|
||||
TableDefinition,
|
||||
} from 'https://deno.land/x/pentagon@v0.1.5/mod.ts'
|
||||
import { TaskModel, TodoModel, UserModel } from '@homeman/models.ts'
|
||||
import { TodoModel, UserModel } from '@homeman/models.ts'
|
||||
|
||||
export const kv = await Deno.openKv('homeman.db')
|
||||
|
||||
|
@ -19,9 +19,6 @@ export const schema: Record<string, TableDefinition> = {
|
|||
assignee: ['users', UserModel, 'assigneeUserId', 'id'],
|
||||
},
|
||||
},
|
||||
tasks: {
|
||||
schema: TaskModel,
|
||||
},
|
||||
}
|
||||
|
||||
export const db = createPentagon(kv, schema)
|
||||
|
|
|
@ -1,16 +1,9 @@
|
|||
import {
|
||||
DailyPhase,
|
||||
DailyPhaseModel,
|
||||
DoneTask,
|
||||
Task,
|
||||
toPhase,
|
||||
} from '@homeman/models.ts'
|
||||
import { DailyPhase, DailyPhaseModel, Task, toPhase } from '@homeman/models.ts'
|
||||
import { useSignal } from '@preact/signals'
|
||||
import { excitement } from '@homeman/common.ts'
|
||||
|
||||
export interface Props {
|
||||
tasks: Task[]
|
||||
done: DoneTask[]
|
||||
}
|
||||
|
||||
interface TaskWithIndex extends Task {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import { DailyPhase, TaskModel } from '@homeman/models.ts'
|
||||
import { db } from '@homeman/db.ts'
|
||||
import { kv } from '@homeman/db.ts'
|
||||
import { ulid } from 'https://deno.land/x/ulid@v0.3.0/mod.ts'
|
||||
|
||||
const hardcoded: Record<DailyPhase, [string, string][]> = {
|
||||
|
@ -41,6 +41,7 @@ const hardcoded: Record<DailyPhase, [string, string][]> = {
|
|||
['👨⚖️', 'Sleep!'],
|
||||
],
|
||||
}
|
||||
console.log({ kv })
|
||||
Object.entries(hardcoded).forEach(async ([tphase, phaseTasks]) => {
|
||||
const phase = tphase as DailyPhase
|
||||
for (const [emoji, description] of phaseTasks) {
|
||||
|
@ -52,9 +53,10 @@ Object.entries(hardcoded).forEach(async ([tphase, phaseTasks]) => {
|
|||
doneAt: null,
|
||||
})
|
||||
console.log(task)
|
||||
const result = await db.tasks.create({
|
||||
data: task,
|
||||
})
|
||||
console.log('after create:', result)
|
||||
try {
|
||||
await kv.set(['task', task.id], task)
|
||||
} catch (e) {
|
||||
console.error('error creating:', e)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { Handlers } from '$fresh/server.ts'
|
||||
import { Task, TaskModel } from '@homeman/models.ts'
|
||||
import { db, kv } from '@homeman/db.ts'
|
||||
import { kv } from '@homeman/db.ts'
|
||||
import { ulid } from 'https://deno.land/x/ulid@v0.3.0/mod.ts'
|
||||
import { z } from 'https://deno.land/x/zod@v3.21.4/mod.ts'
|
||||
|
||||
|
@ -9,16 +9,17 @@ type TaskPayload = z.infer<typeof TaskPayload>
|
|||
|
||||
async function createOrUpdate(task: TaskPayload) {
|
||||
if (!task.id || task.id === '') {
|
||||
const newTask: Task = {
|
||||
const newTask: Task = TaskModel.parse({
|
||||
...task,
|
||||
id: ulid(),
|
||||
}
|
||||
const result = await db.tasks.create({ data: newTask })
|
||||
})
|
||||
const result = await kv.set(['task', newTask.id], newTask)
|
||||
await kv.set(['last_task_updated'], newTask.id)
|
||||
return result
|
||||
} else {
|
||||
const result = await db.tasks.update({ where: { id: task.id }, data: task })
|
||||
await kv.set(['last_task_updated'], task.id)
|
||||
const newTask: Task = TaskModel.parse(task)
|
||||
const result = await kv.set(['task', task.id], newTask)
|
||||
await kv.set(['last_task_updated'], newTask.id)
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
@ -42,17 +43,13 @@ export const handler: Handlers<Task | null> = {
|
|||
emoji: form.get('emoji')?.toString() || null,
|
||||
doneAt: form.get('doneAt')?.toString() || null,
|
||||
description: form.get('description')?.toString(),
|
||||
assigneeUserId: form.get('assigneeUserId')?.toString() || null,
|
||||
phase: form.get('phase')?.toString() || null,
|
||||
})
|
||||
|
||||
if (!id) {
|
||||
delete task.id
|
||||
}
|
||||
|
||||
if (!task.assigneeUserId) {
|
||||
delete task.id
|
||||
}
|
||||
|
||||
await createOrUpdate(task)
|
||||
|
||||
const url = new URL(req.url)
|
||||
|
|
Loading…
Reference in a new issue