Skip to content

Conversation

@HeleneWestrin
Copy link

@HIPPIEKICK HIPPIEKICK self-assigned this 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.`,
},
],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 });
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea! ⭐ However, I think it would make sense with a PATCH req instead of PUT (since we're not replacing the whole resource)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great advice, thank you Matilda! 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants