From 9d8c7428e6d622260c3c406599693e3429df3a4e Mon Sep 17 00:00:00 2001 From: Mayo Date: Fri, 31 Jul 2026 11:25:18 +0200 Subject: [PATCH] refactor: /api/songbooks --- app/api/songbooks/route.ts | 85 ++++++++++++++++++++++++++++++++++---- 1 file changed, 77 insertions(+), 8 deletions(-) diff --git a/app/api/songbooks/route.ts b/app/api/songbooks/route.ts index d51fa62..06089d4 100644 --- a/app/api/songbooks/route.ts +++ b/app/api/songbooks/route.ts @@ -24,7 +24,7 @@ export async function GET(request: Request) { // Wenn ein Suchbegriff übergeben wurde -> Sucheinbindung if (query) { - const res = await searchSongBookByTitle(query); + const res = await searchSongBookByTitle(query); // You can enter a limit if (!res.success) { return NextResponse.json({ message: res.error }, { status: 500 }); @@ -52,19 +52,88 @@ export async function GET(request: Request) { } - +/** + * @openapi + * /api/songbooks: + * post: + * summary: Creates a new songbook + * requestBody: + * required: true + * content: + * application/json: + * schema: + * type: object + * required: + * - name + * properties: + * name: + * type: string + * example: "Feiert Jesus 1" + * language: + * type: string + * example: "DE" + * responses: + * 201: + * description: Songbook successfully created + * content: + * application/json: + * schema: + * type: object + * properties: + * success: + * type: boolean + * example: true + * songBook: + * type: object + * properties: + * id: + * type: integer + * example: 1 + * title: + * type: string + * example: "Feiert Jesus 1" + * language: + * type: string + * example: "DE" + * 400: + * description: Bad Request (missing name) + * 409: + * description: Conflict (title already exists) + * 500: + * description: Internal Server Error + */ export async function POST(request: Request) { try { - const { name, language } = await request.json(); + const body = await request.json(); + const name = body.name ? String(body.name).trim() : ""; + const language = body.language ? String(body.language).trim() : ""; - if (!name ) { - return NextResponse.json({ success: false, message: "Fields incomplete (name)"}, { status: 400 }) + if (!name || !language) { + return NextResponse.json( + { message: "Field 'name' and 'language' are required" }, + { status: 400 } + ); } - const newBook = await createSongBook({String(name), String(language)}) + const res = await createSongBook({ + title: name, + language: language, + }); + + if (!res.success) { + return NextResponse.json( + { message: res.error }, + { status: res.status } // ALERT-TODO Implement!!! + ); + } + + return NextResponse.json(res, { status: 201 }); - return NextResponse.json(newBook, { status: 201 }) } catch (error) { - return NextResponse.json({ success: false }, { status: 500 }) + console.error("POST /api/songbooks error:", error); + return NextResponse.json( + { message: "Internal Server Error" }, + { status: 500 } + ); } }