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

92 lines
2.6 KiB
TypeScript

// @/app/api/songbooks/[songBookId]/songs/route.ts
import { NextResponse } from "next/server";
import { getSongByNumber } from "@/lib/db/songbooksongs";
interface RouteParams {
params: Promise<{
songBookId: string;
}>;
};
/**
* @openapi
* /api/songbooks/{songBookId}/songs:
* get:
* summary: Einen Song nach Nummer in einem Songbook suchen
* tags:
* - Songbooks
* parameters:
* - in: path
* name: songBookId
* required: true
* schema:
* type: integer
* description: Die ID des Songbooks
* example: 1
* - in: query
* name: number
* required: true
* schema:
* type: string
* description: Die Nummer des Songs im Songbook
* example: "123"
* - in: query
* name: n
* required: false
* schema:
* type: string
* description: Alias für `number`
* example: "123"
* responses:
* 200:
* description: Song im Songbook erfolgreich gefunden
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/SongBookEntry'
* 400:
* description: Ungültige Songbook-ID oder fehlender number-Parameter
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 404:
* description: Songbook oder Song 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 { searchParams } = new URL(request.url);
const number = searchParams.get("number") || searchParams.get("n");
if(number){
const res = await getSongByNumber(parseInt(songBookId), number);
if (!res.success) {
return NextResponse.json({ message: res.error }, { status: res.status });
}
return NextResponse.json(res.entry, { status: res.status });
}
return NextResponse.json({ message: "Need a number query (number=123)" }, { status: 400 });
} catch (error) {
console.error("API Error:", error);
return NextResponse.json(
{ error: "Fehler beim Laden der Songbookinfos" },
{ status: 500 }
);
}
}