generated from UoaWDCC/react-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.
- Loading branch information
1 parent
7765025
commit 9333bf0
Showing
11 changed files
with
738 additions
and
193 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { Request, Response } from "express"; | ||
import User from "../models/User"; | ||
import bcrypt from "bcrypt"; | ||
|
||
const createUser = async (req: Request, res: Response) => { | ||
try { | ||
const { name, email, password } = req.body; | ||
|
||
// Validate input | ||
if (!name || !email || !password) { | ||
return res.status(400).json({ message: "All fields are required." }); | ||
} | ||
|
||
const userExists = await User.findOne({ email }); | ||
|
||
if (userExists) { | ||
return res.status(409).json({ message: "User already exists." }); | ||
} | ||
|
||
// Hash the password before saving | ||
const hashedPassword = await bcrypt.hash(password, 10); | ||
|
||
// Create new User object | ||
const user = new User({ name, email, password: hashedPassword }); | ||
|
||
// Save the user to the database | ||
await user.save(); | ||
|
||
// Send a response back to the client | ||
res.status(201).json({ message: "User created successfuly", user }); | ||
} catch (err) { | ||
res.status(500).json({ message: "Internal Server error" }); | ||
} | ||
}; | ||
|
||
const getUsers = async (req: Request, res: Response) => { | ||
try { | ||
const users = await User.find(); | ||
|
||
return res.status(200).json(users); | ||
} catch (err) { | ||
return res.status(500).json({ message: "Internal Server error" }); | ||
} | ||
}; | ||
|
||
const getUser = async (req: Request, res: Response) => { | ||
try { | ||
const { id } = req.params; | ||
|
||
const user = await User.findById(id); | ||
|
||
if (!user) { | ||
return res.status(404).json({ message: "User not found" }); | ||
} | ||
|
||
return res.status(200).json(user); | ||
} catch (err) { | ||
return res.status(500).json({ message: "Internal Server error" }); | ||
} | ||
}; | ||
|
||
const updateUserName = async (req: Request, res: Response) => { | ||
try { | ||
const { id } = req.params; | ||
const { newName } = req.body; | ||
|
||
const user = await User.findByIdAndUpdate(id, { name: newName }, { new: true }); | ||
|
||
if (!user) { | ||
return res.status(404).json({ message: "User not found" }); | ||
} | ||
|
||
return res.status(200).json({ message: "User name updated successfully", user }); | ||
} catch (err) { | ||
return res.status(500).json({ message: "Internal Server error" }); | ||
} | ||
}; | ||
|
||
const deleteUser = async (req: Request, res: Response) => { | ||
try { | ||
const { id } = req.params; | ||
|
||
const user = await User.findByIdAndDelete(id); | ||
|
||
if (!user) { | ||
return res.status(404).json({ message: "User not found" }); | ||
} | ||
|
||
return res.status(200).json({ message: "User deleted successfully", user }); | ||
} catch (err) { | ||
return res.status(500).json({ message: "Internal Server error" }); | ||
} | ||
}; | ||
|
||
export { createUser, getUsers, getUser, updateUserName, deleteUser }; |
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,26 @@ | ||
import { Schema, model } from "mongoose"; | ||
|
||
const userSchema = new Schema( | ||
{ | ||
name: { | ||
type: String, | ||
required: true, | ||
}, | ||
email: { | ||
type: String, | ||
required: true, | ||
unique: true, | ||
}, | ||
password: { | ||
type: String, | ||
required: true, | ||
}, | ||
}, | ||
{ | ||
timestamps: true, | ||
} | ||
); | ||
|
||
const User = model("User", userSchema); | ||
|
||
export default User; |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Router } from "express"; | ||
import { createUser, getUsers, getUser, updateUserName, deleteUser } from "../controllers/UserController"; | ||
|
||
const userRoutes = Router(); | ||
|
||
userRoutes.post("/", createUser); | ||
|
||
userRoutes.get("/", getUsers); | ||
|
||
userRoutes.get("/:id", getUser); | ||
|
||
userRoutes.patch("/:id", updateUserName); | ||
|
||
userRoutes.delete("/:id", deleteUser); | ||
|
||
export default userRoutes; |
Oops, something went wrong.