Add max parameter to /api/reviews
This commit is contained in:
parent
9843459d22
commit
1a967876b8
|
@ -1,6 +1,6 @@
|
|||
import { Express, Request, Response } from "express";
|
||||
import { Express, Response } from "express";
|
||||
import { Logger, Responder, data } from "../utils";
|
||||
import { IdRequest, typeJson } from "../types";
|
||||
import { IdRequest, ReviewRequest, typeJson } from "../types";
|
||||
import ApiRoute from "../utils/ApiRoute";
|
||||
|
||||
export class MessagesResponder extends ApiRoute {
|
||||
|
@ -10,9 +10,26 @@ export class MessagesResponder extends ApiRoute {
|
|||
}
|
||||
|
||||
public async start(): Promise<void> {
|
||||
this.server.get(this.routeName, (req: Request, res: Response) => {
|
||||
this.server.get(this.routeName, (req: ReviewRequest, res: Response) => {
|
||||
// This is a mouthful. :sigh:
|
||||
// If the request parameter "max" is present and it isn't a number
|
||||
if (req.query.max && !+req.query.max || +req.query.max < 1) {
|
||||
res.writeHead(400, typeJson);
|
||||
res.write(Responder.requestError("max parameter must be a positive non-zero number or undefined"));
|
||||
res.end();
|
||||
return;
|
||||
}
|
||||
|
||||
// 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;
|
||||
// }
|
||||
|
||||
const receiver = req.headers["user-agent"];
|
||||
const result = data.getReviews();
|
||||
const result = data.getReviews(+req.query.max);
|
||||
|
||||
res.writeHead(200, typeJson);
|
||||
res.write(JSON.stringify(result));
|
||||
|
|
|
@ -34,3 +34,10 @@ export interface IdRequest extends Request {
|
|||
id: string;
|
||||
};
|
||||
}
|
||||
|
||||
export interface ReviewRequest extends Request {
|
||||
query: {
|
||||
max: string;
|
||||
skip: string;
|
||||
};
|
||||
};
|
||||
|
|
|
@ -6,6 +6,7 @@ import {
|
|||
UserSideReview,
|
||||
} from "../types";
|
||||
import { toServerReview, checkFile, stripId } from "./functions";
|
||||
import { Logger } from "./logger";
|
||||
|
||||
export class ReviewData {
|
||||
public data: ServerSideReview[];
|
||||
|
@ -31,11 +32,21 @@ export class ReviewData {
|
|||
});
|
||||
}
|
||||
|
||||
public getReviews(): ServerSideReview[] {
|
||||
public getReviews(max?: 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;
|
||||
|
||||
for (const review of this.data) {
|
||||
final.push(review);
|
||||
if (sendAllReviews) {
|
||||
for (const review of this.data) {
|
||||
final.push(review);
|
||||
}
|
||||
} else {
|
||||
for (const review of this.data) {
|
||||
while (final.length !== max) {
|
||||
final.push(review);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return final;
|
||||
|
|
Loading…
Reference in a new issue