summaryrefslogtreecommitdiff
path: root/src/pages/api
diff options
context:
space:
mode:
authorYuval Adam <_@yuv.al>2025-06-27 10:10:56 +0200
committerYuval Adam <_@yuv.al>2025-06-27 10:10:56 +0200
commit2ad5ce929f6d69c9aaca71573744b30e02ab8bed (patch)
tree078cd3b8a968ebfd99e86492abb9564f183ec21a /src/pages/api
parent0282fb367ecfd3864cf94c27a44961a61e2ff6f2 (diff)
Add cloudflare basic impl
Diffstat (limited to 'src/pages/api')
-rw-r--r--src/pages/api/tasks.ts66
-rw-r--r--src/pages/api/tasks/[id].ts57
2 files changed, 123 insertions, 0 deletions
diff --git a/src/pages/api/tasks.ts b/src/pages/api/tasks.ts
new file mode 100644
index 0000000..00328e8
--- /dev/null
+++ b/src/pages/api/tasks.ts
@@ -0,0 +1,66 @@
+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' }
+ });
+ }
+}; \ No newline at end of file
diff --git a/src/pages/api/tasks/[id].ts b/src/pages/api/tasks/[id].ts
new file mode 100644
index 0000000..e9341b6
--- /dev/null
+++ b/src/pages/api/tasks/[id].ts
@@ -0,0 +1,57 @@
+import type { APIRoute } from 'astro';
+
+export const PUT: APIRoute = async ({ params, request, locals }) => {
+ try {
+ const db = locals.runtime.env.DB;
+ const id = params.id;
+ const body = await request.json();
+ const { title, description, completed } = body;
+
+ const { results } = await db.prepare(
+ 'UPDATE tasks SET title = ?, description = ?, completed = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ? RETURNING *'
+ ).bind(title, description || '', completed || false, id).all();
+
+ if (results.length === 0) {
+ return new Response(JSON.stringify({ error: 'Task not found' }), {
+ status: 404,
+ headers: { 'Content-Type': 'application/json' }
+ });
+ }
+
+ return new Response(JSON.stringify(results[0]), {
+ status: 200,
+ headers: { 'Content-Type': 'application/json' }
+ });
+ } catch (error) {
+ return new Response(JSON.stringify({ error: 'Failed to update task' }), {
+ status: 500,
+ headers: { 'Content-Type': 'application/json' }
+ });
+ }
+};
+
+export const DELETE: APIRoute = async ({ params, locals }) => {
+ try {
+ const db = locals.runtime.env.DB;
+ const id = params.id;
+
+ const { success } = await db.prepare('DELETE FROM tasks WHERE id = ?').bind(id).run();
+
+ if (!success) {
+ return new Response(JSON.stringify({ error: 'Task not found' }), {
+ status: 404,
+ headers: { 'Content-Type': 'application/json' }
+ });
+ }
+
+ return new Response(JSON.stringify({ message: 'Task deleted successfully' }), {
+ status: 200,
+ headers: { 'Content-Type': 'application/json' }
+ });
+ } catch (error) {
+ return new Response(JSON.stringify({ error: 'Failed to delete task' }), {
+ status: 500,
+ headers: { 'Content-Type': 'application/json' }
+ });
+ }
+}; \ No newline at end of file