Skip to content

Commit

Permalink
feature: #18 - Added Mock Product Reviews Endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
ageddesi committed Sep 11, 2022
1 parent e21a54c commit 5deefb5
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 0 deletions.
1 change: 1 addition & 0 deletions app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 22 additions & 0 deletions modules/products/api/products-routes.ts
Original file line number Diff line number Diff line change
@@ -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)
})

}
55 changes: 55 additions & 0 deletions modules/products/data/product-reviews.ts
Original file line number Diff line number Diff line change
@@ -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;
78 changes: 78 additions & 0 deletions swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"],
Expand Down Expand Up @@ -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"
}
}
}
}
}
}
}
9 changes: 9 additions & 0 deletions utils/dates.ts
Original file line number Diff line number Diff line change
@@ -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
}
7 changes: 7 additions & 0 deletions utils/numbers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const randomRating = () : number => {
return Math.floor(Math.random() * 5) + 1;
}

export {
randomRating
}

0 comments on commit 5deefb5

Please sign in to comment.