import type { APIRoute } from 'astro'; export const GET: APIRoute = async ({ locals }) => { try { const db = locals.runtime?.env?.DB; if (!db) { console.error('Database not available'); return new Response(JSON.stringify([]), { status: 200, headers: { 'Content-Type': 'application/json' } }); } const { results } = await db.prepare('SELECT * FROM tasks ORDER BY created_at DESC').all(); return new Response(JSON.stringify(results || []), { status: 200, headers: { 'Content-Type': 'application/json' } }); } catch (error) { console.error('Database error:', error); return new Response(JSON.stringify([]), { status: 200, headers: { 'Content-Type': 'application/json' } }); } }; export const POST: APIRoute = async ({ request, locals }) => { try { const db = locals.runtime?.env?.DB; if (!db) { return new Response(JSON.stringify({ error: 'Database not available' }), { status: 503, headers: { 'Content-Type': 'application/json' } }); } const body = await request.json(); const { title, description } = body; if (!title) { return new Response(JSON.stringify({ error: 'Title is required' }), { status: 400, headers: { 'Content-Type': 'application/json' } }); } const { results } = await db.prepare( 'INSERT INTO tasks (title, description) VALUES (?, ?) RETURNING *' ).bind(title, description || '').all(); return new Response(JSON.stringify(results?.[0] || { id: Date.now(), title, description, completed: false }), { status: 201, headers: { 'Content-Type': 'application/json' } }); } catch (error) { console.error('Database error:', error); return new Response(JSON.stringify({ error: 'Failed to create task' }), { status: 500, headers: { 'Content-Type': 'application/json' } }); } };