-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: #18 - Added Mock Product Reviews Endpoints
- Loading branch information
Showing
6 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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 |
---|---|---|
@@ -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) | ||
}) | ||
|
||
} |
This file contains 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 |
---|---|---|
@@ -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; |
This file contains 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
This file contains 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 |
---|---|---|
@@ -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 | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const randomRating = () : number => { | ||
return Math.floor(Math.random() * 5) + 1; | ||
} | ||
|
||
export { | ||
randomRating | ||
} |