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
@@ -1,9 +1,5 @@
|
|||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import {
|
import { getSongBooks, searchSongBooksByTitle, createSongBook } from "@/lib/db/songbook";
|
||||||
getSongBooks,
|
|
||||||
searchSongBooksByTitle,
|
|
||||||
createSongBook,
|
|
||||||
} from "@/lib/db/songbook";
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -104,21 +100,16 @@ export async function GET(request: Request) {
|
|||||||
*/
|
*/
|
||||||
export async function POST(request: Request) {
|
export async function POST(request: Request) {
|
||||||
try {
|
try {
|
||||||
const body = await request.json();
|
const data = await request.json();
|
||||||
const name = body.name ? String(body.name).trim() : "";
|
|
||||||
const language = body.language ? String(body.language).trim() : "";
|
|
||||||
|
|
||||||
if (!name || !language) {
|
if (!data.name || !data.language) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ message: "Field 'name' and 'language' are required" },
|
{ message: "Field 'name' and 'language' are required" },
|
||||||
{ status: 400 }
|
{ status: 400 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const res = await createSongBook({
|
const res = await createSongBook(data);
|
||||||
title: name,
|
|
||||||
language: language,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!res.success) {
|
if (!res.success) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
|
|||||||
@@ -1,15 +1,16 @@
|
|||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import { getSongInfos } from "@/lib/db/managing";
|
import { getSongById, updateSong, deleteSong } from "@/lib/db/song";
|
||||||
|
|
||||||
export async function GET(
|
export async function GET(request: Request, { params }) {
|
||||||
request: Request,
|
|
||||||
{ params }: { params: Promise<{ id: string }> }
|
|
||||||
) {
|
|
||||||
try {
|
try {
|
||||||
const { id } = await params;
|
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) {
|
} catch (error) {
|
||||||
console.error("API Error:", error);
|
console.error("API Error:", error);
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
|
|||||||
+105
-16
@@ -1,31 +1,120 @@
|
|||||||
import { NextResponse } from "next/server";
|
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) {
|
* @openapi
|
||||||
return NextResponse.json(songs);
|
* /api/songs:
|
||||||
} else {
|
* get:
|
||||||
return NextResponse.json({ success: false}, { status: 500})
|
* 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) {
|
export async function POST(request: Request) {
|
||||||
try {
|
try {
|
||||||
const { title, songbookId, number } = await request.json();
|
const data = await request.json();
|
||||||
|
|
||||||
if (!title || !songbookId || !number) {
|
if (!data.title) {
|
||||||
return NextResponse.json({ success: false, message: "Fields incomplete"}, { status: 400 });
|
return NextResponse.json({ message: "Fields incomplete"}, { status: 400 });
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = await addSongToSongbook(title, Number(songbookId), String(number));
|
const res = await createSong(data);
|
||||||
if (result.success) {
|
|
||||||
return NextResponse.json({ success: true });
|
if (!res.success) {
|
||||||
} else {
|
return NextResponse.json(
|
||||||
return NextResponse.json({ success: false }, { status: 500 });
|
{ message: res.error },
|
||||||
|
{ status: res.status }
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return NextResponse.json(res, { status: res.status });
|
||||||
} catch (error) {
|
} 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{
|
try{
|
||||||
const song = await prisma.song.create({
|
const song = await prisma.song.create({
|
||||||
data: {
|
data: {
|
||||||
title: data.title
|
title: data.title.trim()
|
||||||
},
|
},
|
||||||
select: {
|
select: {
|
||||||
id: true,
|
id: true,
|
||||||
|
|||||||
+4
-4
@@ -131,8 +131,8 @@ export async function createSongBook(data: { title: string; language: string })
|
|||||||
try {
|
try {
|
||||||
const songBook = await prisma.songBook.create({
|
const songBook = await prisma.songBook.create({
|
||||||
data: {
|
data: {
|
||||||
title: data.title,
|
title: data.title.trim(),
|
||||||
language: data.language,
|
language: data.language.trim(),
|
||||||
},
|
},
|
||||||
select: {
|
select: {
|
||||||
id: true,
|
id: true,
|
||||||
@@ -169,11 +169,11 @@ export async function updateSongBook(
|
|||||||
const updateData: { title?: string; language?: string } = {};
|
const updateData: { title?: string; language?: string } = {};
|
||||||
|
|
||||||
if (data.title) {
|
if (data.title) {
|
||||||
updateData.title = data.title;
|
updateData.title = data.title.trim();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data.language) {
|
if (data.language) {
|
||||||
updateData.language = data.language;
|
updateData.language = data.language.trim();
|
||||||
}
|
}
|
||||||
|
|
||||||
// If no fields to update were provided
|
// If no fields to update were provided
|
||||||
|
|||||||
Reference in new issue
Block a user