From 72cc7e62d2ea2ee0a9d32f8357f7350336eb15eb Mon Sep 17 00:00:00 2001 From: Maciej Ziarkowski Date: Fri, 31 Jan 2020 15:45:12 +0000 Subject: [PATCH] Add param parsing to edit history controller --- .../api/controllers/editHistoryController.ts | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/app/src/api/controllers/editHistoryController.ts b/app/src/api/controllers/editHistoryController.ts index c266e7f5..b6897c28 100644 --- a/app/src/api/controllers/editHistoryController.ts +++ b/app/src/api/controllers/editHistoryController.ts @@ -1,14 +1,24 @@ import express from 'express'; +import { UserInputError } from '../errors'; +import { checkRegexParam, parsePositiveIntParam, processParam } from '../parameters'; import asyncController from "../routes/asyncController"; import * as editHistoryService from '../services/editHistory'; const getGlobalEditHistory = asyncController(async (req: express.Request, res: express.Response) => { + + const revisionIdRegex = /^[1-9]\d*$/; + const afterId: string = processParam(req.query, 'after_id', x => checkRegexParam(x, revisionIdRegex)); + const beforeId: string = processParam(req.query, 'before_id', x => checkRegexParam(x, revisionIdRegex)); + const count: number = processParam(req.query, 'count', parsePositiveIntParam); + + if(afterId != undefined && beforeId != undefined) { + throw new UserInputError('Cannot specify both after_id and before_id parameters'); + } + try { - const result = await editHistoryService.getGlobalEditHistory(); - res.send({ - history: result - }); + const result = await editHistoryService.getGlobalEditHistory(beforeId, afterId, count); + res.send(result); } catch(error) { console.error(error); res.send({ error: 'Database error' });