Work in Songs and Songbooks API
This commit is contained in:
1 parent
e1eaea0f17
commit
4bdf03426c
6 files changed
+289
-11
No files matched your search
@@ -1,6 +1,141 @@
|
|||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import {
|
import { getSongBookById, upateSongBook, deleteSongBook } from "@/lib/db/songbook";
|
||||||
getSongBookById,
|
// import { parseInt } from "@/lib/helper/checkvar";
|
||||||
upateSongBook,
|
|
||||||
deleteSongBook
|
/**
|
||||||
} from "@/lib/db/songbook";
|
* @openapi
|
||||||
|
* /api/songbooks/{id}:
|
||||||
|
* get:
|
||||||
|
* summary: Einzelnen Songbook abrufen
|
||||||
|
* tags:
|
||||||
|
* - Songbooks
|
||||||
|
* 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 { id } = await params;
|
||||||
|
const res = await getSongBookById(id);
|
||||||
|
|
||||||
|
if (!res.success) {
|
||||||
|
return NextResponse.json({ message: res.error }, { status: res.status });
|
||||||
|
}
|
||||||
|
|
||||||
|
return NextResponse.json(res.songBook, { status: res.status });
|
||||||
|
} catch (error) {
|
||||||
|
console.error("API Error:", error);
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: "Fehler beim Laden der Songbookinfos" },
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @openapi
|
||||||
|
* /api/songbooks/{id}:
|
||||||
|
* patch:
|
||||||
|
* summary: Songbook aktualisieren
|
||||||
|
* tags:
|
||||||
|
* - Songbooks
|
||||||
|
* parameters:
|
||||||
|
* - in: path
|
||||||
|
* name: id
|
||||||
|
* required: true
|
||||||
|
* schema:
|
||||||
|
* type: integer
|
||||||
|
* description: Die ID des Songbooks
|
||||||
|
* requestBody:
|
||||||
|
* required: true
|
||||||
|
* content:
|
||||||
|
* application/json:
|
||||||
|
* schema:
|
||||||
|
* type: object
|
||||||
|
* responses:
|
||||||
|
* 200:
|
||||||
|
* description: Songbook erfolgreich aktualisiert
|
||||||
|
* 400:
|
||||||
|
* description: Ungültige Parameter oder Daten
|
||||||
|
* 404:
|
||||||
|
* description: Songbook nicht gefunden
|
||||||
|
* 500:
|
||||||
|
* description: Serverfehler
|
||||||
|
*/
|
||||||
|
export async function PATCH(request: Request, { params }) {
|
||||||
|
try {
|
||||||
|
const { id } = await params;
|
||||||
|
const data = await request.json();
|
||||||
|
const res = await upateSongBook(id, data);
|
||||||
|
|
||||||
|
if (!res.success) {
|
||||||
|
return NextResponse.json({ message: res.error }, { status: res.status });
|
||||||
|
}
|
||||||
|
|
||||||
|
return NextResponse.json(res.songBook, { status: res.status });
|
||||||
|
} catch (error) {
|
||||||
|
console.error("API Error:", error);
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: "Fehler beim Laden der Songbookinfos" },
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @openapi
|
||||||
|
* /api/songbooks/{id}:
|
||||||
|
* delete:
|
||||||
|
* summary: Songbook löschen
|
||||||
|
* tags:
|
||||||
|
* - Songbooks
|
||||||
|
* parameters:
|
||||||
|
* - in: path
|
||||||
|
* name: id
|
||||||
|
* required: true
|
||||||
|
* schema:
|
||||||
|
* type: integer
|
||||||
|
* description: Die ID des Songbooks
|
||||||
|
* responses:
|
||||||
|
* 200:
|
||||||
|
* description: Songbook erfolgreich gelöscht
|
||||||
|
* 400:
|
||||||
|
* description: Ungültige Songbook-ID
|
||||||
|
* 404:
|
||||||
|
* description: Songbook nicht gefunden
|
||||||
|
* 500:
|
||||||
|
* description: Serverfehler
|
||||||
|
*/
|
||||||
|
export async function DELETE(request: Request, { params }) {
|
||||||
|
try {
|
||||||
|
const { id } = await params;
|
||||||
|
const res = await deleteSongBook(id);
|
||||||
|
|
||||||
|
if (!res.success) {
|
||||||
|
return NextResponse.json({ message: res.error }, { status: res.status });
|
||||||
|
}
|
||||||
|
|
||||||
|
return NextResponse.json(res.songBook, { status: res.status });
|
||||||
|
} catch (error) {
|
||||||
|
console.error("API Error:", error);
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: "Fehler beim Laden der Songbookinfos" },
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,32 @@
|
|||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import { getSongById, updateSong, deleteSong } from "@/lib/db/song";
|
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 }) {
|
export async function GET(request: Request, { params }) {
|
||||||
try {
|
try {
|
||||||
const { id } = await params;
|
const { id } = await params;
|
||||||
@@ -19,3 +45,98 @@ export async function GET(request: Request, { params }) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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 { id } = await params;
|
||||||
|
const data = await request.json();
|
||||||
|
const res = await updateSong(id, 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 { id } = await params;
|
||||||
|
const res = await deleteSong(id);
|
||||||
|
|
||||||
|
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 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
|
|
||||||
import { getSongs, searchSongsByTitle, createSong, } from "@/lib/db/song";
|
import { getSongs, searchSongsByTitle, createSong, } from "@/lib/db/song";
|
||||||
|
|
||||||
|
|
||||||
@@ -30,6 +29,7 @@ export async function GET(request: Request) {
|
|||||||
return NextResponse.json(res.songBooks, { status: res.status });
|
return NextResponse.json(res.songBooks, { status: res.status });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ansonsten alle Songs abrufen
|
||||||
const res = await getSongs();
|
const res = await getSongs();
|
||||||
|
|
||||||
if (!res.success) {
|
if (!res.success) {
|
||||||
@@ -38,7 +38,7 @@ export async function GET(request: Request) {
|
|||||||
|
|
||||||
return NextResponse.json(res.songs, { status: res.status });
|
return NextResponse.json(res.songs, { status: res.status });
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("POST /api/songs error:", error);
|
console.error("POST /api/songs error:", error);
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ message: "Internal Server Error" },
|
{ message: "Internal Server Error" },
|
||||||
@@ -97,7 +97,10 @@ export async function POST(request: Request) {
|
|||||||
const data = await request.json();
|
const data = await request.json();
|
||||||
|
|
||||||
if (!data.title) {
|
if (!data.title) {
|
||||||
return NextResponse.json({ message: "Fields incomplete"}, { status: 400 });
|
return NextResponse.json(
|
||||||
|
{ message: "Field 'title' is required"},
|
||||||
|
{ status: 400 }
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const res = await createSong(data);
|
const res = await createSong(data);
|
||||||
@@ -110,6 +113,7 @@ export async function POST(request: Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return NextResponse.json(res, { status: res.status });
|
return NextResponse.json(res, { status: res.status });
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("POST /api/songs error:", error);
|
console.error("POST /api/songs error:", error);
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
|
|||||||
+16
-3
@@ -1,4 +1,5 @@
|
|||||||
import { prisma } from "@/lib/db/prisma";
|
import { prisma } from "@/lib/db/prisma";
|
||||||
|
import { parseInt } from "@/lib/helper/checkvar";
|
||||||
// For Error checking use this import: import { Prisma } from "@prisma/client";
|
// For Error checking use this import: import { Prisma } from "@prisma/client";
|
||||||
|
|
||||||
|
|
||||||
@@ -34,8 +35,12 @@ export async function getSongs() {
|
|||||||
* @param id - ID indicatin the song
|
* @param id - ID indicatin the song
|
||||||
* @returns {"success": boolean, "status": number, "song": {"id":number, "title": string, "songBooks": [{"songNumber": number, "book": {"title": string, "language": string}},...]}}
|
* @returns {"success": boolean, "status": number, "song": {"id":number, "title": string, "songBooks": [{"songNumber": number, "book": {"title": string, "language": string}},...]}}
|
||||||
*/
|
*/
|
||||||
export async function getSongById(id: number) {
|
export async function getSongById(rawId: number) {
|
||||||
try{
|
try{
|
||||||
|
const id = parseInt(rawId);
|
||||||
|
if (!id) {
|
||||||
|
return { success: false, status: 400, error: "Id has to be Integer" };
|
||||||
|
}
|
||||||
const song = await prisma.song.findUnique({
|
const song = await prisma.song.findUnique({
|
||||||
where: { id },
|
where: { id },
|
||||||
select: {
|
select: {
|
||||||
@@ -153,10 +158,14 @@ export async function createSong(data: { title: string }) {
|
|||||||
* @returns {"success": boolean, "status": number, "song": {"id": number, "title": string}}
|
* @returns {"success": boolean, "status": number, "song": {"id": number, "title": string}}
|
||||||
*/
|
*/
|
||||||
export async function updateSong(
|
export async function updateSong(
|
||||||
id: number,
|
rawId: number,
|
||||||
data: { title?: string }
|
data: { title?: string }
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
|
const id = parseInt(rawId);
|
||||||
|
if (!id) {
|
||||||
|
return { success: false, status: 400, error: "Id has to be Integer" };
|
||||||
|
}
|
||||||
// Prepare update fields dynamically
|
// Prepare update fields dynamically
|
||||||
const updateData: { title?: string } = {};
|
const updateData: { title?: string } = {};
|
||||||
|
|
||||||
@@ -200,8 +209,12 @@ export async function updateSong(
|
|||||||
* @param id - ID indicatin the song
|
* @param id - ID indicatin the song
|
||||||
* @returns {"success": boolean, "status": number, "song": {"id": number, "title": string}}
|
* @returns {"success": boolean, "status": number, "song": {"id": number, "title": string}}
|
||||||
*/
|
*/
|
||||||
export async function deleteSong(id: number) {
|
export async function deleteSong(rawId: number) {
|
||||||
try {
|
try {
|
||||||
|
const id = parseInt(rawId);
|
||||||
|
if (!id) {
|
||||||
|
return { success: false, status: 400, error: "Id has to be Integer" };
|
||||||
|
}
|
||||||
const song = await prisma.song.delete({
|
const song = await prisma.song.delete({
|
||||||
where: { id },
|
where: { id },
|
||||||
select: {
|
select: {
|
||||||
|
|||||||
Generated
+1
@@ -3362,6 +3362,7 @@
|
|||||||
"version": "13.0.2",
|
"version": "13.0.2",
|
||||||
"resolved": "https://registry.npmjs.org/better-sqlite3/-/better-sqlite3-13.0.2.tgz",
|
"resolved": "https://registry.npmjs.org/better-sqlite3/-/better-sqlite3-13.0.2.tgz",
|
||||||
"integrity": "sha512-jW6oufeDhXZaiX9Lw5A+oerVClx4iFrI6uDj1zu7SqUAjak9vbJvA0NEcKLNxHiQHb6kYCoFzzXYV0YOauhV3g==",
|
"integrity": "sha512-jW6oufeDhXZaiX9Lw5A+oerVClx4iFrI6uDj1zu7SqUAjak9vbJvA0NEcKLNxHiQHb6kYCoFzzXYV0YOauhV3g==",
|
||||||
|
"hasInstallScript": true,
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"node-addon-api": "^8.0.0"
|
"node-addon-api": "^8.0.0"
|
||||||
|
|||||||
@@ -38,5 +38,9 @@
|
|||||||
"sharp",
|
"sharp",
|
||||||
"unrs-resolver"
|
"unrs-resolver"
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
"allowScripts": {
|
||||||
|
"better-sqlite3@13.0.2": true,
|
||||||
|
"better-sqlite3@12.11.1": true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user