Skip to content

Conversation

@Maryyy-ux
Copy link

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 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" },
],
});
});
Copy link
Contributor

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

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,
});
});
Copy link
Contributor

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

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);
});
Copy link
Contributor

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

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.

2 participants