import type { Express } from "express"; import { createServer, type Server } from "http"; import { storage } from "./storage"; import { insertLabelSchema, insertTaskSchema } from "@shared/schema"; export async function registerRoutes(app: Express): Promise { // Health check endpoint app.get("/api/health", (req, res) => { res.status(200).json({ status: "ok", timestamp: new Date().toISOString() }); }); // Labels API routes app.get("/api/labels", async (req, res) => { try { const labels = await storage.getAllLabels(); res.json(labels); } catch (error) { res.status(500).json({ error: "Failed to fetch labels" }); } }); app.get("/api/labels/:id", async (req, res) => { try { const label = await storage.getLabel(req.params.id); if (!label) { return res.status(404).json({ error: "Label not found" }); } res.json(label); } catch (error) { res.status(500).json({ error: "Failed to fetch label" }); } }); app.post("/api/labels", async (req, res) => { try { const result = insertLabelSchema.safeParse(req.body); if (!result.success) { return res.status(400).json({ error: "Invalid label data", details: result.error }); } const label = await storage.createLabel(result.data); res.status(201).json(label); } catch (error) { res.status(500).json({ error: "Failed to create label" }); } }); app.patch("/api/labels/:id", async (req, res) => { try { const updates = insertLabelSchema.partial().safeParse(req.body); if (!updates.success) { return res.status(400).json({ error: "Invalid label data", details: updates.error }); } const label = await storage.updateLabel(req.params.id, updates.data); if (!label) { return res.status(404).json({ error: "Label not found" }); } res.json(label); } catch (error) { res.status(500).json({ error: "Failed to update label" }); } }); app.delete("/api/labels/:id", async (req, res) => { try { const success = await storage.deleteLabel(req.params.id); if (!success) { return res.status(404).json({ error: "Label not found" }); } res.status(204).send(); } catch (error) { res.status(500).json({ error: "Failed to delete label" }); } }); // Tasks API routes app.get("/api/tasks", async (req, res) => { try { const tasks = await storage.getAllTasks(); res.json(tasks); } catch (error) { res.status(500).json({ error: "Failed to fetch tasks" }); } }); app.get("/api/tasks/:id", async (req, res) => { try { const task = await storage.getTask(req.params.id); if (!task) { return res.status(404).json({ error: "Task not found" }); } res.json(task); } catch (error) { res.status(500).json({ error: "Failed to fetch task" }); } }); app.post("/api/tasks", async (req, res) => { try { const result = insertTaskSchema.safeParse(req.body); if (!result.success) { return res.status(400).json({ error: "Invalid task data", details: result.error }); } const task = await storage.createTask(result.data); res.status(201).json(task); } catch (error) { res.status(500).json({ error: "Failed to create task" }); } }); app.patch("/api/tasks/:id", async (req, res) => { try { const updates = insertTaskSchema.partial().safeParse(req.body); if (!updates.success) { return res.status(400).json({ error: "Invalid task data", details: updates.error }); } const task = await storage.updateTask(req.params.id, updates.data); if (!task) { return res.status(404).json({ error: "Task not found" }); } res.json(task); } catch (error) { res.status(500).json({ error: "Failed to update task" }); } }); app.delete("/api/tasks/:id", async (req, res) => { try { const success = await storage.deleteTask(req.params.id); if (!success) { return res.status(404).json({ error: "Task not found" }); } res.status(204).send(); } catch (error) { res.status(500).json({ error: "Failed to delete task" }); } }); const httpServer = createServer(app); return httpServer; }