Skip to content
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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

Martin-Joensson
Copy link

Copy link

@almaherris almaherris left a 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! 😃 💌

Comment on lines +42 to +44
// Get all the thoughts
const thoughts = await Thought.find().sort(
{ createdAt: "desc" }).limit(20).exec();

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?

Copy link
Contributor

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 👍

Comment on lines +47 to +50
res.status(201).json(thoughts);
} catch (err) {
res.status(400).json({
message: "Could not retrieve the Happy Thoughts.",

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;

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});

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 🤩

Copy link
Contributor

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 😇

Copy link
Contributor

@HIPPIEKICK HIPPIEKICK left a 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 🌻

Comment on lines +28 to +29
maxlength: 140,
minlength: 5,
Copy link
Contributor

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 😅

Comment on lines +31 to +32
hearts: { type: Number, default: 0 },
createdAt: { type: Date, default: Date.now },
Copy link
Contributor

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

Comment on lines +42 to +44
// Get all the thoughts
const thoughts = await Thought.find().sort(
{ createdAt: "desc" }).limit(20).exec();
Copy link
Contributor

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 👍

Comment on lines +61 to +62
const newThought = await thought.save();
res.status(201).json(newThought);
Copy link
Contributor

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) => {
Copy link
Contributor

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

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 😇

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.

3 participants