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
266247d
commit f3815a3
Showing
4 changed files
with
125 additions
and
56 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 |
---|---|---|
@@ -1,27 +1,94 @@ | ||
import { Request, Response } from 'express'; | ||
import userModel from '../models/User'; | ||
|
||
|
||
const getUsers = async (req: Request, res: Response) => { | ||
res.status(200).json("status"); | ||
try { | ||
const users = await userModel.find(); | ||
|
||
return res.status(200).json(users); | ||
|
||
} catch (error) { | ||
return res.status(500).json({ message: "Internal Server error" }); | ||
} | ||
} | ||
|
||
const createUser = async (req: Request, res: Response) => { | ||
|
||
// Check if the request body is missing the username or password | ||
if (!req.body.username || !req.body.password) { | ||
res.status(400).json({ error: "missing username or password" }); | ||
return; | ||
try { | ||
const { username, password } = req.body; | ||
|
||
// Check if the request body is missing the username or password | ||
if (!username || !password) { | ||
return res.status(400).json({ error: "missing username or password" }); | ||
} | ||
|
||
const userExists = await userModel.findOne({ username }); | ||
|
||
if (userExists) { | ||
return res.status(409).json({ error: "Username already exists." }); | ||
} | ||
|
||
if (password.length < 5) { | ||
return res.status(400).json({ error: "Password needs to be more than 5 letters" }); | ||
} | ||
|
||
const user = new userModel({ username, password }) | ||
|
||
await user.save(); | ||
|
||
return res.status(201).json({ message: "User created successfuly", user }); | ||
} catch (error) { | ||
res.status(500).json({ error: "Internal Server error" }); | ||
} | ||
} | ||
|
||
const updateUserPassword = async (req: Request, res: Response) => { | ||
try { | ||
const { username, password } = req.body; | ||
const user = new userModel({ username, password }); | ||
await user.save(); | ||
res.status(201).json(user); | ||
const { id } = req.params; | ||
const { newPassword } = req.body; | ||
|
||
const user = await userModel.findByIdAndUpdate(id, { password: newPassword }, { new: true }); | ||
|
||
if (!user) { | ||
return res.status(404).json({ message: "User not found" }); | ||
} | ||
|
||
return res.status(200).json({ message: "User password updated successfuly", user }); | ||
} catch (error) { | ||
res.status(500).json({ error: "Internal Server error" }); | ||
} | ||
} | ||
|
||
const updateUserName = async (req: Request, res: Response) => { | ||
try { | ||
const { id } = req.params; | ||
const { newName } = req.body; | ||
|
||
const user = await userModel.findByIdAndUpdate(id, { username: newName }, { new: true }); | ||
|
||
if (!user) { | ||
return res.status(404).json({ message: "User not found" }); | ||
} | ||
|
||
return res.status(200).json({ message: "User username updated successfuly", user }); | ||
} catch (error) { | ||
res.status(500).json({ error: "Internal Server error" }); | ||
} | ||
} | ||
|
||
const deleteUser = async (req: Request, res: Response) => { | ||
try { | ||
const { id } = req.params; | ||
|
||
const user = await userModel.findByIdAndDelete(id); | ||
|
||
if (!user) { | ||
return res.status(404).json({ message: "User not found" }); | ||
} | ||
|
||
return res.status(200).json({ message: "User deleted successfuly", user }); | ||
} catch (error) { | ||
res.status(500).json({ error: "username taken" }); | ||
res.status(500).json({ error: "Internal Server error" }); | ||
} | ||
} | ||
|
||
export { getUsers, createUser }; | ||
export { getUsers, createUser, updateUserPassword, 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 |
---|---|---|
@@ -1,10 +1,14 @@ | ||
import { Router } from "express"; | ||
import { getUsers, createUser } from "../controllers/UserController"; | ||
import { getUsers, createUser, updateUserPassword, updateUserName, deleteUser} from "../controllers/UserController"; | ||
|
||
const userRoutes = Router(); | ||
|
||
// Get all Users | ||
userRoutes.get("/", getUsers); | ||
userRoutes.post("/", createUser); | ||
userRoutes.patch("/:id", updateUserPassword); | ||
userRoutes.patch("/:id", updateUserName); | ||
userRoutes.delete("/:id", deleteUser); | ||
|
||
|
||
export default userRoutes; |
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