diff --git a/next.config.js b/next.config.js index 44005a4..af41f91 100644 --- a/next.config.js +++ b/next.config.js @@ -1,7 +1,6 @@ /** @type {import('next').NextConfig} */ const nextConfig = { experimental: { - serverActions: true, optimizePackageImports: ["@chakra-ui/react"], }, images: { diff --git a/src/app/api/audiofile/route.ts b/src/app/api/audiofile/route.ts new file mode 100644 index 0000000..b9b738a --- /dev/null +++ b/src/app/api/audiofile/route.ts @@ -0,0 +1,41 @@ +import { NextResponse } from "next/server"; +import connectDB from "@/database/db"; +import Audiofile from "@/database/models/audiofileSchema"; + +export async function GET() { + try { + await connectDB(); + const audiofiles = await Audiofile.find().exec(); + if (!audiofiles.length) { + return NextResponse.json({ error: "No audio files found." }, { status: 404 }); + } + return NextResponse.json(audiofiles, { status: 200 }); + } catch (error) { + console.error("Error fetching audio files:", error); + return NextResponse.json({ error: "Error: Unable to fetch audio files." }, { status: 500 }); + } +} + +export async function POST(request: Request) { + try { + await connectDB(); + + // Parse the request body + const body = await request.json(); + const { name, url, duration, description } = body; + + // Validate required fields + if (!name || !url) { + return NextResponse.json({ error: "Name and URL are required fields." }, { status: 400 }); + } + + // Create and save the new audiofile + const newAudiofile = new Audiofile({ name, url, duration, description }); + await newAudiofile.save(); + + return NextResponse.json(newAudiofile, { status: 201 }); + } catch (error) { + console.error("Error creating audio file:", error); + return NextResponse.json({ error: "Error: Unable to create audio file." }, { status: 500 }); + } +}