140 lines
3.8 KiB
TypeScript
140 lines
3.8 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import {
|
|
getSongBooks,
|
|
searchSongBooksByTitle,
|
|
createSongBook,
|
|
} from "@/lib/db/songbook";
|
|
|
|
|
|
/**
|
|
* @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 { searchParams } = new URL(request.url);
|
|
const query = searchParams.get("query") || searchParams.get("q");
|
|
|
|
// Wenn ein Suchbegriff übergeben wurde -> Sucheinbindung
|
|
if (query) {
|
|
const res = await searchSongBooksByTitle(query); // You can enter a limit
|
|
|
|
if (!res.success) {
|
|
return NextResponse.json({ message: res.error }, { status: res.status });
|
|
}
|
|
|
|
return NextResponse.json(res.songBooks, { status: res.status };
|
|
}
|
|
|
|
// Ansonsten alle Bücher abrufen
|
|
const res = await getSongBooks();
|
|
|
|
if (!res.success) {
|
|
return NextResponse.json({ message: res.error }, { status: res.status });
|
|
}
|
|
|
|
return NextResponse.json(res.songBooks, { status: res.status });
|
|
|
|
} catch (error) {
|
|
console.error("GET /api/songbooks error:", error);
|
|
return NextResponse.json(
|
|
{ message: "Internal Server Error" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @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 body = await request.json();
|
|
const name = body.name ? String(body.name).trim() : "";
|
|
const language = body.language ? String(body.language).trim() : "";
|
|
|
|
if (!name || !language) {
|
|
return NextResponse.json(
|
|
{ message: "Field 'name' and 'language' are required" },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
const res = await createSongBook({
|
|
title: name,
|
|
language: language,
|
|
});
|
|
|
|
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/songbooks error:", error);
|
|
return NextResponse.json(
|
|
{ message: "Internal Server Error" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|