Files
psalmtafel-webservice/app/api/songbooks/[songBookId]/songs/[songId]/route.ts
T
2026-08-27 14:59:19 +02:00

187 lines
5.2 KiB
TypeScript

import { NextResponse } from "next/server";
import { addSongToBook, updateSongInBook, removeSongFromBook } from "@/lib/db/songbooksongs";
// import { parseInt } from "@/lib/helper/checkvar";
/**
* @openapi
* /api/songbooks/{songBookId}/songs/{songId}:
* post:
* summary: Song zu Liederbuch hinzufügen
* tags:
* - Songbook
* parameters:
* - in: path
* name: songBookId
* required: true
* schema:
* type: integer
* description: Die ID des Liederbuchs
* - in: path
* name: songId
* required: true
* schema:
* type: integer
* description: Die ID des Songs
* 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
* 400:
* description: Ungültige Parameter oder fehlende Songnummer
* 409:
* description: Songnummer bereits vergeben oder Song existiert bereits im Buch
* 500:
* description: Serverfehler
*/
export async function POST(request: Request, { params }) {
try {
const { songBookId, songId } = await params;
const data = await request.json();
const res = await addSongToBook(songId, songBookId, 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 Laden der Songbookinfos" },
{ status: 500 }
);
}
}
/**
* @openapi
* /api/songbooks/{songBookId}/songs/{songId}:
* patch:
* summary: Songnummer eines Songs im Liederbuch aktualisieren
* tags:
* - Songbook
* parameters:
* - in: path
* name: songBookId
* required: true
* schema:
* type: integer
* description: Die ID des Liederbuchs
* - in: path
* name: songId
* required: true
* schema:
* type: integer
* description: Die ID des Songs
* 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
* 400:
* description: Ungültige Parameter oder fehlende Daten
* 404:
* description: Zuweisung nicht gefunden
* 409:
* description: Neue Songnummer bereits vergeben
* 500:
* description: Serverfehler
*/
export async function PATCH(request: Request, { params }) {
try {
const { songBookId, songId } = await params;
const data = await request.json();
const res = await updateSongInBook(songId, 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 Laden der Songbookinfos" },
{ status: 500 }
);
}
}
/**
* @openapi
* /api/songbooks/{songBookId}/songs/{songId}:
* delete:
* summary: Song aus Liederbuch entfernen
* tags:
* - Songbook
* parameters:
* - in: path
* name: songBookId
* required: true
* schema:
* type: integer
* description: Die ID des Liederbuchs
* - in: path
* name: songId
* required: true
* schema:
* type: integer
* description: Die ID des Songs
* responses:
* 200:
* description: Song erfolgreich aus dem Liederbuch entfernt
* 400:
* description: Ungültige Parameter
* 404:
* description: Zuweisung nicht gefunden
* 500:
* description: Serverfehler
*/
export async function DELETE(request: Request, { params }) {
try {
const { songBookId, songId } = await params;
const res = await removeSongFromBook(songId, 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 Laden der Songbookinfos" },
{ status: 500 }
);
}
}