143 lines
3.4 KiB
TypeScript
143 lines
3.4 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { getSongById, updateSong, deleteSong } from "@/lib/db/song";
|
|
// import { parseInt } from "@/lib/helper/checkvar";
|
|
|
|
|
|
/**
|
|
* @openapi
|
|
* /api/songs/{id}:
|
|
* get:
|
|
* summary: Einzelnen Song abrufen
|
|
* tags:
|
|
* - Songs
|
|
* parameters:
|
|
* - in: path
|
|
* name: id
|
|
* required: true
|
|
* schema:
|
|
* type: integer
|
|
* description: Die ID des Songs
|
|
* responses:
|
|
* 200:
|
|
* description: Song erfolgreich gefunden
|
|
* 400:
|
|
* description: Ungültige Song-ID
|
|
* 404:
|
|
* description: Song nicht gefunden
|
|
* 500:
|
|
* description: Serverfehler
|
|
*/
|
|
export async function GET(request: Request, { params }) {
|
|
try {
|
|
const { songId } = await params;
|
|
const res = await getSongById(songId);
|
|
|
|
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(
|
|
{ error: "Fehler beim Laden der Songinfos" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @openapi
|
|
* /api/songs/{id}:
|
|
* patch:
|
|
* summary: Song aktualisieren
|
|
* tags:
|
|
* - Songs
|
|
* parameters:
|
|
* - in: path
|
|
* name: id
|
|
* required: true
|
|
* schema:
|
|
* type: integer
|
|
* description: Die ID des Songs
|
|
* requestBody:
|
|
* required: true
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* responses:
|
|
* 200:
|
|
* description: Song erfolgreich aktualisiert
|
|
* 400:
|
|
* description: Ungültige Parameter oder Daten
|
|
* 404:
|
|
* description: Song nicht gefunden
|
|
* 500:
|
|
* description: Serverfehler
|
|
*/
|
|
export async function PATCH(request: Request, { params }) {
|
|
try {
|
|
const { songId } = await params;
|
|
const data = await request.json();
|
|
const res = await updateSong(songId, data);
|
|
|
|
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(
|
|
{ error: "Fehler beim Laden der Songinfos" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @openapi
|
|
* /api/songs/{id}:
|
|
* delete:
|
|
* summary: Song löschen
|
|
* tags:
|
|
* - Songs
|
|
* parameters:
|
|
* - in: path
|
|
* name: id
|
|
* required: true
|
|
* schema:
|
|
* type: integer
|
|
* description: Die ID des Songs
|
|
* responses:
|
|
* 200:
|
|
* description: Song erfolgreich gelöscht
|
|
* 400:
|
|
* description: Ungültige Song-ID
|
|
* 404:
|
|
* description: Song nicht gefunden
|
|
* 500:
|
|
* description: Serverfehler
|
|
*/
|
|
export async function DELETE(request: Request, { params }) {
|
|
try {
|
|
const { songId } = await params;
|
|
const res = await deleteSong(songId);
|
|
|
|
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(
|
|
{ error: "Fehler beim Laden der Songinfos" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|