-
Notifications
You must be signed in to change notification settings - Fork 529
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
Maryyy_ux_project-express-api #527
base: master
Are you sure you want to change the base?
Conversation
Definite code added and changed branch from master (spanish) into main (english)
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 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
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" }, | ||
], | ||
}); | ||
}); |
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.
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
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, | ||
}); | ||
}); |
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.
This could be a query param under the /books endpoint instead: /books?page=1
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); | ||
}); |
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.
This as well: /books?search=harry
https://maryyy-ux-project-express-api.onrender.com