Conversation
| }) | ||
|
|
||
| // Message model | ||
| const Message = mongoose.model('Message', messageSchema) |
There was a problem hiding this comment.
Looks really clean and easy to read 👍
I like that you added minlength validation for the message — that’s a nice touch to prevent empty or too short content.
Also good call setting default values for hearts and createdAt, it makes the model more robust and we don’t have to handle that manually every time.
The userId reference is set up properly with ref: 'User', so population should work smoothly.
Overall, solid schema structure, nothing overcomplicated — great job! 🚀
| }) | ||
|
|
||
| // User model | ||
| const User = mongoose.model('User', userSchema) |
There was a problem hiding this comment.
Everything looks simple and readable, no unnecessary stuff. Solid implementation, good job! 🚀
| } catch (error) { | ||
| res.status(500).json({ message: "Internal server error", error: error.message }) | ||
| } | ||
| } |
There was a problem hiding this comment.
Also, good job handling both cases: when the user exists and when the token is missing/invalid. Returning 401 with a clear message is very clear for the frontend.
In the future, you could add a small check to ensure req.header('Authorization') exists before calling .replace(), to avoid possible runtime errors.
| response: error, | ||
| }) | ||
| } | ||
| }) |
There was a problem hiding this comment.
This signup flow looks really solid 👍
Great job hashing the password with bcrypt before saving — that’s super important for security. Using salt makes it even better 🔐
I like that you’re returning only safe user data in the response (no password), that’s a very good practice.
The response structure is also clean and consistent with success, message, and response, which makes it easy to handle on the frontend.
Maybe in the future you could consider using the async versions of bcrypt methods (genSalt / hash) to avoid blocking, but overall this is a really nice implementation.
Good job, this looks production-ready to me 🚀
| } catch (error) { | ||
| res.status(500).json({ error: "Failed to get liked messages" }) | ||
| } | ||
| }) |
There was a problem hiding this comment.
Nice endpoint, very clear and focused 👍
| } catch (error) { | ||
| res.status(500).json({ error: "Failed to get messages including the word happy" }) | ||
| } | ||
| }) |
There was a problem hiding this comment.
I also like that you’re handling the case when no messages are found and returning a clear 404 response.
| } catch (error) { | ||
| res.status(400).json({ error: "could not update likes" }) | ||
| } | ||
| }) |
There was a problem hiding this comment.
Using $inc to increase hearts is a really good choice — it’s efficient and avoids race conditions compared to updating the value manually.
I like that you’re returning the updated document with { new: true }, that’s super practical for the frontend.
| } catch (error) { | ||
| res.status(400).json({ error: "Invalid message-id or request" }) | ||
| } | ||
| }) |
There was a problem hiding this comment.
Using || to fallback to the old message if no new message is provided is a neat little touch.
Overall, really clean and safe implementation — looks solid! 👏
| } | ||
|
|
||
| }) | ||
|
|
There was a problem hiding this comment.
The error handling is clean too, returning validation errors if something goes wrong.
| } catch (error) { | ||
| res.status(400).json({ error: "Invalid message id" }) | ||
| } | ||
| }) |
There was a problem hiding this comment.
Nice DELETE route 👍
I like that you’re using authenticateUser to make sure only logged-in users can delete messages — that’s super important 🔒
Checking both if the message exists (404) and if the user is the owner (403) is great for security and makes the API predictable.
Returning { success: true } on success is clean and easy for the frontend to handle.
Small tip: since you already used findByIdAndDelete, the extra deleteOne() might not be necessary, but overall the route is solid, readable, and safe 🚀
https://happy-thoughts-api-nicolina.onrender.com
Check it out connected to frontend here:
https://happy-thoughts-nicolina.netlify.app/