From bfbc6e4407c692aecb8e0e8dec9a3b5e122a86c1 Mon Sep 17 00:00:00 2001 From: Grok Bot Date: Thu, 26 Nov 2026 09:10:37 +0000 Subject: [PATCH] Add task data layer and server actions --- app/actions.ts | 26 ++++++++++++++++++++++++++ lib/tasks.ts | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 app/actions.ts create mode 100644 lib/tasks.ts diff --git a/app/actions.ts b/app/actions.ts new file mode 100644 index 0000000..7eb0dc4 --- /dev/null +++ b/app/actions.ts @@ -0,0 +1,26 @@ +'use server'; + +import { revalidatePath } from 'next/cache'; +import { createTask, deleteTask, isStatus, moveTask } from '@/lib/tasks'; + +export async function addTask(formData: FormData) { + const title = String(formData.get('title') ?? '').trim(); + if (!title) return; + await createTask(title, String(formData.get('assignee') ?? '')); + revalidatePath('/'); +} + +export async function setStatus(formData: FormData) { + const id = Number(formData.get('id')); + const status = formData.get('status'); + if (!Number.isInteger(id) || !isStatus(status)) return; + await moveTask(id, status); + revalidatePath('/'); +} + +export async function removeTask(formData: FormData) { + const id = Number(formData.get('id')); + if (!Number.isInteger(id)) return; + await deleteTask(id); + revalidatePath('/'); +} diff --git a/lib/tasks.ts b/lib/tasks.ts new file mode 100644 index 0000000..aaa9f1f --- /dev/null +++ b/lib/tasks.ts @@ -0,0 +1,44 @@ +import { getPool } from './db'; + +export type Status = 'todo' | 'doing' | 'done'; +export const STATUSES: Status[] = ['todo', 'doing', 'done']; + +export type Task = { + id: number; + title: string; + status: Status; + assignee: string | null; + notes: string | null; + created_at: Date; + updated_at: Date; +}; + +export const isStatus = (v: unknown): v is Status => typeof v === 'string' && (STATUSES as string[]).includes(v); + +export async function listTasks(): Promise { + const { rows } = await getPool().query( + 'SELECT id, title, status, assignee, notes, created_at, updated_at FROM tasks ORDER BY id' + ); + return rows; +} + +export async function createTask(title: string, assignee?: string | null): Promise { + const { rows } = await getPool().query( + 'INSERT INTO tasks (title, assignee) VALUES ($1, $2) RETURNING *', + [title.trim().slice(0, 200), assignee?.trim().slice(0, 80) || null] + ); + return rows[0]; +} + +export async function moveTask(id: number, status: Status): Promise { + const { rows } = await getPool().query( + 'UPDATE tasks SET status = $2, updated_at = now() WHERE id = $1 RETURNING *', + [id, status] + ); + return rows[0] ?? null; +} + +export async function deleteTask(id: number): Promise { + const r = await getPool().query('DELETE FROM tasks WHERE id = $1', [id]); + return (r.rowCount ?? 0) > 0; +}