// @/app/api/songs/route.ts import { NextResponse } from "next/server"; import { getSongs, searchSongsByTitle, createSong } from "@/lib/db/song"; /** * @openapi * /api/songs: * get: * summary: Get all songs or search songs by title * tags: * - Songs * parameters: * - in: query * name: query * required: false * schema: * type: string * description: Search term for the song title * example: "Großer Gott" * - in: query * name: q * required: false * schema: * type: string * description: Alias for `query` * example: "Großer Gott" * responses: * 200: * description: List of songs (filtered by title if a query is given) * content: * application/json: * schema: * type: array * items: * $ref: '#/components/schemas/Song' * 500: * description: Internal Server Error * content: * application/json: * schema: * $ref: '#/components/schemas/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 }); } // Ansonsten alle Songs abrufen 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("GET /api/songs error:", error); return NextResponse.json( { message: "Internal Server Error" }, { status: 500 } ); } } /** * @openapi * /api/songs: * post: * summary: Creates a new song * tags: * - Songs * requestBody: * required: true * content: * application/json: * schema: * type: object * required: * - title * properties: * title: * type: string * example: "Großer Gott" * responses: * 201: * description: Song successfully created * content: * application/json: * schema: * type: object * properties: * success: * type: boolean * example: true * song: * $ref: '#/components/schemas/Song' * 400: * description: Bad Request (missing title) * content: * application/json: * schema: * $ref: '#/components/schemas/Error' * 409: * description: Conflict (title already exists) * content: * application/json: * schema: * $ref: '#/components/schemas/Error' * 500: * description: Internal Server Error * content: * application/json: * schema: * $ref: '#/components/schemas/Error' */ export async function POST(request: Request) { try { const data = await request.json(); if (!data.title) { return NextResponse.json( { message: "Field 'title' is required" }, { status: 400 } ); } 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) { console.error("POST /api/songs error:", error); return NextResponse.json( { message: "Internal Server Error" }, { status: 500 } ); } }