-
Notifications
You must be signed in to change notification settings - Fork 516
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Erika's Happy Thoughts API #483
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good job and nice to see that you made it your own with the dislike endpoint. What do you think about having it as one endpoint and then using a query to specify exactly what the user wants to do? Something like:
/thoughts/mongoID?like=true
and
/thoughts/mongoID?like=false
or something, maybe with better naming. Just a thought.
Good work!
app.use((req, res, next) => { | ||
if (mongoose.connection.readyState === 1) { | ||
next() | ||
} else { | ||
res.status(503).json({ error: "Service unavailable." }) | ||
} | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
try { | ||
const thoughts = await Thought.find().sort({ createdAt: -1 }).limit(20).exec() | ||
if (thoughts.length > 0) { | ||
res.json(thoughts) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A status code would be nice here
} | ||
}) | ||
|
||
app.patch("/thoughts/:id/like", async (req, res) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice with patch 🪡
const thought = await Thought.findByIdAndUpdate( | ||
req.params.id, | ||
{ $inc: { hearts: 1 } }, { new: true, runValidators: true }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
⭐
app.patch("/thoughts/:id/dislike", async (req, res) => { | ||
try { | ||
const thought = await Thought.findByIdAndUpdate( | ||
req.params.id, | ||
{ $inc: { hearts: -1 } }, | ||
{ new: true, runValidators: true } | ||
) | ||
res.status(200).json(thought) | ||
} catch (error) { | ||
res.status(400).json({ | ||
success: false, | ||
response: error, | ||
message: "Could not dislike thought.", | ||
}) | ||
} | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haha, love this! ⭐ ⭐ ⭐
Netlify link
https://tejpex-happy-thoughts-api.onrender.com/
Collaborators
Add your collaborators here. Write their GitHub usernames in square brackets. If there's more than one, separate them with a comma, like this:
[github-username-1, github-username-2]