Skip to content

Commit

Permalink
Merge pull request #36 from hack4impact-calpoly/add-getAll-audios
Browse files Browse the repository at this point in the history
Added HTTP request to get all audios and post audios
  • Loading branch information
gracebw7 authored Feb 10, 2025
2 parents b906d51 + 6ca97e2 commit 9104820
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
1 change: 0 additions & 1 deletion next.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
serverActions: true,
optimizePackageImports: ["@chakra-ui/react"],
},
images: {
Expand Down
41 changes: 41 additions & 0 deletions src/app/api/audiofile/route.ts
Original file line number Diff line number Diff line change
@@ -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 });
}
}

0 comments on commit 9104820

Please sign in to comment.