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

147 lines
3.7 KiB
TypeScript

import { NextResponse } from "next/server";
import { getSongBookById, updateSongBook, deleteSongBook } from "@/lib/db/songbook";
// import { parseInt } from "@/lib/helper/checkvar";
/**
* @openapi
* /api/songbooks/{id}:
* get:
* summary: Einzelnen Songbook abrufen
* tags:
* - Songbooks
* parameters:
* - in: path
* name: id
* required: true
* schema:
* type: integer
* description: Die ID des Songbooks
* responses:
* 200:
* description: Songbook erfolgreich gefunden
* 400:
* description: Ungültige Songbook-ID
* 404:
* description: Songbook nicht gefunden
* 500:
* description: Serverfehler
*/
export async function GET(request: Request, { params }) {
try {
const { songBookId } = await params;
const res = await getSongBookById(songBookId);
if (!res.success) {
return NextResponse.json({ message: res.error }, { status: res.status });
}
return NextResponse.json(res.songBook, { status: res.status });
} catch (error) {
console.error("API Error:", error);
return NextResponse.json(
{ error: "Fehler beim Laden der Songbookinfos" },
{ status: 500 }
);
}
}
/**
* @openapi
* /api/songbooks/{id}:
* patch:
* summary: Songbook aktualisieren
* tags:
* - Songbooks
* parameters:
* - in: path
* name: id
* required: true
* schema:
* type: integer
* description: Die ID des Songbooks
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* responses:
* 200:
* description: Songbook erfolgreich aktualisiert
* 400:
* description: Ungültige Parameter oder Daten
* 404:
* description: Songbook nicht gefunden
* 500:
* description: Serverfehler
*/
export async function PATCH(request: Request, { params }) {
try {
const { songBookId } = await params;
const data = await request.json();
const res = await updateSongBook(songBookId, data);
if (!res.success) {
return NextResponse.json({ message: res.error }, { status: res.status });
}
return NextResponse.json(res.songBook, { status: res.status });
} catch (error) {
console.error("API Error:", error);
return NextResponse.json(
{ error: "Fehler beim Laden der Songbookinfos" },
{ status: 500 }
);
}
}
/**
* @openapi
* /api/songbooks/{id}:
* delete:
* summary: Songbook löschen
* tags:
* - Songbooks
* parameters:
* - in: path
* name: id
* required: true
* schema:
* type: integer
* description: Die ID des Songbooks
* responses:
* 200:
* description: Songbook erfolgreich gelöscht
* 400:
* description: Ungültige Songbook-ID
* 404:
* description: Songbook nicht gefunden
* 500:
* description: Serverfehler
*/
export async function DELETE(request: Request, { params }) {
try {
const { songBookId } = await params;
const res = await deleteSongBook(songBookId);
if (!res.success) {
return NextResponse.json({ message: res.error }, { status: res.status });
}
return NextResponse.json(res.songBook, { status: res.status });
} catch (error) {
console.error("API Error:", error);
return NextResponse.json(
{ error: "Fehler beim Laden der Songbookinfos" },
{ status: 500 }
);
}
}