generated from Technigo/express-api-starter
-
Notifications
You must be signed in to change notification settings - Fork 530
Maryyy_ux_project-express-api #527
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
Open
Maryyy-ux
wants to merge
3
commits into
Technigo:master
Choose a base branch
from
Maryyy-ux:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,13 @@ | ||
| # Project Express API | ||
|
|
||
| Replace this readme with your own information about your project. | ||
| Week 17th project. Build an API with Express.js. And deploy on Render (first time). | ||
|
|
||
| Start by briefly describing the assignment in a sentence or two. Keep it short and to the point. | ||
| Using REST (Http method + path) and the results displayed must be: an array, a single register, or a failure (incomplete data). | ||
|
|
||
| ## 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? | ||
| It was my first time on back end and I feel very confortable on that side (server, etc). If I will have more time I would like to style and create a page to show the results properly, but the final project is close! | ||
|
|
||
| ## View it live | ||
|
|
||
| Every project should be deployed somewhere. Be sure to include the link to the deployed project so that the viewer can click around and see what it's all about. | ||
| (https://maryyy-ux-project-express-api.onrender.com) |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,4 +16,4 @@ | |
| "express": "^4.17.3", | ||
| "nodemon": "^3.0.1" | ||
| } | ||
| } | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,30 +1,92 @@ | ||
| import express from "express"; | ||
| import cors from "cors"; | ||
| import booksData from "./data/books.json"; | ||
|
|
||
| // If you're using one of our datasets, uncomment the appropriate import below | ||
| // to get started! | ||
| // import avocadoSalesData from "./data/avocado-sales.json"; | ||
| // import booksData from "./data/books.json"; | ||
| // import goldenGlobesData from "./data/golden-globes.json"; | ||
| // import netflixData from "./data/netflix-titles.json"; | ||
| // import topMusicData from "./data/top-music.json"; | ||
|
|
||
| // Defines the port the app will run on. Defaults to 8080, but can be overridden | ||
| // when starting the server. Example command to overwrite PORT env variable value: | ||
| // PORT=9000 npm start | ||
| const port = process.env.PORT || 8080; | ||
| const app = express(); | ||
|
|
||
| // Add middlewares to enable cors and json body parsing | ||
| app.use(cors()); | ||
| app.use(express.json()); | ||
|
|
||
| // Start defining your routes here | ||
| // Documentación de la API | ||
| 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" }, | ||
| ], | ||
| }); | ||
| }); | ||
|
|
||
| // Start the server | ||
| // Obtener todos los libros | ||
| app.get("/books", (req, res) => { | ||
| res.json(booksData); | ||
| }); | ||
|
|
||
| // Endpoint para la paginación | ||
| 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, | ||
| }); | ||
| }); | ||
|
Comment on lines
+30
to
+55
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be a query param under the /books endpoint instead: |
||
|
|
||
| // Endpoint para buscar libros | ||
| 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); | ||
| }); | ||
|
Comment on lines
+58
to
+74
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This as well: |
||
|
|
||
| // Endpoint para obtener un libro por ID | ||
| app.get("/books/:id", (req, res) => { | ||
| const { id } = req.params; | ||
|
|
||
| const book = booksData.find((book) => book.bookID === parseInt(id)); // Cambié 'books' por 'booksData' | ||
|
|
||
| if (!book) { | ||
| return res.status(404).json({ error: `Book with ID ${id} not found` }); | ||
| } | ||
|
|
||
| res.json(book); | ||
| }); | ||
|
|
||
| // Iniciar el servidor | ||
| app.listen(port, () => { | ||
| console.log(`Server running on http://localhost:${port}`); | ||
| }); | ||
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.
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