Some API changes in Song. Added '.trim()' for better string processing

This commit is contained in:
rudi committed 2026-08-03 08:16:55 +02:00
1 parent 61f1eb5a2c
commit 66c9dc28fc
5 files changed
+123 -42

No files matched your search

+4 -13
View File
@@ -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(
+9 -8
View File
@@ -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(
+105 -16
View File
@@ -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 }
);
}
}