diff --git a/app.ts b/app.ts index f489c51f..048ddfae 100644 --- a/app.ts +++ b/app.ts @@ -12,6 +12,7 @@ require('./modules/countries/api/countries-routes')(app); // Countries require('./modules/currency/api/currency-routes')(app); // Currencies require('./modules/images/api/images-routes')(app); // Images require('./modules/names/api/names-routes')(app); // Names +require('./modules/products/api/products-routes')(app); // Products require('./modules/sports/api/sports-routes')(app); // Sports require('./modules/users/api/user-routes')(app); // Users diff --git a/modules/products/api/products-routes.ts b/modules/products/api/products-routes.ts new file mode 100644 index 00000000..cbf468f2 --- /dev/null +++ b/modules/products/api/products-routes.ts @@ -0,0 +1,22 @@ +import { Request, Response } from 'express'; +import * as core from 'express-serve-static-core'; +import productReviews from '../data/product-reviews'; + +module.exports = function(app : core.Express){ + + // Returns a set of reviews + app.get("/products/reviews", (req: Request, res: Response) => { + res.json(productReviews) + }) + + // returns a set of reviews with a given rating + app.get("/products/reviews/rating/:rating", (req: Request, res: Response) => { + let reviews = productReviews; + const rating = parseInt(req.params.rating); + reviews.forEach(element => { + element.rating = rating + }); + res.json(reviews) + }) + +} \ No newline at end of file diff --git a/modules/products/data/product-reviews.ts b/modules/products/data/product-reviews.ts new file mode 100644 index 00000000..184bc9b3 --- /dev/null +++ b/modules/products/data/product-reviews.ts @@ -0,0 +1,55 @@ +import { getRandomDate } from "../../../utils/dates"; +import { randomRating } from "../../../utils/numbers"; +import getFullNames from "../../names/utils/getFullNames"; + +const productReviews = [ + + { + productName: "Super Gamer Juice", + productId: 1, + message: "The only drink I will ever need.", + dateTime: getRandomDate(new Date('1950-02-12T01:57:45.271Z'), new Date('2022-02-12T01:57:45.271Z')), + rating: randomRating(), + userName: getFullNames(1), + categories: ['sport-drinks','gaming'] + }, + { + productName: "Super Gamer Juice", + productId: 1, + message: "I couldn't have managed my 36 hour stint on Fallout 76, without the 4 litres of Super Gamer Juice I drank. My eyes burn and my spine is permanently damaged, but I levelled up so much", + dateTime: getRandomDate(new Date('1950-02-12T01:57:45.271Z'), new Date('2022-02-12T01:57:45.271Z')), + rating: randomRating(), + userName: getFullNames(1), + categories: ['sport-drinks','gaming'] + }, + { + productName: "Super Gamer Juice", + productId: 1, + message: "I can do this all day... thanks to Super Gamer Juice", + dateTime: getRandomDate(new Date('1950-02-12T01:57:45.271Z'), new Date('2022-02-12T01:57:45.271Z')), + rating: randomRating(), + userName: getFullNames(1), + categories: ['sport-drinks','gaming'] + }, + { + productName: "Super Gamer Juice", + productId: 1, + message: "After just a few sips I was that great at a game I was signed to an E Sports team", + dateTime: getRandomDate(new Date('1950-02-12T01:57:45.271Z'), new Date('2022-02-12T01:57:45.271Z')), + rating: randomRating(), + userName: getFullNames(1), + categories: ['sport-drinks','gaming'] + }, + { + productName: "Super Gamer Juice", + productId: 1, + message: "The flavour was not as I was expecting, but none the less I was instantly addicted. Can't get deliveries quick enough", + dateTime: getRandomDate(new Date('1950-02-12T01:57:45.271Z'), new Date('2022-02-12T01:57:45.271Z')), + rating: randomRating(), + userName: getFullNames(1), + categories: ['sport-drinks','gaming'] + } + +] + +export default productReviews; \ No newline at end of file diff --git a/swagger.json b/swagger.json index 1aca0384..1dd23950 100644 --- a/swagger.json +++ b/swagger.json @@ -31,6 +31,10 @@ "name": "Names", "description": "A set of endpoints to get randomly generated names" }, + { + "name": "Products", + "description": "A set of endpoints to get random product related info" + }, { "name": "Sports", "description": "A set of endpoints to get random sports data" @@ -1510,6 +1514,41 @@ } } }, + "/products/reviews" : { + "get": { + "tags": ["Products"], + "summary" : "Obtain a list of random reviews", + "responses": { + "200" : { + "description" : "OK", + "schema": { + "$ref": "#/definitions/MockProductReview" + } + } + } + } + }, + "/products/reviews/{rating}" : { + "get": { + "tags": ["Products"], + "summary" : "Obtain a list of random reviews with a specific rating", + "parameters": [ + { + "in": "path", + "name": "rating", + "description": "The rating you would like your mocked reviews to be" + } + ], + "responses": { + "200" : { + "description" : "OK", + "schema": { + "$ref": "#/definitions/MockProductReview" + } + } + } + } + }, "/sports/football/leagues/premier/teams" : { "get": { "tags": ["Sports"], @@ -1585,6 +1624,45 @@ } } } + }, + "MockProductReview" : { + "type" : "array", + "items": { + "type" : "object", + "properties": { + "productName" : { + "type" : "string", + "example" : "Super Gamer Juice" + }, + "productId" : { + "type" : "number", + "example" : 1 + }, + "message" : { + "type" : "string", + "example" : "The only drink I will ever need." + }, + "dateTime" : { + "type" : "string", + "example" : "1984-05-21T00:02:11.497Z" + }, + "rating" : { + "type" : "number", + "example" : 1 + }, + "userName" : { + "type" : "string", + "example" : "Aaron Rackley" + }, + "categories" : { + "type" : "array", + "items": { + "type" : "string", + "example" : "sport-drinks" + } + } + } + } } } } \ No newline at end of file diff --git a/utils/dates.ts b/utils/dates.ts new file mode 100644 index 00000000..f6295aba --- /dev/null +++ b/utils/dates.ts @@ -0,0 +1,9 @@ +function getRandomDate(from: Date, to: Date) { + const fromTime = from.getTime(); + const toTime = to.getTime(); + return new Date(fromTime + Math.random() * (toTime - fromTime)); +} + +export { + getRandomDate +} \ No newline at end of file diff --git a/utils/numbers.ts b/utils/numbers.ts new file mode 100644 index 00000000..8950372f --- /dev/null +++ b/utils/numbers.ts @@ -0,0 +1,7 @@ +const randomRating = () : number => { + return Math.floor(Math.random() * 5) + 1; +} + +export { + randomRating +} \ No newline at end of file