API work on SongbookSongs

This commit is contained in:
Rudi committed 2026-08-27 14:59:19 +02:00
1 parent 4bdf03426c
commit b060ab29cf
10 files changed
+344 -116

No files matched your search

+12 -7
View File
@@ -1,5 +1,5 @@
import { NextResponse } from "next/server";
import { getSongBookById, upateSongBook, deleteSongBook } from "@/lib/db/songbook";
import { getSongBookById, updateSongBook, deleteSongBook } from "@/lib/db/songbook";
// import { parseInt } from "@/lib/helper/checkvar";
/**
@@ -26,10 +26,15 @@ import { getSongBookById, upateSongBook, deleteSongBook } from "@/lib/db/songboo
* 500:
* description: Serverfehler
*/
export async function GET(request: Request, { params }) {
try {
const { id } = await params;
const res = await getSongBookById(id);
const { songBookId } = await params;
const res = await getSongBookById(songBookId);
if (!res.success) {
return NextResponse.json({ message: res.error }, { status: res.status });
@@ -78,9 +83,9 @@ export async function GET(request: Request, { params }) {
*/
export async function PATCH(request: Request, { params }) {
try {
const { id } = await params;
const { songBookId } = await params;
const data = await request.json();
const res = await upateSongBook(id, data);
const res = await updateSongBook(songBookId, data);
if (!res.success) {
return NextResponse.json({ message: res.error }, { status: res.status });
@@ -123,8 +128,8 @@ export async function PATCH(request: Request, { params }) {
*/
export async function DELETE(request: Request, { params }) {
try {
const { id } = await params;
const res = await deleteSongBook(id);
const { songBookId } = await params;
const res = await deleteSongBook(songBookId);
if (!res.success) {
return NextResponse.json({ message: res.error }, { status: res.status });
@@ -0,0 +1,186 @@
import { NextResponse } from "next/server";
import { addSongToBook, updateSongInBook, removeSongFromBook } from "@/lib/db/songbooksongs";
// import { parseInt } from "@/lib/helper/checkvar";
/**
* @openapi
* /api/songbooks/{songBookId}/songs/{songId}:
* post:
* summary: Song zu Liederbuch hinzufügen
* tags:
* - Songbook
* parameters:
* - in: path
* name: songBookId
* required: true
* schema:
* type: integer
* description: Die ID des Liederbuchs
* - in: path
* name: songId
* required: true
* schema:
* type: integer
* description: Die ID des Songs
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required:
* - number
* properties:
* number:
* type: string
* example: "104a"
* description: Die Songnummer im Liederbuch
* responses:
* 200:
* description: Song erfolgreich zugewiesen
* 400:
* description: Ungültige Parameter oder fehlende Songnummer
* 409:
* description: Songnummer bereits vergeben oder Song existiert bereits im Buch
* 500:
* description: Serverfehler
*/
export async function POST(request: Request, { params }) {
try {
const { songBookId, songId } = await params;
const data = await request.json();
const res = await addSongToBook(songId, songBookId, data.number);
if (!res.success) {
return NextResponse.json({ message: res.error }, { status: res.status });
}
return NextResponse.json(res.entry, { status: res.status });
} catch (error) {
console.error("API Error:", error);
return NextResponse.json(
{ error: "Fehler beim Laden der Songbookinfos" },
{ status: 500 }
);
}
}
/**
* @openapi
* /api/songbooks/{songBookId}/songs/{songId}:
* patch:
* summary: Songnummer eines Songs im Liederbuch aktualisieren
* tags:
* - Songbook
* parameters:
* - in: path
* name: songBookId
* required: true
* schema:
* type: integer
* description: Die ID des Liederbuchs
* - in: path
* name: songId
* required: true
* schema:
* type: integer
* description: Die ID des Songs
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required:
* - number
* properties:
* number:
* type: string
* example: "105"
* description: Die neue Songnummer im Liederbuch
* responses:
* 200:
* description: Songnummer erfolgreich aktualisiert
* 400:
* description: Ungültige Parameter oder fehlende Daten
* 404:
* description: Zuweisung nicht gefunden
* 409:
* description: Neue Songnummer bereits vergeben
* 500:
* description: Serverfehler
*/
export async function PATCH(request: Request, { params }) {
try {
const { songBookId, songId } = await params;
const data = await request.json();
const res = await updateSongInBook(songId, songBookId, data);
if (!res.success) {
return NextResponse.json({ message: res.error }, { status: res.status });
}
return NextResponse.json(res.entry, { status: res.status });
} catch (error) {
console.error("API Error:", error);
return NextResponse.json(
{ error: "Fehler beim Laden der Songbookinfos" },
{ status: 500 }
);
}
}
/**
* @openapi
* /api/songbooks/{songBookId}/songs/{songId}:
* delete:
* summary: Song aus Liederbuch entfernen
* tags:
* - Songbook
* parameters:
* - in: path
* name: songBookId
* required: true
* schema:
* type: integer
* description: Die ID des Liederbuchs
* - in: path
* name: songId
* required: true
* schema:
* type: integer
* description: Die ID des Songs
* responses:
* 200:
* description: Song erfolgreich aus dem Liederbuch entfernt
* 400:
* description: Ungültige Parameter
* 404:
* description: Zuweisung nicht gefunden
* 500:
* description: Serverfehler
*/
export async function DELETE(request: Request, { params }) {
try {
const { songBookId, songId } = await params;
const res = await removeSongFromBook(songId, songBookId);
if (!res.success) {
return NextResponse.json({ message: res.error }, { status: res.status });
}
return NextResponse.json(res.entry, { status: res.status });
} catch (error) {
console.error("API Error:", error);
return NextResponse.json(
{ error: "Fehler beim Laden der Songbookinfos" },
{ status: 500 }
);
}
}
@@ -0,0 +1,53 @@
import { NextResponse } from "next/server";
import { getSongByNumber } from "@/lib/db/songbooksongs";
/**
* @openapi
* /api/songbooks/{id}/songs:
* get:
* summary: Einen Song nach Nummer in einem Songbook suchen
* tags:
* - Songbook
* parameters:
* - in: path
* name: id
* required: true
* schema:
* type: integer
* description: Die ID des Songbooks
* responses:
* 200:
* description: Songbook erfolgreich gefunden
* 400:
* description: Ungültige Songbook-ID
* 404:
* description: Songbook nicht gefunden
* 500:
* description: Serverfehler
*/
export async function GET(request: Request, { params }) {
try {
const { songBookId } = await params;
const { searchParams } = new URL(request.url);
const number = searchParams.get("number") || searchParams.get("n");
if(number){
const res = await getSongByNumber(songBookId, number);
if (!res.success) {
return NextResponse.json({ message: res.error }, { status: res.status });
}
return NextResponse.json(res.entry, { status: res.status });
}
return NextResponse.json({ message: "Need a number query (number=123)" }, { status: 400 });
} catch (error) {
console.error("API Error:", error);
return NextResponse.json(
{ error: "Fehler beim Laden der Songbookinfos" },
{ status: 500 }
);
}
}
+8 -5
View File
@@ -2,6 +2,7 @@ import { NextResponse } from "next/server";
import { getSongBooks, searchSongBooksByTitle, createSongBook } from "@/lib/db/songbook";
/**
* @openapi
* /api/songbooks:
@@ -18,7 +19,9 @@ export async function GET(request: Request) {
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
@@ -60,9 +63,9 @@ export async function GET(request: Request) {
* schema:
* type: object
* required:
* - name
* - title
* properties:
* name:
* title:
* type: string
* example: "Feiert Jesus 1"
* language:
@@ -92,7 +95,7 @@ export async function GET(request: Request) {
* type: string
* example: "DE"
* 400:
* description: Bad Request (missing name)
* description: Bad Request (missing title)
* 409:
* description: Conflict (title already exists)
* 500:
@@ -102,9 +105,9 @@ export async function POST(request: Request) {
try {
const data = await request.json();
if (!data.name || !data.language) {
if (!data.title || !data.language) {
return NextResponse.json(
{ message: "Field 'name' and 'language' are required" },
{ message: "Field 'title' and 'language' are required" },
{ status: 400 }
);
}
@@ -29,8 +29,8 @@ import { getSongById, updateSong, deleteSong } from "@/lib/db/song";
*/
export async function GET(request: Request, { params }) {
try {
const { id } = await params;
const res = await getSongById(id);
const { songId } = await params;
const res = await getSongById(songId);
if (!res.success) {
return NextResponse.json({ message: res.error }, { status: res.status });
@@ -79,9 +79,9 @@ export async function GET(request: Request, { params }) {
*/
export async function PATCH(request: Request, { params }) {
try {
const { id } = await params;
const { songId } = await params;
const data = await request.json();
const res = await updateSong(id, data);
const res = await updateSong(songId, data);
if (!res.success) {
return NextResponse.json({ message: res.error }, { status: res.status });
@@ -124,8 +124,8 @@ export async function PATCH(request: Request, { params }) {
*/
export async function DELETE(request: Request, { params }) {
try {
const { id } = await params;
const res = await deleteSong(id);
const { songId } = await params;
const res = await deleteSong(songId);
if (!res.success) {
return NextResponse.json({ message: res.error }, { status: res.status });
-40
View File
@@ -1,40 +0,0 @@
import { NextResponse } from "next/server";
import { attachSongToSongbook } from "@/lib/db/managing";
export async function POST(request: Request) {
try {
const body = await request.json();
const { songId, songbookId, number } = body;
if (isNaN(songId) || isNaN(songbookId) || !number) {
return NextResponse.json(
{ error: "Ungültige IDs oder fehlende Nummer." },
{ status: 400 }
);
}
const result = await attachSongToSongbook(
Number(songId),
Number(songbookId),
String(number)
);
if (result.success) {
return NextResponse.json(
{ message: "Lied erfolgreich verknüpft!" },
{ status: 200 }
);
} else {
return NextResponse.json(
{ error: "Fehler beim Verknüpfen in der Datenbank." },
{ status: 500 }
);
}
} catch (error) {
console.error("API Error in /api/songs/attach:", error);
return NextResponse.json(
{ error: "Ungültiges JSON oder Serverfehler." },
{ status: 500 }
);
}
}
-39
View File
@@ -1,39 +0,0 @@
import { NextResponse } from "next/server";
import { removeSongfromSongbook } from "@/lib/db/managing";
export async function POST(request: Request) {
try {
const body = await request.json();
const { songId, songbookId } = body;
if (isNaN(songId) || isNaN(songbookId)) {
return NextResponse.json(
{ error: "Ungültige IDs." },
{ status: 400 }
);
}
const result = await removeSongfromSongbook(
Number(songId),
Number(songbookId)
);
if (result.success) {
return NextResponse.json(
{ message: "Lied erfolgreich aus dem Liederbuch gelöscht!" },
{ status: 200 }
);
} else {
return NextResponse.json(
{ error: "Fehler beim Löschen aus dem Liederbuch." },
{ status: 500 }
);
}
} catch (error) {
console.error("API Error in /api/songs/remove:", error);
return NextResponse.json(
{ error: "Ungültiges JSON oder Serverfehler." },
{ status: 500 }
);
}
}
+3
View File
@@ -125,6 +125,9 @@ export async function searchSongsByTitle(query: string, limit: number = 20) {
*/
export async function createSong(data: { title: string }) {
try{
if(!data.title){
return { success: false, status: 400, error: "Field 'title' is required" };
}
const song = await prisma.song.create({
data: {
title: data.title.trim()
+20 -4
View File
@@ -1,4 +1,5 @@
import { prisma } from "@/lib/db/prisma";
import { parseInt } from "@/lib/helper/checkvar";
/**
@@ -34,8 +35,12 @@ export async function getSongBooks() {
* @param id - ID indicating the songbook
* @returns {"success": boolean, "songBook": object}
*/
export async function getSongBookById(id: number) {
export async function getSongBookById(rawId: number) {
try {
const id = parseInt(rawId);
if (!id) {
return { success: false, status: 400, error: "Id has to be Integer" };
}
const songBook = await prisma.songBook.findUnique({
where: { id },
select: {
@@ -129,10 +134,13 @@ export async function searchSongBooksByTitle(query: string, limit: number = 20)
*/
export async function createSongBook(data: { title: string; language: string }) {
try {
if(!data.title){
return { success: false, status: 400, error: "Field 'title' is required" };
}
const songBook = await prisma.songBook.create({
data: {
title: data.title.trim(),
language: data.language.trim(),
language: data.language?.trim() ?? "",
},
select: {
id: true,
@@ -161,10 +169,14 @@ export async function createSongBook(data: { title: string; language: string })
* @returns {"success": boolean, "songBook": {"id": number, "title": string, "language": string}}
*/
export async function updateSongBook(
id: number,
rawId: number,
data: { title?: string; language?: string }
) {
try {
const id = parseInt(rawId);
if (!id) {
return { success: false, status: 400, error: "Id has to be Integer" };
}
// Prepare update fields dynamically
const updateData: { title?: string; language?: string } = {};
@@ -211,8 +223,12 @@ export async function updateSongBook(
* @param id - ID indicating the songbook
* @returns {"success": boolean, "songBook": object}
*/
export async function deleteSongBook(id: number) {
export async function deleteSongBook(rawId: number) {
try {
const id = parseInt(rawId);
if (!id) {
return { success: false, status: 400, error: "Id has to be Integer" };
}
const songBook = await prisma.songBook.delete({
where: { id },
select: {
+56 -15
View File
@@ -1,10 +1,11 @@
import { prisma } from "@/lib/db/prisma";
import { parseInt } from "@/lib/helper/checkvar";
export type DbResponse =
| { success: true; status: number, entry: {
songId: number | undefined;
songBookId: number | undefined;
songNumber: number | undefined
songNumber: string | undefined
}, song: {id: number, title: string} | undefined }
| { success: false; status: number; error: string };
@@ -17,8 +18,20 @@ export type DbResponse =
* @param songNumber - Track/Song number in that specific book
* @returns {"success": boolean, "status": number, "entry": {"songId": number, "bookId": number, "songNumber": number}}
*/
export async function addSongToBook(songId: number, songBookId: number, songNumber: number): Promise<DbResponse> {
export async function addSongToBook(rawSongId: number, rawSongBookId: number, rawSongNumber: string): Promise<DbResponse> {
try {
const songId = parseInt(rawSongId);
if (!songId) {
return { success: false, status: 400, error: "SongId has to be Integer" };
}
const songBookId = parseInt(rawSongBookId);
if (!songBookId) {
return { success: false, status: 400, error: "SongbookId has to be Integer" };
}
const songNumber = rawSongNumber?.trim() ?? null;
if(!songNumber){
return { success: false, status: 400, error: "Field 'SongNumber' ist required" };
}
const isTaken = await isSongNumberTaken(songBookId, songNumber);
if (isTaken) {
return { success: false, status: 409, error: "Songnumber already taken in this songbook" };
@@ -54,9 +67,21 @@ export async function addSongToBook(songId: number, songBookId: number, songNumb
* @param data - Object of the new data
* @returns {"success": true, "status": number, "entry": {"songId": number, "bookId": number, "songNumber": number}} | {"success": false, "error": string}
*/
export async function updateSongInBook(songId: number, songBookId: number, data: {songNumber: number}) {
export async function updateSongInBook(rawSongId: number, rawSongBookId: number, data: {number: string}) {
try {
const isTaken = await isSongNumberTaken(songBookId, data.songNumber);
const songId = parseInt(rawSongId);
if (!songId) {
return { success: false, status: 400, error: "Id has to be Integer" };
}
const songBookId = parseInt(rawSongBookId);
if (!songBookId) {
return { success: false, status: 400, error: "Id has to be Integer" };
}
const songNumber = data.number?.trim() ?? null;
if(!songNumber){
return { success: false, status: 400, error: "Field 'SongNumber' ist required" };
}
const isTaken = await isSongNumberTaken(songBookId, songNumber);
if (isTaken) {
return { success: false, status: 409, error: "New song number is already taken in this songbook" };
}
@@ -69,7 +94,7 @@ export async function updateSongInBook(songId: number, songBookId: number, data:
},
},
data: {
songNumber: data.songNumber,
songNumber,
},
select: {
songId: true,
@@ -103,9 +128,17 @@ export async function updateSongInBook(songId: number, songBookId: number, data:
* @param bookId - ID of the songbook
* @returns {"success": boolean, "status": number}
*/
export async function removeSongFromBook(songId: number, songBookId: number) {
export async function removeSongFromBook(rawSongId: number, rawSongBookId: number) {
try {
await prisma.songBookSongs.delete({
const songId = parseInt(rawSongId);
if (!songId) {
return { success: false, status: 400, error: "Id has to be Integer" };
}
const songBookId = parseInt(rawSongBookId);
if (!songBookId) {
return { success: false, status: 400, error: "Id has to be Integer" };
}
const entry = await prisma.songBookSongs.delete({
where: {
songBookId_songId: {
songId,
@@ -114,7 +147,7 @@ export async function removeSongFromBook(songId: number, songBookId: number) {
},
});
return { success: true, status: 200 };
return { success: true, status: 200, entry };
} catch (error) {
if (error.code === "P2025") {
return { success: false, status: 404, error: "Song assignment not found" };
@@ -132,12 +165,20 @@ export async function removeSongFromBook(songId: number, songBookId: number) {
* @param songNumber - The song number in that book
* @returns {"success": true, "status": number, "entry": {"songNumber": number "song": {"id": number, "title": string}}}
*/
export async function getSongByNumber(bookId: number, songNumber: number) {
export async function getSongByNumber(rawSongBookId: number, rawSongNumber: string) {
try {
const songBookId = parseInt(rawSongBookId);
if (!songBookId) {
return { success: false, status: 400, error: "Id has to be Integer" };
}
const songNumber = rawSongNumber?.trim() ?? null;
if(!songNumber){
return { success: false, status: 400, error: "Field 'SongNumber' ist required" };
}
const entry = await prisma.songBookSongs.findFirst({
where: {
songBookId: bookId,
songNumber: songNumber,
songBookId,
songNumber,
},
select: {
songNumber: true,
@@ -168,19 +209,19 @@ export async function getSongByNumber(bookId: number, songNumber: number) {
* @param songNumber - The song number to check
* @returns true if taken, false if available
*/
export async function isSongNumberTaken(songBookId: number, songNumber: number): Promise<boolean> {
export async function isSongNumberTaken(songBookId: number, songNumber: string): Promise<boolean> {
try {
const existing = await prisma.songBookSongs.findFirst({
where: {
songBookId,
songNumber,
songBookId: songBookId,
songNumber: songNumber,
},
select: {
songId: true,
},
});
return existing !== null;
return existing != null;
} catch (error) {
console.error("Error in isSongNumberTaken:", error);
return true; // If Error->Number is taken