Skip to content

Commit

Permalink
feat & fix: added get all users & fixed other endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
jaydelal13 committed Aug 29, 2024
1 parent 5889a50 commit 8aa649a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 53 deletions.
54 changes: 25 additions & 29 deletions backend/controllers/UserController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const createUser = async (req: Request, res: Response) => {

await user.save();

return res.status(201).json({ message: "User created successfully" });
return res.status(201).json({ message: "User created successfully", user });

} catch (error) {

Expand All @@ -33,14 +33,10 @@ const createUser = async (req: Request, res: Response) => {
const getUser = async (req: Request, res: Response) => {
try {
// Get email
const { email } = req.body;

if (!email) {
return res.status(400).json({ message: "Email not provided" });
}
const { id } = req.params;

// Find the user by email
const user = await userModel.findOne({ email });
const user = await userModel.findById(id);

// If user not found
if (!user) {
Expand All @@ -59,25 +55,16 @@ const getUser = async (req: Request, res: Response) => {
const updateUser = async (req: Request, res: Response) => {
try {
// Get email, username, password from body
const { username, email, password } = req.body;

// Check if email is provided
if (!email) {
return res.status(400).json({ message: "Email is required" });
}
const { id } = req.params;
const { newName } = req.body;

// Check if no data is provided for update
if (!username && !password) {
if (!newName) {
return res.status(400).json({ message: "No data provided for update" });
}

// Prepare the update data
const updateData: any = {};
if (username) updateData.username = username;
if (password) updateData.password = password;

// Find the user by email and update
const user = await userModel.findOneAndUpdate({ email }, updateData, { new: true });
const user = await userModel.findByIdAndUpdate(id, {username: newName}, { new: true });

if (!user) {
return res.status(404).json({ message: "User not found" });
Expand All @@ -94,22 +81,17 @@ const updateUser = async (req: Request, res: Response) => {

const deleteUser = async (req: Request, res: Response) => {
try {
const { username, email, password } = req.body;

// Check if email is provided
if (!username || !email || !password) {
return res.status(400).json({ message: "Information is required" });
}
const { id } = req.params;

// Find the user by email and delete
const user = await userModel.findOneAndDelete({ email, username, password });
const user = await userModel.findByIdAndDelete(id);

// If user not found
if (!user) {
return res.status(404).json({ message: "User not found" });
}

return res.status(204).json({ message: "User deleted successfully" });
return res.status(200).json({ message: "User deleted successfully" });

} catch (error) {

Expand All @@ -118,4 +100,18 @@ const deleteUser = async (req: Request, res: Response) => {
}
};

export { createUser, getUser, updateUser, deleteUser };
const getAllUsers = async (req: Request, res: Response) => {
try {
// Find all users
const users = await userModel.find();

return res.status(200).json({ message: "Users obtained successfully", users });

} catch (error) {

return res.status(500).json({ message: "Internal Server Error" });

}
}

export { createUser, getUser, updateUser, deleteUser, getAllUsers };
10 changes: 6 additions & 4 deletions backend/routes/userRoutes.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { Router } from "express";
import { createUser, getUser, updateUser, deleteUser } from "../controllers/UserController";
import { createUser, getUser, updateUser, deleteUser, getAllUsers } from "../controllers/UserController";

const userRoutes = Router();

userRoutes.post("/", createUser);

userRoutes.get("/", getUser);
userRoutes.get("/:id", getUser);

userRoutes.put("/", updateUser);
userRoutes.get("/", getAllUsers);

userRoutes.delete("/", deleteUser);
userRoutes.put("/:id", updateUser);

userRoutes.delete("/:id", deleteUser);

export default userRoutes;
39 changes: 19 additions & 20 deletions backend/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -395,21 +395,14 @@ csstype@^3.0.2:
resolved "https://registry.npmjs.org/csstype/-/csstype-3.1.3.tgz"
integrity sha512-M1uQkMl8rQK/szD0LNhtqxIPLpimGm8sOBwU7lLnCpSbTyY3yeU1Vc7l4KT5zT4s/yOxHH5O7tIuuLOCnLADRw==

debug@^4:
version "4.3.4"
resolved "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz"
integrity sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==
dependencies:
ms "2.1.2"

debug@2.6.9:
version "2.6.9"
resolved "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz"
integrity sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==
dependencies:
ms "2.0.0"

debug@4.x:
debug@4.x, debug@^4:
version "4.3.4"
resolved "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz"
integrity sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==
Expand Down Expand Up @@ -490,7 +483,9 @@ etag@~1.8.1:
integrity sha512-aIL5Fx7mawVa300al2BnEE4iNvo1qETxLrPI/o05L7z6go7fCw1J6EQmbK4FmJ2AS7kgVF/KEZWufBfdClMcPg==

express-cors@^0.0.3:
version "v0.0.3"
version "0.0.3"
resolved "https://registry.yarnpkg.com/express-cors/-/express-cors-0.0.3.tgz#5c25a78d7be69a4fcb08412cb27c8dfc758896bd"
integrity sha512-niRr2FV60JnzthAq6fmVsnvIuu6H+SQF5GcoIDMz9z3EF+sT+aGiiyWgeAj8Qj6VIXyX0NmuZQIqqcaQFvKsEg==

express@^4.19.2:
version "4.19.2"
Expand Down Expand Up @@ -568,6 +563,11 @@ fresh@0.5.2:
resolved "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz"
integrity sha512-zJ2mQYM18rEFOudeV4GShTGIQ7RbzA7ozbU9I/XBpm7kqgMywgmylMwXHxZJmkVoYkna9d2pVXVXPdYTP9ej8Q==

fsevents@~2.3.2:
version "2.3.3"
resolved "https://registry.yarnpkg.com/fsevents/-/fsevents-2.3.3.tgz#cac6407785d03675a2a5e1a5305c697b347d90d6"
integrity sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==

function-bind@^1.1.2:
version "1.1.2"
resolved "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz"
Expand Down Expand Up @@ -855,11 +855,6 @@ mquery@5.0.0:
dependencies:
debug "4.x"

ms@^2.1.1, ms@2.1.3:
version "2.1.3"
resolved "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz"
integrity sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==

ms@2.0.0:
version "2.0.0"
resolved "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz"
Expand All @@ -870,6 +865,11 @@ ms@2.1.2:
resolved "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz"
integrity sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==

ms@2.1.3, ms@^2.1.1:
version "2.1.3"
resolved "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz"
integrity sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==

negotiator@0.6.3:
version "0.6.3"
resolved "https://registry.npmjs.org/negotiator/-/negotiator-0.6.3.tgz"
Expand Down Expand Up @@ -932,9 +932,8 @@ parseurl@~1.3.3:
resolved "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz"
integrity sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ==

"passport-api@file:":
"passport-api@file:.":
version "0.0.0"
resolved "file:"
dependencies:
"@notionhq/client" "^2.2.15"
"@types/cors" "^2.8.17"
Expand All @@ -946,7 +945,7 @@ parseurl@~1.3.3:
express-cors "^0.0.3"
jsonwebtoken "^9.0.2"
mongoose "^8.1.1"
passport-api "file:"
passport-api "file:../../../../../AppData/Local/Yarn/Cache/v6/npm-passport-api-0.0.0-b03e9637-e343-4f77-be08-31a11a23cbfd-1724899323100/node_modules/passport-api"
ts-node "^10.9.2"
zod "^3.22.4"

Expand Down Expand Up @@ -1007,7 +1006,7 @@ readdirp@~3.6.0:
dependencies:
picomatch "^2.2.1"

safe-buffer@^5.0.1, safe-buffer@5.2.1:
safe-buffer@5.2.1, safe-buffer@^5.0.1:
version "5.2.1"
resolved "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz"
integrity sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==
Expand Down Expand Up @@ -1169,7 +1168,7 @@ type-is@~1.6.18:
media-typer "0.3.0"
mime-types "~2.1.24"

typescript@^5.3.3, typescript@>=2.7:
typescript@^5.3.3:
version "5.3.3"
resolved "https://registry.npmjs.org/typescript/-/typescript-5.3.3.tgz"
integrity sha512-pXWcraxM0uxAS+tN0AG/BF2TyqmHO014Z070UsJ+pFvYuRSq8KH8DmWpnbXe0pEPDHXZV3FcAbJkijJ5oNEnWw==
Expand All @@ -1184,7 +1183,7 @@ undici-types@~5.26.4:
resolved "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz"
integrity sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==

unpipe@~1.0.0, unpipe@1.0.0:
unpipe@1.0.0, unpipe@~1.0.0:
version "1.0.0"
resolved "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz"
integrity sha512-pjy2bYhSsufwWlKwPc+l3cN7+wuJlK6uz0YdJEOlQDbl6jo/YlPi4mb8agUkVC8BF7V8NuzeyPNqRksA3hztKQ==
Expand Down

0 comments on commit 8aa649a

Please sign in to comment.