Some API changes in Song. Added '.trim()' for better string processing
This commit is contained in:
1 parent
61f1eb5a2c
commit
66c9dc28fc
5 files changed
+123
-42
No files matched your search
+105
-16
@@ -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 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user