diff --git a/app/api/songbooks/route.ts b/app/api/songbooks/route.ts index 7258ec9..6568a67 100644 --- a/app/api/songbooks/route.ts +++ b/app/api/songbooks/route.ts @@ -1,9 +1,5 @@ import { NextResponse } from "next/server"; -import { - getSongBooks, - searchSongBooksByTitle, - createSongBook, -} from "@/lib/db/songbook"; +import { getSongBooks, searchSongBooksByTitle, createSongBook } from "@/lib/db/songbook"; /** @@ -104,21 +100,16 @@ export async function GET(request: Request) { */ export async function POST(request: Request) { try { - const body = await request.json(); - const name = body.name ? String(body.name).trim() : ""; - const language = body.language ? String(body.language).trim() : ""; + const data = await request.json(); - if (!name || !language) { + if (!data.name || !data.language) { return NextResponse.json( { message: "Field 'name' and 'language' are required" }, { status: 400 } ); } - const res = await createSongBook({ - title: name, - language: language, - }); + const res = await createSongBook(data); if (!res.success) { return NextResponse.json( diff --git a/app/api/songs/[id]/route.ts b/app/api/songs/[id]/route.ts index 7b43176..3305a2f 100644 --- a/app/api/songs/[id]/route.ts +++ b/app/api/songs/[id]/route.ts @@ -1,15 +1,16 @@ import { NextResponse } from "next/server"; -import { getSongInfos } from "@/lib/db/managing"; +import { getSongById, updateSong, deleteSong } from "@/lib/db/song"; -export async function GET( - request: Request, - { params }: { params: Promise<{ id: string }> } -) { +export async function GET(request: Request, { params }) { try { const { id } = await params; - const songInfos = await getSongInfos(id); - - return NextResponse.json(songInfos || []); + const res = await getSongById(id); + + if (!res.success) { + return NextResponse.json({ message: res.error }, { status: res.status }); + } + + return NextResponse.json(res.song, { status: res.status }); } catch (error) { console.error("API Error:", error); return NextResponse.json( diff --git a/app/api/songs/route.ts b/app/api/songs/route.ts index 97f3a4c..ca0e9fd 100644 --- a/app/api/songs/route.ts +++ b/app/api/songs/route.ts @@ -1,31 +1,120 @@ import { NextResponse } from "next/server"; -import { getSongs, addSongToSongbook } from "@/lib/db/managing"; +import { getSongs, searchSongsByTitle, createSong, } from "@/lib/db/song"; -export async function GET() { - const songs = await getSongs(); - if (songs) { - return NextResponse.json(songs); - } else { - return NextResponse.json({ success: false}, { status: 500}) + +/** + * @openapi + * /api/songs: + * get: + * summary: Get all songs + * responses: + * 200: + * description: List of songs + * 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"); + + + if (query) { + const res = await searchSongsByTitle(query/*, 20*/); // You can enter a limit (default: 20) + + if (!res.success) { + return NextResponse.json({ message: res.error }, { status: res.status }); + } + + return NextResponse.json(res.songBooks, { status: res.status }); + } + + const res = await getSongs(); + + if (!res.success) { + return NextResponse.json({ message: res.error }, { status: res.status }); + } + + return NextResponse.json(res.songs, { status: res.status }); + + } catch (error) { + console.error("POST /api/songs error:", error); + return NextResponse.json( + { message: "Internal Server Error" }, + { status: 500 } + ); } } + + +/** + * @openapi + * /api/songs: + * post: + * summary: Creates a new song + * requestBody: + * required: true + * content: + * application/json: + * schema: + * type: object + * required: + * - name + * properties: + * name: + * type: string + * example: "Großer Gott" + * 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: "Großer Gott" + * 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 { title, songbookId, number } = await request.json(); + const data = await request.json(); - if (!title || !songbookId || !number) { - return NextResponse.json({ success: false, message: "Fields incomplete"}, { status: 400 }); + if (!data.title) { + return NextResponse.json({ message: "Fields incomplete"}, { status: 400 }); } - const result = await addSongToSongbook(title, Number(songbookId), String(number)); - if (result.success) { - return NextResponse.json({ success: true }); - } else { - return NextResponse.json({ success: false }, { status: 500 }); + const res = await createSong(data); + + if (!res.success) { + return NextResponse.json( + { message: res.error }, + { status: res.status } + ); } + return NextResponse.json(res, { status: res.status }); } catch (error) { - return NextResponse.json({ success: false }, { status: 500 }); + console.error("POST /api/songs error:", error); + return NextResponse.json( + { message: "Internal Server Error" }, + { status: 500 } + ); } } \ No newline at end of file diff --git a/lib/db/song.ts b/lib/db/song.ts index e5734e5..88903d6 100644 --- a/lib/db/song.ts +++ b/lib/db/song.ts @@ -122,7 +122,7 @@ export async function createSong(data: { title: string }) { try{ const song = await prisma.song.create({ data: { - title: data.title + title: data.title.trim() }, select: { id: true, diff --git a/lib/db/songbook.ts b/lib/db/songbook.ts index e37604e..b13d68d 100644 --- a/lib/db/songbook.ts +++ b/lib/db/songbook.ts @@ -131,8 +131,8 @@ export async function createSongBook(data: { title: string; language: string }) try { const songBook = await prisma.songBook.create({ data: { - title: data.title, - language: data.language, + title: data.title.trim(), + language: data.language.trim(), }, select: { id: true, @@ -169,11 +169,11 @@ export async function updateSongBook( const updateData: { title?: string; language?: string } = {}; if (data.title) { - updateData.title = data.title; + updateData.title = data.title.trim(); } if (data.language) { - updateData.language = data.language; + updateData.language = data.language.trim(); } // If no fields to update were provided