-
Notifications
You must be signed in to change notification settings - Fork 496
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
Martin - Happy Thoughts API #476
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.
Hej Martin! Your API is working as it should and your code looks clean! We used different status codes for the GET request so I just pointed that out. Good job! 😃 💌
// Get all the thoughts | ||
const thoughts = await Thought.find().sort( | ||
{ createdAt: "desc" }).limit(20).exec(); |
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.
I might be completely wrong here, but I just reacted because I did it differently. If there are errors thrown by "await Thought.find()", would they be handled? I'm thinking since it's outside of the try/catch?
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.
I'd also put it in the try block 👍
res.status(201).json(thoughts); | ||
} catch (err) { | ||
res.status(400).json({ | ||
message: "Could not retrieve the Happy 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.
Interesting, we used different status codes here 😄
In "try" I used 200 as I read it's a more general "OK" status for GET requests while 201 specifically means "created" when you have done a successful POST request.
In "catch" I used 500 which indicates server-side errors (such as database query error, ,which would likely be the case here).
But again, might be wrong here too! By no means I feel comfortable with these status codes, just find it interesting that we chose different ones :)
|
||
app.post("/thoughts/", async (req, res) => { | ||
// Create a thought in the database | ||
const { message, heart } = req.body; |
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.
Not so sure why heart would be a property here? I think it would be enough with "message" since "hearts" will not be edited in this endpoint 😊
const { thoughtId } = req.params; | ||
|
||
try { | ||
const thought = await Thought.findByIdAndUpdate(thoughtId, {$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.
This looks like a cool and smooth way to update by 1. Nice 🤩
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.
Agreed! Just missing a bit of formatting 😇
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 Martin! I just added some small comments to think about going forward 🌻
maxlength: 140, | ||
minlength: 5, |
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.
Why maxlength before minlength? My eeeeyes 😅
hearts: { type: Number, default: 0 }, | ||
createdAt: { type: Date, default: Date.now }, |
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.
Format these lines as you did with the message for better readability
// Get all the thoughts | ||
const thoughts = await Thought.find().sort( | ||
{ createdAt: "desc" }).limit(20).exec(); |
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.
I'd also put it in the try block 👍
const newThought = await thought.save(); | ||
res.status(201).json(newThought); |
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.post("/thoughts/", 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.
Minor thing, but no need for the second /
const { thoughtId } = req.params; | ||
|
||
try { | ||
const thought = await Thought.findByIdAndUpdate(thoughtId, {$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.
Agreed! Just missing a bit of formatting 😇
Netlify link
Back end: https://happy-thoughts-api-uu1o.onrender.com/
Front end: https://manyhappythoughts.netlify.app/