summaryrefslogtreecommitdiff
path: root/src/pages/api/tasks.ts
blob: 2b76ed3106331cd4986d9c588bc4619be417ca10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import type { APIRoute } from 'astro';
import { jsonResponse, getDB, handleAPIError } from '../../lib/api';

export const prerender = false;

export const GET: APIRoute = async ({ locals }) => {
  return handleAPIError(async () => {
    const db = getDB(locals);
    const { results } = await db.prepare('SELECT * FROM tasks ORDER BY created_at DESC').all();
    return jsonResponse(results || []);
  }, []);
};

export const POST: APIRoute = async ({ request, locals }) => {
  return handleAPIError(async () => {
    const db = getDB(locals);
    const body = await request.json();
    const { title, description } = body;

    if (!title) {
      return jsonResponse({ error: 'Title is required' }, 400);
    }

    const { results } = await db.prepare(
      'INSERT INTO tasks (title, description) VALUES (?, ?) RETURNING *'
    ).bind(title, description || '').all();

    return jsonResponse(results?.[0] || { id: Date.now(), title, description, completed: false }, 201);
  });
};