Some Route.js changes

This commit is contained in:
rudi committed 2026-07-31 11:15:18 +02:00
1 parent cb18b0e14f
commit 69378ca439
2 files changed
+56 -7

No files matched your search

+50 -7
View File
@@ -1,15 +1,58 @@
import { NextResponse } from "next/server";
import { getSongbooks, addSongbook } from "@/lib/db/managing";
import {
getSongBooks,
searchSongBookByTitle,
createSongBook,
} from "@/lib/db/songbook";
export async function GET() {
/**
* @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 songbooks = await getSongbooks();
return NextResponse.json(songbooks);
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) {
return NextResponse.json({ success: false, message: error}, { status: 500})
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();
@@ -18,10 +61,10 @@ export async function POST(request: Request) {
return NextResponse.json({ success: false, message: "Fields incomplete (name)"}, { status: 400 })
}
const newBook = await addSongbook(String(name), String(language))
const newBook = await createSongBook({String(name), String(language)})
return NextResponse.json(newBook, { status: 201 })
} catch (error) {
return NextResponse.json({ success: false }, { status: 500 })
}
}
}