Happy Thoughts API Project - Asako#36
Conversation
| const sortOrder = order === "asc" ? 1 : -1 | ||
|
|
||
| try { | ||
| const messages = await Message.find(query).sort({ [sortField]: sortOrder }) |
There was a problem hiding this comment.
Really great work with combining the filtering and sorting! They work well combining together, and it's nice that you have some input validation and make sure they are always defined.
A maybe "nice-to-have thing" would be a message showing when there is nothing matching the filter criteria, so it's ultra clear for the user!
| const newMessage = await new Message({ | ||
| message: body.message, | ||
| hearts: 0, | ||
| userId: req.userId || null, // Set userId if authenticated, else null |
There was a problem hiding this comment.
Not sure if I'm thinking right here, but maybe you could just set null as default value of userId already in the user schema?
| if (message.likedByClients.includes(clientId)) { | ||
| return res.status(400).json({ |
There was a problem hiding this comment.
Nice error handling here to make sure a user can only like a thought once! Would be nice if that error message was the same for the user on the frontend, as the "failed to send like" got me a bit confused at first!
| router.patch("/:id/like", async (req, res) => { | ||
| const id = req.params.id | ||
| const authHeader = req.header("Authorization") | ||
| const clientId = req.header("X-Client-Id") |
There was a problem hiding this comment.
For a logged in user, will it also be a client ID (and not a user ID)? Or what is the authHeader for here? (just a question out of curiosity hehe :D)
gabriellaberko
left a comment
There was a problem hiding this comment.
Well done! The code is really organized in the files and easy to follow. Sorting and filtering the thoughts works great, and you are making sure to include proper error handling in all the routes.
Some really minor improvements that popped up in my mind during the code review was only to make some of the errors more clear towards the user on the front-end side, as they are on the backend/in the console.
Other than that, it looks and behaves great. You can tell that you understand how everything go together!
Render link - https://js-project-api-gx01.onrender.com/