diff --git a/app/api/songbooks/[songBookId]/route.ts b/app/api/songbooks/[songBookId]/route.ts new file mode 100644 index 0000000..9fb47ba --- /dev/null +++ b/app/api/songbooks/[songBookId]/route.ts @@ -0,0 +1,6 @@ +import { NextResponse } from "next/server"; +import { + getSongBookById, + upateSongBook, + deleteSongBook +} from "@/lib/db/songbook"; diff --git a/app/api/songbooks/route.ts b/app/api/songbooks/route.ts index 124a7b3..d51fa62 100644 --- a/app/api/songbooks/route.ts +++ b/app/api/songbooks/route.ts @@ -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 }) } -} \ No newline at end of file +}