Some Route.js changes
This commit is contained in:
1 parent
cb18b0e14f
commit
69378ca439
2 files changed
+55
-6
No files matched your search
@@ -0,0 +1,6 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import {
|
||||
getSongBookById,
|
||||
upateSongBook,
|
||||
deleteSongBook
|
||||
} from "@/lib/db/songbook";
|
||||
@@ -1,15 +1,58 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { getSongbooks, addSongbook } from "@/lib/db/managing";
|
||||
import {
|
||||
getSongBooks,
|
||||
searchSongBookByTitle,
|
||||
createSongBook,
|
||||
} from "@/lib/db/songbook";
|
||||
|
||||
|
||||
export async function GET() {
|
||||
/**
|
||||
* @openapi
|
||||
* /api/songbooks:
|
||||
* get:
|
||||
* summary: Get all songbooks
|
||||
* responses:
|
||||
* 200:
|
||||
* description: List of songbooks
|
||||
* 500:
|
||||
* description: Internal Server Error
|
||||
*/
|
||||
export async function GET(request: Request) {
|
||||
try {
|
||||
const songbooks = await getSongbooks();
|
||||
return NextResponse.json(songbooks);
|
||||
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 searchSongBookByTitle(query);
|
||||
|
||||
if (!res.success) {
|
||||
return NextResponse.json({ message: res.error }, { status: 500 });
|
||||
}
|
||||
|
||||
return NextResponse.json(res.songBooks, { status: 200 });
|
||||
}
|
||||
|
||||
// Ansonsten alle Bücher abrufen
|
||||
const res = await getSongBooks();
|
||||
|
||||
if (!res.success) {
|
||||
return NextResponse.json({ message: res.error }, { status: 500 });
|
||||
}
|
||||
|
||||
return NextResponse.json(res.songBooks, { status: 200 });
|
||||
|
||||
} catch (error) {
|
||||
return NextResponse.json({ success: false, message: error}, { status: 500})
|
||||
console.error("GET /api/songbooks error:", error);
|
||||
return NextResponse.json(
|
||||
{ message: "Internal Server Error" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const { name, language } = await request.json();
|
||||
@@ -18,7 +61,7 @@ export async function POST(request: Request) {
|
||||
return NextResponse.json({ success: false, message: "Fields incomplete (name)"}, { status: 400 })
|
||||
}
|
||||
|
||||
const newBook = await addSongbook(String(name), String(language))
|
||||
const newBook = await createSongBook({String(name), String(language)})
|
||||
|
||||
return NextResponse.json(newBook, { status: 201 })
|
||||
} catch (error) {
|
||||
|
||||
Reference in new issue
Block a user