diff --git a/README.md b/README.md index 5241826b..728ec993 100644 --- a/README.md +++ b/README.md @@ -10,4 +10,4 @@ Describe how you approached to problem, and what tools and techniques you used t ## 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://project-express-api-17pi.onrender.com diff --git a/package.json b/package.json index f93ddb52..fcbf9202 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "@babel/preset-env": "^7.16.11", "cors": "^2.8.5", "express": "^4.17.3", + "express-list-endpoints": "^7.1.1", "nodemon": "^3.0.1" } } diff --git a/server.js b/server.js index b5fec6fe..c72ca0d3 100644 --- a/server.js +++ b/server.js @@ -1,19 +1,13 @@ import express from "express"; import cors from "cors"; - -// 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"; +import booksData from "./data/books.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(); +const listEndpoints = require("express-list-endpoints"); // Add middlewares to enable cors and json body parsing app.use(cors()); @@ -21,10 +15,61 @@ app.use(express.json()); // Start defining your routes here app.get("/", (req, res) => { - res.send("Hello Technigo!"); + const endpoints = listEndpoints(app); + res.json({ + message: "Welcome to the Book API!", + endpoints: endpoints + }); +}); + +// Popular books with rating >4 +app.get("/books/popular", (req, res) => { + const popularBooks = booksData.filter(book => book.average_rating && parseFloat(book.average_rating) > +4) + res.status(200).json(popularBooks) }); +// author and title +app.get("/books", (req, res) => { + const { title, author } = req.query; + + let filteredBooks = booksData; + + if (title) { + filteredBooks = filteredBooks.filter(book => + book.title.toLowerCase().includes(title.toLowerCase()) + ); + } + + if (author) { + filteredBooks = filteredBooks.filter(book => { + if (!book.authors) return false; + + const authorsArray = book.authors.split("-").map(author => author.trim().toLowerCase()); + return authorsArray.some(auth => auth.includes(author.toLowerCase())); + }); + } + + if (filteredBooks.length === 0) { + return res.status(404).json({ error: "No books found matching the criteria" }); + } + + res.status(200).json(filteredBooks); +}); + +// ISBN +app.get("/books/:isbn", (req, res) => { + const isbn = req.params.isbn + const book = booksData.find(book => book.isbn === +isbn) + res.json(book); { + if (book) { + res.status(200).json(book) + } else { + res.status(404).send("no book found with that ISBN") + } + } +}) + // Start the server app.listen(port, () => { console.log(`Server running on http://localhost:${port}`); -}); +}); \ No newline at end of file