-
Notifications
You must be signed in to change notification settings - Fork 514
project-happy-thoughts-api #477
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
Conversation
HIPPIEKICK
left a comment
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! 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); |
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.
Would be nice with a status code here too
| const oldestThoughts = await Thought.find() | ||
| .sort({ createdAt: 1 }) | ||
| .limit(20); | ||
| res.json(oldestThoughts); |
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.
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) => { |
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.
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; |
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.
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) => { |
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.
Same with this, GET endpoints should be named after what they return, so this could also be a query under the /thoughts endpoint
Render & Netlify link
Backend: https://project-happy-thoughts-api-qgyf.onrender.com/
Frontend: https://celebrated-shortbread-0673ce.netlify.app/
Collaborators
Solo