From 22b076c886afc623aa9fc040dbafb8302b00a343 Mon Sep 17 00:00:00 2001 From: Emelie Nyberg Date: Thu, 5 Dec 2024 15:32:33 +0100 Subject: [PATCH 1/4] requirements are met --- data/nintendoswitch-games.json | 212 +++++++++++++++++++++++++++++++++ package.json | 5 +- server.js | 51 +++++++- 3 files changed, 265 insertions(+), 3 deletions(-) create mode 100644 data/nintendoswitch-games.json diff --git a/data/nintendoswitch-games.json b/data/nintendoswitch-games.json new file mode 100644 index 000000000..5ddd8b5b1 --- /dev/null +++ b/data/nintendoswitch-games.json @@ -0,0 +1,212 @@ +[ + { + "id": 1, + "name": "The Legend of Zelda: Breath of the Wild", + "category": "Action-Adventure", + "release_year": 2017, + "rating": 9.5 + }, + { + "id": 2, + "name": "Super Mario Odyssey", + "category": "Platformer", + "release_year": 2017, + "rating": 9.4 + }, + { + "id": 3, + "name": "Mario Kart 8 Deluxe", + "category": "Racing", + "release_year": 2017, + "rating": 9.3 + }, + { + "id": 4, + "name": "Super Smash Bros. Ultimate", + "category": "Fighting", + "release_year": 2018, + "rating": 9.2 + }, + { + "id": 5, + "name": "Animal Crossing: New Horizons", + "category": "Simulation", + "release_year": 2020, + "rating": 9.1 + }, + { + "id": 6, + "name": "The Legend of Zelda: Tears of the Kingdom", + "category": "Action-Adventure", + "release_year": 2023, + "rating": 9.0 + }, + { + "id": 7, + "name": "Metroid Dread", + "category": "Action", + "release_year": 2021, + "rating": 8.9 + }, + { + "id": 8, + "name": "Splatoon 2", + "category": "Shooter", + "release_year": 2017, + "rating": 8.8 + }, + { + "id": 9, + "name": "Fire Emblem: Three Houses", + "category": "Strategy", + "release_year": 2019, + "rating": 8.7 + }, + { + "id": 10, + "name": "Xenoblade Chronicles 2", + "category": "Role-Playing", + "release_year": 2017, + "rating": 8.6 + }, + { + "id": 11, + "name": "Luigi's Mansion 3", + "category": "Action-Adventure", + "release_year": 2019, + "rating": 8.5 + }, + { + "id": 12, + "name": "Pokémon Sword and Shield", + "category": "Role-Playing", + "release_year": 2019, + "rating": 8.4 + }, + { + "id": 13, + "name": "Super Mario Party", + "category": "Party", + "release_year": 2018, + "rating": 8.3 + }, + { + "id": 14, + "name": "Octopath Traveler", + "category": "Role-Playing", + "release_year": 2018, + "rating": 8.2 + }, + { + "id": 15, + "name": "Hades", + "category": "Action", + "release_year": 2020, + "rating": 8.1 + }, + { + "id": 16, + "name": "The Witcher 3: Wild Hunt - Complete Edition", + "category": "Role-Playing", + "release_year": 2019, + "rating": 8.0 + }, + { + "id": 17, + "name": "Celeste", + "category": "Platformer", + "release_year": 2018, + "rating": 7.9 + }, + { + "id": 18, + "name": "Hollow Knight", + "category": "Action-Adventure", + "release_year": 2018, + "rating": 7.8 + }, + { + "id": 19, + "name": "Monster Hunter Rise", + "category": "Action", + "release_year": 2021, + "rating": 7.7 + }, + { + "id": 20, + "name": "Ring Fit Adventure", + "category": "Fitness", + "release_year": 2019, + "rating": 7.6 + }, + { + "id": 21, + "name": "Splatoon 3", + "category": "Shooter", + "release_year": 2022, + "rating": 7.5 + }, + { + "id": 22, + "name": "Mario + Rabbids Kingdom Battle", + "category": "Strategy", + "release_year": 2017, + "rating": 7.4 + }, + { + "id": 23, + "name": "Donkey Kong Country: Tropical Freeze", + "category": "Platformer", + "release_year": 2018, + "rating": 7.3 + }, + { + "id": 24, + "name": "Pokémon Legends: Arceus", + "category": "Role-Playing", + "release_year": 2022, + "rating": 7.2 + }, + { + "id": 25, + "name": "Kirby and the Forgotten Land", + "category": "Platformer", + "release_year": 2022, + "rating": 7.1 + }, + { + "id": 26, + "name": "Bayonetta 2", + "category": "Action", + "release_year": 2018, + "rating": 7.0 + }, + { + "id": 27, + "name": "Super Mario Maker 2", + "category": "Platformer", + "release_year": 2019, + "rating": 6.9 + }, + { + "id": 28, + "name": "Xenoblade Chronicles: Definitive Edition", + "category": "Role-Playing", + "release_year": 2020, + "rating": 6.8 + }, + { + "id": 29, + "name": "Astral Chain", + "category": "Action", + "release_year": 2019, + "rating": 6.7 + }, + { + "id": 30, + "name": "Pokémon Let's Go, Pikachu! and Let's Go, Eevee!", + "category": "Role-Playing", + "release_year": 2018, + "rating": 6.6 + } +] \ No newline at end of file diff --git a/package.json b/package.json index f93ddb524..5e8e8362c 100644 --- a/package.json +++ b/package.json @@ -11,9 +11,12 @@ "dependencies": { "@babel/core": "^7.17.9", "@babel/node": "^7.16.8", - "@babel/preset-env": "^7.16.11", "cors": "^2.8.5", "express": "^4.17.3", + "express-list-endpoints": "^7.1.1", "nodemon": "^3.0.1" + }, + "devDependencies": { + "@babel/preset-env": "^7.26.0" } } diff --git a/server.js b/server.js index b5fec6fe2..31220b924 100644 --- a/server.js +++ b/server.js @@ -1,5 +1,7 @@ import express from "express"; import cors from "cors"; +import listEndpoints from "express-list-endpoints"; + // If you're using one of our datasets, uncomment the appropriate import below // to get started! @@ -8,11 +10,12 @@ import cors from "cors"; // import goldenGlobesData from "./data/golden-globes.json"; // import netflixData from "./data/netflix-titles.json"; // import topMusicData from "./data/top-music.json"; +import videogames from "./data/nintendoswitch-games.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 port = process.env.PORT || 3000; const app = express(); // Add middlewares to enable cors and json body parsing @@ -21,7 +24,51 @@ app.use(express.json()); // Start defining your routes here app.get("/", (req, res) => { - res.send("Hello Technigo!"); + res.json(listEndpoints(app)); +}); + +// Endpoint that returns all the videogames +app.get("/videogames", (req, res) => { + const category = req.query.category; + const year = req.query.year; + + // Filter on category if category is sent + // For example http://localhost:3000/videogames?category=racing + if (category) { + const filterCategory = videogames.filter(game => game.category.toLowerCase() === category); + res.json(filterCategory); + + // Endpoint that returns a specific year + // For example http://localhost:3000/videogames?year=2020 + } if (year) { + const videogameByYear = videogames.filter(game => game.release_year === +year); + res.json(videogameByYear); + + // If no categori is sent, return all videogames + // For example http://localhost:3000/videogames + } else { + res.json(videogames); + }; +}); + +// Endpoint that returns one single videogame +// For example with id=3 http://localhost:3000/videogames/3 +app.get("/videogames/:id", (req, res) => { + const id = req.params.id; + const videogame = videogames.find(game => game.id === +id); + + if (videogame) { + res.json(videogame); + } else { + res.send("No videogame found with that ID"); + } +}); + +// Endpoint that returns a list sorted by highest rating +// For example http://localhost:3000/videogames/sorted/ratings +app.get("/videogames/sorted/ratings", (req, res) => { + const sortedGames = videogames.sort((a, b) => b.rating - a.rating); + res.json(sortedGames); }); // Start the server From 1068b343360d59e230365b3b0b705937c9da0284 Mon Sep 17 00:00:00 2001 From: Emelie Nyberg Date: Thu, 5 Dec 2024 15:42:59 +0100 Subject: [PATCH 2/4] added a readme file --- README.md | 28 +++++++++++++++++++++++----- server.js | 9 --------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 5241826b3..be8ecd494 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,31 @@ # Project Express API -Replace this readme with your own information about your project. - -Start by briefly describing the assignment in a sentence or two. Keep it short and to the point. +This project is an introduction to backend development using Express. The main goal is to create a RESTful API with at least a few endpoints that return data from a hardcoded JSON dataset. These endpoints can return either collections or individual items based on client requests. ## 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? +The challenge was to design and implement a functional API that adheres to RESTful principles. To achieve this, I planned the structure of the API by identifying potential endpoints and designing their responses based on user requirements. I used Node.js, Express, and array methods like `.find()`, `.filter()`, and `.sort()` to manipulate the JSON data effectively. + +I also added optional query parameters for filtering and sorting the dataset, making the API more dynamic and useful. If I had more time, I would consider adding: + +Pagination for large datasets. +A frontend to visualize and interact with the API. +A connection to a database for persistent data storage. ## 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. + + +## Features and Routes + +`GET /` +Returns API documentation, listing all available endpoints. + +`GET /videogames` +Returns all videogames or allows filtering by category (e.g., `?category=racing`) or release year (e.g., `?year=2020`). + +`GET /videogames/:id` +Returns details of a single videogame based on its unique ID. + +`GET /videogames/sorted/ratings` +Returns all videogames sorted by their rating, from highest to lowest. \ No newline at end of file diff --git a/server.js b/server.js index 31220b924..d25ce2d9f 100644 --- a/server.js +++ b/server.js @@ -1,15 +1,6 @@ import express from "express"; import cors from "cors"; import listEndpoints from "express-list-endpoints"; - - -// 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 videogames from "./data/nintendoswitch-games.json"; // Defines the port the app will run on. Defaults to 8080, but can be overridden From 7199f80e5372b0322d5c1fa1972896aa000d3f75 Mon Sep 17 00:00:00 2001 From: Emelie Nyberg Date: Thu, 5 Dec 2024 15:45:48 +0100 Subject: [PATCH 3/4] fixed some styling in readme file --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index be8ecd494..69263e05a 100644 --- a/README.md +++ b/README.md @@ -8,9 +8,9 @@ The challenge was to design and implement a functional API that adheres to RESTf I also added optional query parameters for filtering and sorting the dataset, making the API more dynamic and useful. If I had more time, I would consider adding: -Pagination for large datasets. -A frontend to visualize and interact with the API. -A connection to a database for persistent data storage. +- Pagination for large datasets. +- A frontend to visualize and interact with the API. +- A connection to a database for persistent data storage. ## View it live From a2d41058e912ad97a73525f67e4d854860195501 Mon Sep 17 00:00:00 2001 From: Emelie Nyberg Date: Fri, 6 Dec 2024 10:46:05 +0100 Subject: [PATCH 4/4] Render link inkluded --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 69263e05a..4f44ce225 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ I also added optional query parameters for filtering and sorting the dataset, ma ## View it live - +Render: https://project-express-api-ek.onrender.com ## Features and Routes