Files
2026-10-13 19:26:56 +02:00

208 lines
5.7 KiB
TypeScript

// @/app/api/songbooks/[songBookId]/route.ts
import { NextResponse } from "next/server";
import { getSongBookById, updateSongBook, deleteSongBook } from "@/lib/db/songbook";
interface RouteParams {
params: Promise<{
songBookId: string;
}>;
};
/**
* @openapi
* /api/songbooks/{songBookId}:
* get:
* summary: Einzelnes Songbook abrufen
* tags:
* - Songbooks
* parameters:
* - in: path
* name: songBookId
* required: true
* schema:
* type: integer
* description: Die ID des Songbooks
* example: 1
* responses:
* 200:
* description: Songbook erfolgreich gefunden
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/SongBook'
* 400:
* description: Ungültige Songbook-ID
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 404:
* description: Songbook nicht gefunden
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 500:
* description: Serverfehler
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
*/
export async function GET(_request: Request, { params }: RouteParams) {
try {
const { songBookId } = await params;
const res = await getSongBookById(parseInt(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/{songBookId}:
* patch:
* summary: Songbook aktualisieren
* tags:
* - Songbooks
* parameters:
* - in: path
* name: songBookId
* required: true
* schema:
* type: integer
* description: Die ID des Songbooks
* example: 1
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* properties:
* title:
* type: string
* example: "Feiert Jesus 1"
* language:
* type: string
* example: "DE"
* responses:
* 200:
* description: Songbook erfolgreich aktualisiert
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/SongBook'
* 400:
* description: Ungültige Parameter oder Daten
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 404:
* description: Songbook nicht gefunden
* 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 } = await params;
const data = await request.json();
const res = await updateSongBook(parseInt(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 Aktualisieren des Songbooks" },
{ status: 500 }
);
}
}
/**
* @openapi
* /api/songbooks/{songBookId}:
* delete:
* summary: Songbook löschen
* tags:
* - Songbooks
* parameters:
* - in: path
* name: songBookId
* required: true
* schema:
* type: integer
* description: Die ID des Songbooks
* example: 1
* responses:
* 200:
* description: Songbook erfolgreich gelöscht (gibt das gelöschte Songbook zurück)
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/SongBook'
* 400:
* description: Ungültige Songbook-ID
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 404:
* description: Songbook 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 } = await params;
const res = await deleteSongBook(parseInt(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 Löschen des Songbooks" },
{ status: 500 }
);
}
}