Add task data layer and server actions

This commit is contained in:
Grok Bot
2026-11-26 09:10:37 +00:00
parent 4924059685
commit bfbc6e4407
2 changed files with 70 additions and 0 deletions
+44
View File
@@ -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<Task[]> {
const { rows } = await getPool().query<Task>(
'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<Task> {
const { rows } = await getPool().query<Task>(
'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<Task | null> {
const { rows } = await getPool().query<Task>(
'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<boolean> {
const r = await getPool().query('DELETE FROM tasks WHERE id = $1', [id]);
return (r.rowCount ?? 0) > 0;
}