User routes

This commit is contained in:
Andrew Trieu
2023-07-10 11:15:29 +03:00
committed by Andrew Trieu
parent 8b276dbadb
commit 6c35f7d006
4 changed files with 130 additions and 0 deletions

View File

@@ -9,3 +9,4 @@ Table:
| 07.07.2023 | Brainstorming and initialize project | 2 |
| 07.07.2023 | Setting up database | 2 |
| 07.07.2023 | Authentication and authorization | 6 |
| 10.07.2023 | User routes | 4 |

101
server/controllers/users.js Normal file
View File

@@ -0,0 +1,101 @@
import User from "../models/User";
/**
* Read - GET
*/
export const getUser = async (req, res) => {
try {
const { id } = req.params;
const user = await User.findById(id);
res.status(200).json(user);
} catch (error) {
res.status(404).json({ error: error.message });
}
};
export const getFriends = async (req, res) => {
try {
const { id } = req.params;
const user = await User.findById(id);
const friends = await Promise.all(
user.friends.map((id) => User.findById(id))
);
const friendList = friends.map(
({
_id,
firstName,
lastName,
profilePicturePath,
location,
description,
admin,
}) => {
return {
_id,
firstName,
lastName,
profilePicturePath,
location,
description,
admin,
};
}
);
res.status(200).json(friendList);
} catch (error) {
res.status(404).json({ error: error.message });
}
};
/**
* Update - PATCH
*/
export const patchFriend = async (req, res) => {
try {
const { id, friendId } = req.params;
const user = await User.findById(id);
const friend = await User.findById(friendId);
if (user.friends.includes(friendId)) {
user.friends = user.friends.filter((id) => id !== friendId);
friend.friends = friend.friends.filter((id) => id !== id);
} else {
user.friends.push(friendId);
friend.friends.push(id);
}
await user.save();
await friend.save();
const friends = await Promise.all(
user.friends.map((id) => User.findById(id))
);
const friendList = friends.map(
({
_id,
firstName,
lastName,
profilePicturePath,
location,
description,
admin,
}) => {
return {
_id,
firstName,
lastName,
profilePicturePath,
location,
description,
admin,
};
}
);
res.status(200).json(friendList);
} catch (error) {
res.status(404).json({ error: error.message });
}
};

View File

@@ -8,9 +8,15 @@ import helmet from "helmet";
import morgan from "morgan";
import path from "path";
import { fileURLToPath } from "url";
/* Routes */
import authRoutes from "./routes/auth.js";
import userRoutes from "./routes/user.js";
import postRoutes from "./routes/post.js";
/* Controllers */
import { register } from "./controllers/auth.js";
import { createPost } from "./controllers/post.js";
/* Middlewares */
import { verifyToken } from "./middlewares/auth.js";
/**
* Config
@@ -45,6 +51,10 @@ const upload = multer({ storage });
* Routes
* */
app.post("/auth/register", upload.single("profilePicture"), register);
app.post("/posts", verifyToken, upload.single("image"), createPost);
app.use("/auth", authRoutes);
app.use("/users", userRoutes);
app.use("/posts", postRoutes);
/**
* Database

18
server/routes/users.js Normal file
View File

@@ -0,0 +1,18 @@
import express from "express";
import { getUser, getFriends, patchFriend } from "../controllers/users.js";
import { verifyToken } from "../middlewares/auth.js";
const router = express.Router();
/**
* Read - GET
*/
router.get("/:id", verifyToken, getUser);
router.get("/:id/friends", verifyToken, getFriends);
/**
* Update - PATCH
*/
router.patch("/:id/:friendId", verifyToken, patchFriend);
export default router;