generated from Dicken/dickendock
Commit 4: Kategorien-API (CRUD + Reorder) und Favoriten-Toggle im Frontend
This commit is contained in:
53
apps/backend/src/routes/categories.ts
Normal file
53
apps/backend/src/routes/categories.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import {
|
||||
CategoryCreateSchema,
|
||||
CategoryReorderSchema,
|
||||
CategoryUpdateSchema,
|
||||
} from "@launchpad/shared";
|
||||
import * as categoryRepo from "../db/repositories/categories.js";
|
||||
|
||||
export async function categoryRoutes(app: FastifyInstance): Promise<void> {
|
||||
app.get("/api/categories", async () => {
|
||||
return categoryRepo.listCategories();
|
||||
});
|
||||
|
||||
app.post("/api/categories", async (request, reply) => {
|
||||
const parsed = CategoryCreateSchema.safeParse(request.body);
|
||||
if (!parsed.success) {
|
||||
return reply.code(400).send({ error: "Ungültige Eingabe", issues: parsed.error.issues });
|
||||
}
|
||||
const category = categoryRepo.createCategory(parsed.data);
|
||||
return reply.code(201).send(category);
|
||||
});
|
||||
|
||||
// Muss vor der /:id-Route registriert sein, damit "reorder" nicht als ID interpretiert wird.
|
||||
app.patch("/api/categories/reorder", async (request, reply) => {
|
||||
const parsed = CategoryReorderSchema.safeParse(request.body);
|
||||
if (!parsed.success) {
|
||||
return reply.code(400).send({ error: "Ungültige Eingabe", issues: parsed.error.issues });
|
||||
}
|
||||
return categoryRepo.reorderCategories(parsed.data);
|
||||
});
|
||||
|
||||
app.patch("/api/categories/:id", async (request, reply) => {
|
||||
const { id } = request.params as { id: string };
|
||||
const parsed = CategoryUpdateSchema.safeParse(request.body);
|
||||
if (!parsed.success) {
|
||||
return reply.code(400).send({ error: "Ungültige Eingabe", issues: parsed.error.issues });
|
||||
}
|
||||
const category = categoryRepo.updateCategory(id, parsed.data);
|
||||
if (!category) {
|
||||
return reply.code(404).send({ error: "Kategorie nicht gefunden" });
|
||||
}
|
||||
return category;
|
||||
});
|
||||
|
||||
app.delete("/api/categories/:id", async (request, reply) => {
|
||||
const { id } = request.params as { id: string };
|
||||
const deleted = categoryRepo.deleteCategory(id);
|
||||
if (!deleted) {
|
||||
return reply.code(404).send({ error: "Kategorie nicht gefunden" });
|
||||
}
|
||||
return reply.code(204).send();
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user