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
+347 -119

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,14 +29,14 @@ 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 });
}
return NextResponse.json({ message: res.error }, { status: res.status });
}
return NextResponse.json(res.song, { status: res.status });
return NextResponse.json(res.song, { status: res.status });
} catch (error) {
console.error("API Error:", error);
return NextResponse.json(
@@ -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 }
);
}
}