Add param parsing to edit history controller
This commit is contained in:
parent
753c84acb7
commit
72cc7e62d2
@ -1,14 +1,24 @@
|
|||||||
import express from 'express';
|
import express from 'express';
|
||||||
|
|
||||||
|
import { UserInputError } from '../errors';
|
||||||
|
import { checkRegexParam, parsePositiveIntParam, processParam } from '../parameters';
|
||||||
import asyncController from "../routes/asyncController";
|
import asyncController from "../routes/asyncController";
|
||||||
import * as editHistoryService from '../services/editHistory';
|
import * as editHistoryService from '../services/editHistory';
|
||||||
|
|
||||||
const getGlobalEditHistory = asyncController(async (req: express.Request, res: express.Response) => {
|
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 {
|
try {
|
||||||
const result = await editHistoryService.getGlobalEditHistory();
|
const result = await editHistoryService.getGlobalEditHistory(beforeId, afterId, count);
|
||||||
res.send({
|
res.send(result);
|
||||||
history: result
|
|
||||||
});
|
|
||||||
} catch(error) {
|
} catch(error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
res.send({ error: 'Database error' });
|
res.send({ error: 'Database error' });
|
||||||
|
Loading…
Reference in New Issue
Block a user