Files
psalmtafel-webservice/app/api/songbooks/route.ts
T
2026-10-13 19:26:56 +02:00

166 lines
4.6 KiB
TypeScript

// @/app/api/songbooks/route.ts
import { NextResponse } from "next/server";
import { getSongBooks, searchSongBooksByTitle, createSongBook } from "@/lib/db/songbook";
/**
* @openapi
* /api/songbooks:
* get:
* summary: Get all songbooks or search songbooks by title
* tags:
* - Songbooks
* parameters:
* - in: query
* name: query
* required: false
* schema:
* type: string
* description: Search term for the songbook title
* example: "Feiert Jesus"
* - in: query
* name: q
* required: false
* schema:
* type: string
* description: Alias for `query`
* example: "Feiert Jesus"
* responses:
* 200:
* description: List of songbooks (filtered by title if a query is given)
* content:
* application/json:
* schema:
* type: array
* items:
* $ref: '#/components/schemas/SongBook'
* 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");
// 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
* tags:
* - Songbooks
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required:
* - title
* - language
* properties:
* title:
* 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:
* $ref: '#/components/schemas/SongBook'
* 400:
* description: Bad Request (missing title or language)
* 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 || !data.language) {
return NextResponse.json(
{ message: "Field 'title' and 'language' are required" },
{ status: 400 }
);
}
const res = await createSongBook(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/songbooks error:", error);
return NextResponse.json(
{ message: "Internal Server Error" },
{ status: 500 }
);
}
}