Conversation
sandrahagevall
left a comment
There was a problem hiding this comment.
Great job Sara! I think you have done a well-structured and clear API, which is easy to follow. I don’t have much to add since your code works well, but I’ve included a few nice-to-have improvement suggestions :) Well done!
Side note: I noticed that your Render-link was missing in the pull-request.
|
|
||
| const router = express.Router(); | ||
|
|
||
| router.post("/user-signup", async (req, res) => { |
There was a problem hiding this comment.
Since the base route is already /users, you could make it more RESTful by using /signup, instead of /user-signup.
| } | ||
| }); | ||
|
|
||
| router.post("/user-login", async (req, res) => { |
There was a problem hiding this comment.
Same with this one, /login instead of /user-login
| if (user && bcrypt.compareSync(password, user.password)) { | ||
| res.json({ | ||
| success: true, | ||
| message: "Login successful", | ||
| response: { | ||
| username: user.username, | ||
| email: user.email, | ||
| userId: user._id, | ||
| accessToken: user.accessToken, | ||
| }, |
There was a problem hiding this comment.
Could explicitly use res.status(200).json for clarity and consistency with the error response.
| }); | ||
| } | ||
|
|
||
| const deletedThought = await Thoughts.findByIdAndDelete(id); |
There was a problem hiding this comment.
Everything works as expected here. As a nice-to-have improvement, you could link thoughts to the user who created them, making it possible to update or delete only your own thoughts.
| } | ||
|
|
||
| try { | ||
| const updatedThought = await Thoughts.findByIdAndUpdate( |
There was a problem hiding this comment.
This is what I referred to in the comment on R127
| const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; | ||
|
|
||
| if (!emailRegex.test(email)) { | ||
| return res.status(400).json({ | ||
| success: false, | ||
| message: "invalid email format", | ||
| }); | ||
| } |
There was a problem hiding this comment.
Nice that you have this check for the format of the email!
| const { id } = req.params; | ||
|
|
||
| try { | ||
| if (!mongoose.Types.ObjectId.isValid(id)) { |
There was a problem hiding this comment.
I like that you validate the id before fetching the thought 👍
https://happy-thoughts-api-11z5.onrender.com