generated from hack4impact-calpoly/nextjs-app-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from hack4impact-calpoly:CreateAudioFileSchema
Create audio file schema
- Loading branch information
Showing
9 changed files
with
102 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ yarn-error.log* | |
# local env files | ||
.env*.local | ||
.env | ||
.env.local | ||
|
||
# vercel | ||
.vercel | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { NextResponse } from "next/server"; | ||
import type { NextRequest } from "next/server"; | ||
import mongoose from "mongoose"; | ||
import connectDB from "@/database/db"; | ||
import Audiofile from "@/database/models/audiofileSchema"; | ||
|
||
// Ensure database connection before handling requests | ||
async function ensureDatabaseConnection() { | ||
try { | ||
await connectDB(); | ||
} catch (connectError) { | ||
console.error("Error connecting to database:", connectError); | ||
throw new Error("Database connection error."); | ||
} | ||
} | ||
|
||
// Get specific audiofile by ID | ||
export async function GET(request: NextRequest, { params }: { params: { _id: string } }) { | ||
console.log("PARAMS", params); | ||
const { _id } = params; | ||
|
||
try { | ||
await ensureDatabaseConnection(); | ||
console.log("GETTING AUDIOFILE", _id); | ||
const audiofile = await Audiofile.findById(_id).exec(); | ||
console.log(audiofile); | ||
if (!audiofile) { | ||
return NextResponse.json({ error: "Audiofile not found." }, { status: 404 }); | ||
} | ||
return NextResponse.json(audiofile, { status: 200 }); | ||
} catch (error) { | ||
console.error("Error fetching audiofile:", error); | ||
|
||
// Handle Mongoose CastError (invalid ObjectId) | ||
if (error instanceof mongoose.Error.CastError) { | ||
return NextResponse.json({ error: "Invalid audiofile ID." }, { status: 400 }); | ||
} | ||
|
||
// Handle other errors | ||
return NextResponse.json({ error: "Error: Unable to fetch the audiofile." }, { status: 500 }); | ||
} | ||
} | ||
|
||
// Update specific audiofile by ID | ||
export async function PUT(request: NextRequest, { params }: { params: { _id: string } }) { | ||
const { _id } = params; | ||
|
||
try { | ||
await ensureDatabaseConnection(); | ||
|
||
// Parse the request body | ||
const body = await request.json(); | ||
const { name, duration, description } = body; | ||
|
||
// Find the audiofile by ID and update it | ||
const updatedAudiofile = await Audiofile.findByIdAndUpdate( | ||
_id, | ||
{ $set: { name, duration, description } }, | ||
{ new: true, runValidators: true }, | ||
).exec(); | ||
|
||
if (!updatedAudiofile) { | ||
return NextResponse.json({ error: "Audiofile not found." }, { status: 404 }); | ||
} | ||
|
||
return NextResponse.json(updatedAudiofile, { status: 200 }); | ||
} catch (error) { | ||
console.error("Error updating audiofile:", error); | ||
|
||
// Handle Mongoose CastError (invalid ObjectId) | ||
if (error instanceof mongoose.Error.CastError) { | ||
return NextResponse.json({ error: "Invalid audiofile ID." }, { status: 400 }); | ||
} | ||
|
||
// Handle other errors | ||
return NextResponse.json({ error: "Error: Unable to update the audiofile." }, { status: 500 }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import mongoose, { Schema } from "mongoose"; | ||
|
||
const audiofileSchema = new Schema( | ||
{ | ||
name: { type: String, required: true, unique: true }, | ||
url: { type: String, required: true, unique: true }, | ||
duration: { type: String, required: true }, | ||
description: { type: String, required: true }, | ||
}, | ||
{ collection: "audiofiles" }, | ||
); | ||
|
||
export default mongoose.models.Audiofile || mongoose.model("Audiofile", audiofileSchema); |