-
Notifications
You must be signed in to change notification settings - Fork 3
K1 5 create user schema and endpoints #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,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 }); | ||
| } | ||
| } | ||
This file contains hidden or 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,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 }); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -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"); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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!