Skip to content

Commit

Permalink
Merge pull request #35 from hack4impact-calpoly:CreateAudioFileSchema
Browse files Browse the repository at this point in the history
Create audio file schema
  • Loading branch information
gracebw7 authored Feb 4, 2025
2 parents 2539d1b + 2a99f33 commit b906d51
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 9 deletions.
8 changes: 4 additions & 4 deletions .github/ISSUE_TEMPLATE/new-issue.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
---
name: New Issue
about: Create a new issue
title: ''
labels: ''
assignees: ''

title: ""
labels: ""
assignees: ""
---

**Description**
Expand All @@ -14,6 +13,7 @@ A clear and concise description of what the problem is. Ex. I'm always frustrate
A clear and concise description of what you want to happen.

Steps:

- step 1
- step 2

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ jobs:
# - Settings -> Secrets and variables -> New repository secret
MONGO_URI: ${{ secrets.MONGO_URI }}
# Add additional environment variables here

- name: Run tests
run: npm test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ yarn-error.log*
# local env files
.env*.local
.env
.env.local

# vercel
.vercel
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ The LCSLO team consists of {#} Cal Poly students. Over the course of about 9 mon
- [Saurish Suman](https://www.linkedin.com/in/saurish-suman/) - Software Developer
- [Ethan Yang]() - Software Developer
- [Jason Yu](https://www.linkedin.com/in/jasonyu101/) - Software Developer

## Getting Started And Contributing

Visit [getting-started.md](docs/getting-started.md) on info for how to set up this repo.
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"@emotion/react": "^11.14.0",
"@vercel/blob": "^0.27.1",
"howler": "^2.2.4",
"mongoose": "^8",
"mongoose": "^8.9.5",
"next": "^14",
"next-themes": "^0.4.4",
"react": "^18",
Expand Down
78 changes: 78 additions & 0 deletions src/app/api/audiofile/[_id]/route.ts
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 });
}
}
4 changes: 2 additions & 2 deletions src/database/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ let connection: typeof mongoose;
const connectDB = async () => {
if (!connection) {
// uncomment this line once you have the MONGO_URI set up
// connection = await mongoose.connect(url);
connection = "remove me" as any; // remove me
connection = await mongoose.connect(url);
// connection = "remove me" as any; // remove me
return connection;
}
};
Expand Down
13 changes: 13 additions & 0 deletions src/database/models/audiofileSchema.ts
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);

0 comments on commit b906d51

Please sign in to comment.