Skip to content

Commit

Permalink
feat: finished homework
Browse files Browse the repository at this point in the history
  • Loading branch information
ayepricots committed Aug 30, 2024
1 parent 266247d commit f3815a3
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 56 deletions.
93 changes: 80 additions & 13 deletions backend/controllers/UserController.ts
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 };
26 changes: 12 additions & 14 deletions backend/models/User.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import { Schema } from 'mongoose';
import { Schema } from 'mongoose';
import mongoose from "mongoose";

const userSchema = new Schema({
username: {
type: String,
required: true,
unique: true,
},
password: {
type: String,
required: true,
},
createdAt: {
type: Date,
default: Date.now,
},
username: {
type: String,
required: true,
unique: true,
},
password: {
type: String,
required: true,
},
}, {
timestamps: true,
});

const User = mongoose.model("User", userSchema);
Expand Down
6 changes: 5 additions & 1 deletion backend/routes/userRoutes.ts
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;
56 changes: 28 additions & 28 deletions backend/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -394,21 +394,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 @@ -483,7 +476,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.18.2:
version "4.18.2"
Expand Down Expand Up @@ -561,6 +556,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 @@ -814,19 +814,19 @@ mongodb-connection-string-url@^3.0.0:
"@types/whatwg-url" "^11.0.2"
whatwg-url "^13.0.0"

mongodb@^6.8.0:
version "6.8.0"
resolved "https://registry.npmjs.org/mongodb/-/mongodb-6.8.0.tgz"
integrity sha512-HGQ9NWDle5WvwMnrvUxsFYPd3JEbqD3RgABHBQRuoCEND0qzhsd0iH5ypHsf1eJ+sXmvmyKpP+FLOKY8Il7jMw==
mongodb@6.7.0:
version "6.7.0"
resolved "https://registry.npmjs.org/mongodb/-/mongodb-6.7.0.tgz"
integrity sha512-TMKyHdtMcO0fYBNORiYdmM25ijsHs+Njs963r4Tro4OQZzqYigAzYQouwWRg4OIaiLRUEGUh/1UAcH5lxdSLIA==
dependencies:
"@mongodb-js/saslprep" "^1.1.5"
bson "^6.7.0"
mongodb-connection-string-url "^3.0.0"

mongodb@6.7.0:
version "6.7.0"
resolved "https://registry.npmjs.org/mongodb/-/mongodb-6.7.0.tgz"
integrity sha512-TMKyHdtMcO0fYBNORiYdmM25ijsHs+Njs963r4Tro4OQZzqYigAzYQouwWRg4OIaiLRUEGUh/1UAcH5lxdSLIA==
mongodb@^6.8.0:
version "6.8.0"
resolved "https://registry.npmjs.org/mongodb/-/mongodb-6.8.0.tgz"
integrity sha512-HGQ9NWDle5WvwMnrvUxsFYPd3JEbqD3RgABHBQRuoCEND0qzhsd0iH5ypHsf1eJ+sXmvmyKpP+FLOKY8Il7jMw==
dependencies:
"@mongodb-js/saslprep" "^1.1.5"
bson "^6.7.0"
Expand Down Expand Up @@ -857,11 +857,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 @@ -872,6 +867,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 @@ -934,9 +934,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 @@ -947,8 +946,9 @@ parseurl@~1.3.3:
express "^4.18.2"
express-cors "^0.0.3"
jsonwebtoken "^9.0.2"
mongodb "^6.8.0"
mongoose "^8.5.4"
passport-api "file:"
passport-api "file:../../../../../Users/ayemy/AppData/Local/Yarn/Cache/v6/npm-passport-api-0.0.0-87e3f761-09af-4d8c-a8ae-7d867aabea5c-1725001731449/node_modules/passport-api"
ts-node "^10.9.2"
zod "^3.22.4"

Expand Down Expand Up @@ -1009,7 +1009,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 @@ -1171,7 +1171,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 @@ -1186,7 +1186,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 f3815a3

Please sign in to comment.