Files
psalmtafel-webservice/app/api/songbooks/route.ts
T
2026-07-31 11:15:18 +02:00

71 lines
1.8 KiB
TypeScript

import { NextResponse } from "next/server";
import {
getSongBooks,
searchSongBookByTitle,
createSongBook,
} from "@/lib/db/songbook";
/**
* @openapi
* /api/songbooks:
* get:
* summary: Get all songbooks
* responses:
* 200:
* description: List of songbooks
* 500:
* description: Internal Server Error
*/
export async function GET(request: Request) {
try {
const { searchParams } = new URL(request.url);
const query = searchParams.get("query") || searchParams.get("q");
// Wenn ein Suchbegriff übergeben wurde -> Sucheinbindung
if (query) {
const res = await searchSongBookByTitle(query);
if (!res.success) {
return NextResponse.json({ message: res.error }, { status: 500 });
}
return NextResponse.json(res.songBooks, { status: 200 });
}
// Ansonsten alle Bücher abrufen
const res = await getSongBooks();
if (!res.success) {
return NextResponse.json({ message: res.error }, { status: 500 });
}
return NextResponse.json(res.songBooks, { status: 200 });
} catch (error) {
console.error("GET /api/songbooks error:", error);
return NextResponse.json(
{ message: "Internal Server Error" },
{ status: 500 }
);
}
}
export async function POST(request: Request) {
try {
const { name, language } = await request.json();
if (!name ) {
return NextResponse.json({ success: false, message: "Fields incomplete (name)"}, { status: 400 })
}
const newBook = await createSongBook({String(name), String(language)})
return NextResponse.json(newBook, { status: 201 })
} catch (error) {
return NextResponse.json({ success: false }, { status: 500 })
}
}