Open
Conversation
Definite code added and changed branch from master (spanish) into main (english)
HIPPIEKICK
approved these changes
Dec 10, 2024
Contributor
HIPPIEKICK
left a comment
There was a problem hiding this comment.
Good job creating your first API Maria! Just remember going forward to keep it RESTful by using query params instead of creating endpoints for different filters. Endpoints should be named after what they return and you're returning books in all of them
Comment on lines
12
to
22
| app.get("/", (req, res) => { | ||
| res.send("Hello Technigo!"); | ||
| res.json({ | ||
| message: "Welcome to the Books API", | ||
| routes: [ | ||
| { method: "GET", endpoint: "/books", description: "Get all books" }, | ||
| { method: "GET", endpoint: "/books/:id", description: "Get a single book by ID" }, | ||
| { method: "GET", endpoint: "/books/search", description: "Search books by query parameters" }, | ||
| { method: "GET", endpoint: "/books/page", description: "Get paginated books" }, | ||
| ], | ||
| }); | ||
| }); |
Contributor
There was a problem hiding this comment.
Next week you might want to look into Express List Endpoints (see instructions) so that you don't have to update your docs manually when you change something
Comment on lines
+30
to
+55
| app.get("/books/page", (req, res) => { | ||
| const { page = 1, limit = 10 } = req.query; | ||
|
|
||
| const pageNum = parseInt(page); | ||
| const limitNum = parseInt(limit); | ||
|
|
||
| if (isNaN(pageNum) || isNaN(limitNum) || pageNum <= 0 || limitNum <= 0) { | ||
| return res.status(400).json({ error: "Invalid page or limit parameters" }); | ||
| } | ||
|
|
||
| const startIndex = (pageNum - 1) * limitNum; | ||
| const endIndex = startIndex + limitNum; | ||
|
|
||
| const paginatedBooks = booksData.slice(startIndex, endIndex); // Aquí corregimos 'books' a 'booksData' | ||
|
|
||
| if (paginatedBooks.length === 0) { | ||
| return res.status(404).json({ error: "No books found for the given page" }); | ||
| } | ||
|
|
||
| res.json({ | ||
| page: pageNum, | ||
| limit: limitNum, | ||
| totalBooks: booksData.length, | ||
| books: paginatedBooks, | ||
| }); | ||
| }); |
Contributor
There was a problem hiding this comment.
This could be a query param under the /books endpoint instead: /books?page=1
Comment on lines
+58
to
+74
| app.get("/books/search", (req, res) => { | ||
| const { title } = req.query; | ||
|
|
||
| if (!title) { | ||
| return res.status(400).json({ error: "Query parameter 'title' is required" }); | ||
| } | ||
|
|
||
| const results = booksData.filter((book) => | ||
| book.title.toLowerCase().includes(title.toLowerCase()) // Cambié 'books' por 'booksData' | ||
| ); | ||
|
|
||
| if (results.length === 0) { | ||
| return res.status(404).json({ error: "No books found with the given title" }); | ||
| } | ||
|
|
||
| res.json(results); | ||
| }); |
Contributor
There was a problem hiding this comment.
This as well: /books?search=harry
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
https://maryyy-ux-project-express-api.onrender.com