Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/app/api/users/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -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);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Next time, I'd recommend leaving a more descriptive error like "Error with getting specific user" but that's minor, I'll merge it!

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 });
}
}
30 changes: 30 additions & 0 deletions src/app/api/users/route.ts
Original file line number Diff line number Diff line change
@@ -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 });
}
}
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);
console.log("DATABASE CONNECTED");
return connection;
}
};
Expand Down
11 changes: 7 additions & 4 deletions src/database/userSchema.ts
Original file line number Diff line number Diff line change
@@ -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");