Conversation
…ing with 404 responses.
…lters to one route with if/else statements for personality, fur length and commonality
HIPPIEKICK
left a comment
There was a problem hiding this comment.
Good job Anna! Just remember to keep it clean and RESTful until next time :)
| ## The problem | ||
|
|
||
| Describe how you approached to problem, and what tools and techniques you used to solve it. How did you plan? What technologies did you use? If you had more time, what would be next? | ||
| I looked a lot at Matildas live session where she had different examples of query parameters or route parameters and filtering or find methods. I think it was very straight forward but I had one problem with the find method and route paths, there was a conflict between the routes for breed and ID so I needed to add more unique path to the cats breed. "/cats/breed/:breed" instead of "/cats/:breed" which worked. |
There was a problem hiding this comment.
The most RESTful thing would've been to put the breed as a query param (/cats?breed=bengal) instead of creating its own route. This way, you've still named the endpoint after what it returns: cats (filtered on breed)
There was a problem hiding this comment.
I thought about that first but then it said in the requirements that we should have "A minimum of one endpoint to return a single result (single element)." so then I added it as a route parameter so I had 2 of the single element endpoints.
| // Combined cats query parameter filtering on all cats and cats with category query filter for personality, fur length and commonality. | ||
|
|
||
| app.get("/cats", (request, response) => { // http://localhost:9000/cats -route for displaying all cat breeds. | ||
| const {personality, fur_length, commonality}= request.query |
There was a problem hiding this comment.
Nice! Here you could've added breed as well
| if (cat) { | ||
| response.status(200).json(cat) | ||
| } else { | ||
| response.status(404).send("No cat found with that ID") | ||
| } |
| app.get ("/cats/breed/:breed", (request, response) => { | ||
| const breed = request.params.breed.trim().toLowerCase() | ||
|
|
||
| const cat = cats.find (cat => cat.breed.toLowerCase() === breed) //Find specific cat breed. |
There was a problem hiding this comment.
Could you think of a better variable name? 👀
There was a problem hiding this comment.
I thought breed was a good name since I only have one key in the json file called breed. But another suggestion could be requestedBreed.
| const cat = cats.find (cat => cat.breed.toLowerCase() === breed) //Find specific cat breed. | ||
| //https://project-express-api-cats.onrender.com/cats/breed/russian%20blue | ||
| if (cat) { | ||
| response.status (200).json (cat) |
There was a problem hiding this comment.
Nice that you included status codes! But both status and json are JS methods and therefor it should not be a space between method and ()
Netlify link
https://project-express-api-cats.onrender.com