Files
task-manager/server/routes.ts
T
paul-nothaft 52f29fa101 Add a system for organizing tasks with customizable color-coded labels
Implement a label system with API endpoints for CRUD operations on labels and tasks, including schema definitions for labels and tasks with label associations.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: ceced2fc-aa46-458d-ba87-ddd4b7bb1518
Replit-Commit-Checkpoint-Type: intermediate_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/659922a9-0087-461c-90dd-6d9a58b81d4d/ceced2fc-aa46-458d-ba87-ddd4b7bb1518/XrG8SoT
2025-09-11 11:54:28 +00:00

141 lines
4.1 KiB
TypeScript

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<Server> {
// 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;
}