Files
psalmtafel-webservice/app/api/songs/route.ts
T
2026-08-27 13:26:50 +02:00

125 lines
3.3 KiB
TypeScript

import { NextResponse } from "next/server";
import { getSongs, searchSongsByTitle, createSong, } from "@/lib/db/song";
/**
* @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 });
}
// 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("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 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 }
);
}
}