// @/app/api/songbooks/[songBookId]/songs/[songId]/route.ts import { NextResponse } from "next/server"; import { addSongToBook, updateSongInBook, removeSongFromBook } from "@/lib/db/songbooksongs"; interface RouteParams { params: Promise<{ songBookId: string; songId: string; }>; }; /** * @openapi * /api/songbooks/{songBookId}/songs/{songId}: * post: * summary: Song zu Liederbuch hinzufügen * tags: * - Songbooks * parameters: * - in: path * name: songBookId * required: true * schema: * type: integer * description: Die ID des Liederbuchs * example: 1 * - in: path * name: songId * required: true * schema: * type: integer * description: Die ID des Songs * example: 1 * requestBody: * required: true * content: * application/json: * schema: * type: object * required: * - number * properties: * number: * type: string * example: "104a" * description: Die Songnummer im Liederbuch * responses: * 200: * description: Song erfolgreich zugewiesen * content: * application/json: * schema: * $ref: '#/components/schemas/SongBookEntry' * 400: * description: Ungültige Parameter oder fehlende Songnummer * content: * application/json: * schema: * $ref: '#/components/schemas/Error' * 409: * description: Songnummer bereits vergeben oder Song existiert bereits im Buch * content: * application/json: * schema: * $ref: '#/components/schemas/Error' * 500: * description: Serverfehler * content: * application/json: * schema: * $ref: '#/components/schemas/Error' */ export async function POST(request: Request, { params }: RouteParams) { try { const { songBookId, songId } = await params; const data = await request.json(); const res = await addSongToBook(parseInt(songId), parseInt(songBookId), String(data.number)); if (!res.success) { return NextResponse.json({ message: res.error }, { status: res.status }); } return NextResponse.json(res.entry, { status: res.status }); } catch (error) { console.error("API Error:", error); return NextResponse.json( { error: "Fehler beim Zuweisen des Songs" }, { status: 500 } ); } } /** * @openapi * /api/songbooks/{songBookId}/songs/{songId}: * patch: * summary: Songnummer eines Songs im Liederbuch aktualisieren * tags: * - Songbooks * parameters: * - in: path * name: songBookId * required: true * schema: * type: integer * description: Die ID des Liederbuchs * example: 1 * - in: path * name: songId * required: true * schema: * type: integer * description: Die ID des Songs * example: 1 * requestBody: * required: true * content: * application/json: * schema: * type: object * required: * - number * properties: * number: * type: string * example: "105" * description: Die neue Songnummer im Liederbuch * responses: * 200: * description: Songnummer erfolgreich aktualisiert * content: * application/json: * schema: * $ref: '#/components/schemas/SongBookEntry' * 400: * description: Ungültige Parameter oder fehlende Daten * content: * application/json: * schema: * $ref: '#/components/schemas/Error' * 404: * description: Zuweisung nicht gefunden * content: * application/json: * schema: * $ref: '#/components/schemas/Error' * 409: * description: Neue Songnummer bereits vergeben * content: * application/json: * schema: * $ref: '#/components/schemas/Error' * 500: * description: Serverfehler * content: * application/json: * schema: * $ref: '#/components/schemas/Error' */ export async function PATCH(request: Request, { params }: RouteParams) { try { const { songBookId, songId } = await params; const data = await request.json(); const res = await updateSongInBook(parseInt(songId), parseInt(songBookId), data); if (!res.success) { return NextResponse.json({ message: res.error }, { status: res.status }); } return NextResponse.json(res.entry, { status: res.status }); } catch (error) { console.error("API Error:", error); return NextResponse.json( { error: "Fehler beim Aktualisieren der Songnummer" }, { status: 500 } ); } } /** * @openapi * /api/songbooks/{songBookId}/songs/{songId}: * delete: * summary: Song aus Liederbuch entfernen * tags: * - Songbooks * parameters: * - in: path * name: songBookId * required: true * schema: * type: integer * description: Die ID des Liederbuchs * example: 1 * - in: path * name: songId * required: true * schema: * type: integer * description: Die ID des Songs * example: 1 * responses: * 200: * description: Song erfolgreich aus dem Liederbuch entfernt (gibt die entfernte Zuweisung zurück) * content: * application/json: * schema: * $ref: '#/components/schemas/SongBookEntry' * 400: * description: Ungültige Parameter * content: * application/json: * schema: * $ref: '#/components/schemas/Error' * 404: * description: Zuweisung nicht gefunden * content: * application/json: * schema: * $ref: '#/components/schemas/Error' * 500: * description: Serverfehler * content: * application/json: * schema: * $ref: '#/components/schemas/Error' */ export async function DELETE(_request: Request, { params }: RouteParams) { try { const { songBookId, songId } = await params; const res = await removeSongFromBook(parseInt(songId), parseInt(songBookId)); if (!res.success) { return NextResponse.json({ message: res.error }, { status: res.status }); } return NextResponse.json(res.entry, { status: res.status }); } catch (error) { console.error("API Error:", error); return NextResponse.json( { error: "Fehler beim Entfernen des Songs" }, { status: 500 } ); } }