Expres.js and MongoDB-backed Happy Thoughts#506
Open
HeleneWestrin wants to merge 6 commits intoTechnigo:masterfrom
Open
Expres.js and MongoDB-backed Happy Thoughts#506HeleneWestrin wants to merge 6 commits intoTechnigo:masterfrom
HeleneWestrin wants to merge 6 commits intoTechnigo:masterfrom
Conversation
HIPPIEKICK
approved these changes
Jan 9, 2025
Comment on lines
+19
to
+34
| validate: [ | ||
| { | ||
| validator: function (value) { | ||
| return value.length >= 5; | ||
| }, | ||
| message: (props) => | ||
| `Message must be at least 5 characters. Your message has ${props.value.length} characters.`, | ||
| }, | ||
| { | ||
| validator: function (value) { | ||
| return value.length <= 140; | ||
| }, | ||
| message: (props) => | ||
| `The maximum length of a message is 140 characters. Your message has ${props.value.length} characters.`, | ||
| }, | ||
| ], |
Comment on lines
+90
to
+119
| app.put("/thoughts/:id/like", async (req, res) => { | ||
| const { id } = req.params; | ||
| const { action } = req.body; // Expecting "add" or "remove" in the request body | ||
|
|
||
| if (!["add", "remove"].includes(action)) { | ||
| return res | ||
| .status(400) | ||
| .json({ error: "Invalid action. Use 'add' or 'remove'." }); | ||
| } | ||
|
|
||
| try { | ||
| const update = | ||
| action === "add" ? { $inc: { hearts: 1 } } : { $inc: { hearts: -1 } }; | ||
|
|
||
| const updatedThought = await Thought.findByIdAndUpdate( | ||
| id, // Find the thought by its ID | ||
| update, // Increment or decrement the `hearts` field | ||
| { new: true, runValidators: true } // Return the updated document | ||
| ); | ||
|
|
||
| if (!updatedThought) { | ||
| return res.status(404).json({ error: "Thought not found" }); | ||
| } | ||
|
|
||
| res.status(200).json(updatedThought); | ||
| } catch (err) { | ||
| res | ||
| .status(500) | ||
| .json({ error: "Failed to update like", details: err.message }); | ||
| } |
Contributor
There was a problem hiding this comment.
Good idea! ⭐ However, I think it would make sense with a PATCH req instead of PUT (since we're not replacing the whole resource)
Author
There was a problem hiding this comment.
Great advice, thank you Matilda! 🙏
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Links
View API »
View Frontend Site »