Add skip parameter to /api/reviews
This commit is contained in:
parent
1a967876b8
commit
65382936cf
|
@ -21,15 +21,15 @@ export class MessagesResponder extends ApiRoute {
|
|||
}
|
||||
|
||||
// Same check as the above one, but for skip
|
||||
// if (req.query.skip && !+req.query.skip) {
|
||||
// res.writeHead(400, typeJson);
|
||||
// res.write(Responder.requestError("skip parameter must be a number or undefined"));
|
||||
// res.end();
|
||||
// return;
|
||||
// }
|
||||
if (req.query.skip && !+req.query.skip || +req.query.skip < 1) {
|
||||
res.writeHead(400, typeJson);
|
||||
res.write(Responder.requestError("skip parameter must be a number or undefined"));
|
||||
res.end();
|
||||
return;
|
||||
}
|
||||
|
||||
const receiver = req.headers["user-agent"];
|
||||
const result = data.getReviews(+req.query.max);
|
||||
const result = data.getReviews(+req.query.max, +req.query.skip);
|
||||
|
||||
res.writeHead(200, typeJson);
|
||||
res.write(JSON.stringify(result));
|
||||
|
|
|
@ -32,17 +32,18 @@ export class ReviewData {
|
|||
});
|
||||
}
|
||||
|
||||
public getReviews(max?: number): ServerSideReview[] {
|
||||
public getReviews(max?: number, skip?: number): ServerSideReview[] {
|
||||
let final: ServerSideReview[] = [];
|
||||
// If "max" isn't provided, is Infinity, or is greater than the amount of reviews
|
||||
const sendAllReviews = !max || max === Infinity || max > this.data.length;
|
||||
const useList = skip ? this.data.slice(skip, this.data.length) : this.data;
|
||||
|
||||
if (sendAllReviews) {
|
||||
for (const review of this.data) {
|
||||
for (const review of useList) {
|
||||
final.push(review);
|
||||
}
|
||||
} else {
|
||||
for (const review of this.data) {
|
||||
for (const review of useList) {
|
||||
while (final.length !== max) {
|
||||
final.push(review);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue