diff --git a/src/app/api/users/[id]/route.ts b/src/app/api/users/[id]/route.ts new file mode 100644 index 0000000..f5bded6 --- /dev/null +++ b/src/app/api/users/[id]/route.ts @@ -0,0 +1,46 @@ +import connectDB from "@/database/db"; +import { NextResponse, NextRequest } from "next/server"; +import User from "@/database/userSchema"; +import mongoose from "mongoose"; + +// Get a specific user, finding the user by their userID +export async function GET(_req: NextRequest, { params }: { params: { id: string } }) { + try { + const { id } = params; + if (!mongoose.Types.ObjectId.isValid(id)) { + return NextResponse.json({ error: "Invalid user id" }, { status: 400 }); + } + await connectDB(); + + const user = await User.findById(id).lean(); + if (!user) { + return NextResponse.json({ error: "User not found" }, { status: 404 }); + } + return NextResponse.json(user, { status: 200 }); + } catch (err: any) { + console.error("Caught an error", err); + return NextResponse.json({ error: err.message }, { status: 500 }); + } +} + +// Update a specific user, finding the user by their userID +export async function PUT(req: NextRequest, { params }: { params: { id: string } }) { + try { + const { id } = params; + const changes = await req.json(); + if (!mongoose.Types.ObjectId.isValid(id)) { + return NextResponse.json({ error: "Invalid user id" }, { status: 400 }); + } + await connectDB(); + + const updatedUser = await User.findByIdAndUpdate(id, { $set: changes }, { new: true, runValidators: true }).lean(); + if (!updatedUser) { + return NextResponse.json({ error: "User not found" }, { status: 404 }); + } + + return NextResponse.json(updatedUser, { status: 200 }); + } catch (err: any) { + console.error("Caught an error", err); + return NextResponse.json({ error: err.message }, { status: 400 }); + } +} diff --git a/src/app/api/users/route.ts b/src/app/api/users/route.ts new file mode 100644 index 0000000..ea7de63 --- /dev/null +++ b/src/app/api/users/route.ts @@ -0,0 +1,30 @@ +import connectDB from "@/database/db"; +import { NextResponse, NextRequest } from "next/server"; +import User from "@/database/userSchema"; + +// CREATE a user with the parameters that are passed in, catching any server connection or bad requestst that may occur +export async function POST(req: NextRequest) { + try { + await connectDB(); + + const body = await req.json(); + const user = await User.create(body); + return NextResponse.json(user, { status: 201 }); + } catch (err: any) { + console.error("Caught an error", err); + return NextResponse.json({ error: err.message }, { status: 400 }); + } +} + +// RETRIEVE all users from the database, catching any errors that may occur +export async function GET() { + try { + await connectDB(); + + const retrieved_users = await User.find({}).lean(); + return NextResponse.json(retrieved_users, { status: 200 }); + } catch (err: any) { + console.error("Caught an error", err); + return NextResponse.json({ error: err.message }, { status: 400 }); + } +} diff --git a/src/database/db.ts b/src/database/db.ts index c54cfb5..de6318d 100644 --- a/src/database/db.ts +++ b/src/database/db.ts @@ -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); + console.log("DATABASE CONNECTED"); return connection; } }; diff --git a/src/database/userSchema.ts b/src/database/userSchema.ts index 166f887..6e84619 100644 --- a/src/database/userSchema.ts +++ b/src/database/userSchema.ts @@ -1,9 +1,12 @@ import mongoose, { Schema } from "mongoose"; -//! Example user schema. Not guaranteed to work +// Updated UserSchema to match specifications const UserSchema = new Schema({ - email: { type: String, required: true, unique: true }, - password: { type: String, required: true }, + name: { type: String, required: true, trim: true }, + username: { type: String, required: true, trim: true }, + role: { type: String, required: true, trim: true }, + email: { type: String, required: true, trim: true }, }); -export default mongoose.models.User || mongoose.model("User", UserSchema); +// Added "users" at the end to ensure that it maps to the users schema that is in the Atlas Validator +export default mongoose.models.User || mongoose.model("User", UserSchema, "users");