Skip to content

Commit babba90

Browse files
Keagan ArominKeagan Aromin
authored andcommitted
Added get message + get users endpoints
1 parent 64de860 commit babba90

6 files changed

Lines changed: 55 additions & 2 deletions

File tree

backend/controllers/message.controller.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,26 @@ export const sendMessage = async (req, res) => {
4141
console.log("Error in sendMessage controller: ", error.message);
4242
res.status(500).json({ error: "Internal server error" });
4343
}
44-
};
44+
};
45+
46+
export const getMessages = async (req, res) => {
47+
try {
48+
const {id:userToChatId} = req.params.id; //get req.params.id and rename to userToChatId
49+
const senderId = req.user._id;
50+
51+
//conversation document, has array of mesage references as an attribute, use mongoose populate method to get actual contents of each message
52+
const conversation = await Conversation.findOne({
53+
participants: {$all: [senderId, userToChatId]},
54+
}).populate("messages");
55+
56+
//if not messages, successful request but returned array will be empty
57+
if (!conversation) return res.status(200).json([]);
58+
59+
const messages = conversation.messages;
60+
res.status(200).json(messages);
61+
62+
} catch (error) {
63+
console.log("Error in getMessage controller: ", error.message);
64+
res.status(500).json({ error: "Internal server error" });
65+
}
66+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import User from "../models/user.model.js"
2+
3+
export const getUsersForSidebar = async (req, res) => {
4+
try {
5+
const loggedInUserId = req.user._id
6+
//User.find finds ALL users rather than ONE | $ne = not equal | .select("-password") excludes password attribute from each user in json response
7+
// we are finding any users besides the current one
8+
const filteredUsers = await User.find({ _id: {$ne: loggedInUserId}})
9+
res.status(200).json(filteredUsers);
10+
} catch (error) {
11+
console.error("Error in getUsersForSidebar: ", error.message)
12+
res.status(500).json({error: "Internal server error"})
13+
}
14+
}

backend/middleware/protectRoute.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const protectRoute = async (req, res, next) => {
2626
}
2727

2828
//assign request user attribute to the user we decoded using mongoose model + jwt
29+
//because of this line of code, we can use req.user._id instead of req.user.Id
2930
req.user = user;
3031

3132
//this says that once everything has run, exit this function and call the next one

backend/routes/message.routes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import protectRoute from "../middleware/protectRoute";
55
const router = express.Router();
66

77
//when we have a post request, we first run protectRoute until the next() call is reached, then we run sendMessage
8+
//protectRoute makes sure that only authenticated users can send a message
89
router.post("/send/:id", protectRoute, sendMessage)
910

1011
export default router;

backend/routes/user.routes.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import express from "express";
2+
import protectRoute from "../middleware/protectRoute.js";
3+
import { getUsersForSidebar } from "../controllers/user.controller.js";
4+
5+
const router = express.Router();
6+
7+
//when we have a get request, we first run protectRoute until the next() call is reached, then we run getUsersForSidebar
8+
//protectRoute makes sure that only authenticated users can get users
9+
router.get("/", protectRoute, getUsersForSidebar)
10+
11+
export default router;

backend/server.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import express from "express";
22
import dotenv from "dotenv";
33
import cookieParser from "cookie-parser";
4+
45
import authRoutes from "./routes/auth.routes.js";
56
import messageRoutes from "./routes/auth.routes.js";
67
import userRoutes from "./routes/auth.routes.js";
8+
79
import connectToMongoDB from "./db/connectToMongoDB.js";
810

911
const app = express();
@@ -26,4 +28,6 @@ app.use("/api/users", userRoutes);
2628
app.listen(PORT, () => {
2729
connectToMongoDB();
2830
console.log(`Server Running on port ${PORT}`)
29-
});
31+
});
32+
33+
//The reason we separate auth route + controller from user route + controller is that authentication is its own service & we want to keep our app structure clean

0 commit comments

Comments
 (0)