Files
2026-10-13 19:26:56 +02:00

204 lines
5.3 KiB
TypeScript

// @/app/api/songs/[songId]/route.ts
import { NextResponse } from "next/server";
import { getSongById, updateSong, deleteSong } from "@/lib/db/song";
interface RouteParams {
params: Promise<{
songId: string;
}>;
};
/**
* @openapi
* /api/songs/{songId}:
* get:
* summary: Einzelnen Song abrufen
* tags:
* - Songs
* parameters:
* - in: path
* name: songId
* required: true
* schema:
* type: integer
* description: Die ID des Songs
* example: 1
* responses:
* 200:
* description: Song erfolgreich gefunden
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Song'
* 400:
* description: Ungültige Song-ID
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 404:
* description: Song nicht gefunden
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 500:
* description: Serverfehler
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
*/
export async function GET(_request: Request, { params }: RouteParams) {
try {
const { songId } = await params;
const res = await getSongById(parseInt(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/{songId}:
* patch:
* summary: Song aktualisieren
* tags:
* - Songs
* parameters:
* - in: path
* name: songId
* required: true
* schema:
* type: integer
* description: Die ID des Songs
* example: 1
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* properties:
* title:
* type: string
* example: "Großer Gott"
* responses:
* 200:
* description: Song erfolgreich aktualisiert
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Song'
* 400:
* description: Ungültige Parameter oder Daten
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 404:
* description: Song nicht gefunden
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 500:
* description: Serverfehler
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
*/
export async function PATCH(request: Request, { params }: RouteParams) {
try {
const { songId } = await params;
const data = await request.json();
const res = await updateSong(parseInt(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 Aktualisieren des Songs" },
{ status: 500 }
);
}
}
/**
* @openapi
* /api/songs/{songId}:
* delete:
* summary: Song löschen
* tags:
* - Songs
* parameters:
* - in: path
* name: songId
* required: true
* schema:
* type: integer
* description: Die ID des Songs
* example: 1
* responses:
* 200:
* description: Song erfolgreich gelöscht (gibt den gelöschten Song zurück)
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Song'
* 400:
* description: Ungültige Song-ID
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 404:
* description: Song nicht gefunden
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 500:
* description: Serverfehler
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
*/
export async function DELETE(_request: Request, { params }: RouteParams) {
try {
const { songId } = await params;
const res = await deleteSong(parseInt(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 Löschen des Songs" },
{ status: 500 }
);
}
}