Some API changes in Song. Added '.trim()' for better string processing
This commit is contained in:
1 parent
61f1eb5a2c
commit
66c9dc28fc
5 files changed
+122
-41
No files matched your search
@@ -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(
|
||||
|
||||
@@ -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);
|
||||
const res = await getSongById(id);
|
||||
|
||||
return NextResponse.json(songInfos || []);
|
||||
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
@@ -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 }
|
||||
);
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -122,7 +122,7 @@ export async function createSong(data: { title: string }) {
|
||||
try{
|
||||
const song = await prisma.song.create({
|
||||
data: {
|
||||
title: data.title
|
||||
title: data.title.trim()
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
|
||||
+4
-4
@@ -131,8 +131,8 @@ export async function createSongBook(data: { title: string; language: string })
|
||||
try {
|
||||
const songBook = await prisma.songBook.create({
|
||||
data: {
|
||||
title: data.title,
|
||||
language: data.language,
|
||||
title: data.title.trim(),
|
||||
language: data.language.trim(),
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
@@ -169,11 +169,11 @@ export async function updateSongBook(
|
||||
const updateData: { title?: string; language?: string } = {};
|
||||
|
||||
if (data.title) {
|
||||
updateData.title = data.title;
|
||||
updateData.title = data.title.trim();
|
||||
}
|
||||
|
||||
if (data.language) {
|
||||
updateData.language = data.language;
|
||||
updateData.language = data.language.trim();
|
||||
}
|
||||
|
||||
// If no fields to update were provided
|
||||
|
||||
Reference in new issue
Block a user