Skip to content

Conversation

@lunek1
Copy link

@lunek1 lunek1 commented May 17, 2024

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! Wrote some pointers on best practices to think about going forward 🌻

PS. In your frontend, you probably want to trigger a re-render after a new though has been posted

app.get("/thoughts", async (req, res) => {
try {
const thoughts = await Thought.find().sort({ createdAt: -1 }).limit(20);
res.json(thoughts);
Copy link
Contributor

Choose a reason for hiding this comment

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

Would be nice with a status code here too

const oldestThoughts = await Thought.find()
.sort({ createdAt: 1 })
.limit(20);
res.json(oldestThoughts);
Copy link
Contributor

Choose a reason for hiding this comment

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

and here (status code)

});

// GET /thoughts/oldest endpoint to return 20 oldest thoughts ordered by createdAt in ascending order
app.get("/thoughts/oldest", 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.

Better to use query params for these types of queries. Then you'd only need one endpoint that's named after what it returns (/thoughts) and the user can choose whether or not they want to add a sorting query, i.e. /thoughts would work but /thoughts?sort=desc would also work

if (!thought) {
return res.status(404).json({ error: "Thought not found" });
}
thought.hearts += 1;
Copy link
Contributor

Choose a reason for hiding this comment

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

Here you could also use $inc

});

// GET /thoughts/likes endpoint to retrieve all thoughts sorted by most likes to lowest
app.get("/thoughts/likes", 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.

Same with this, GET endpoints should be named after what they return, so this could also be a query under the /thoughts endpoint

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