simplify log router

pull/15/head
HardiReady 2017-10-22 17:10:13 +02:00
parent 75019a619f
commit a8960506c6
1 changed files with 20 additions and 71 deletions

View File

@ -18,26 +18,29 @@ const LogTransportModel = require('../models/logs/transport');
const LogFlagModel = require('../models/logs/flag');
const LogPointsModel = require('../models/logs/points');
const notFoundError = new Error('No logs found');
const logsRouter = express.Router();
function processLogRequest(model, filter, res, next) {
model.find(filter, {}, {sort: {time: 1}}, (err, log) => {
if (err) return next(err);
if (!log || log.length === 0) {
const err = notFoundError;
err.status = codes.notfound;
return next(err)
}
res.locals.items = log;
next();
})
}
// routes **********************
logsRouter.route('/:warId/budget')
.get((req, res, next) => {
const filter = {war: req.params.warId};
if (req.query.fraction) filter['fraction'] = req.query.fraction;
LogBudgetModel.find(filter, {}, {sort: {time: 1}}, (err, logBudget) => {
if (err) return next(err);
if (!logBudget || logBudget.length === 0) {
const err = notFoundError;
err.status = codes.notfound;
return next(err)
}
res.locals.items = logBudget;
next();
})
processLogRequest(LogBudgetModel, filter, res, next);
})
.all(
routerHandling.httpMethodNotAllowed
@ -47,16 +50,7 @@ logsRouter.route('/:warId/respawn')
.get((req, res, next) => {
const filter = {war: req.params.warId};
if (req.query.player) filter['player'] = req.query.player;
LogRespawnModel.find(filter, {}, {sort: {time: 1}}, (err, logRespawn) => {
if (err) return next(err);
if (!logRespawn || logRespawn.length === 0) {
const err = notFoundError;
err.status = codes.notfound;
return next(err)
}
res.locals.items = logRespawn;
next();
})
processLogRequest(LogRespawnModel, filter, res, next);
})
.all(
routerHandling.httpMethodNotAllowed
@ -70,16 +64,7 @@ logsRouter.route('/:warId/revive')
if (req.query.stabilized) filter['stabilized'] = true;
if (req.query.revive) filter['stabilized'] = false;
if (req.query.fraction) filter['fraction'] = req.query.fraction;
LogReviveModel.find(filter, {}, {sort: {time: 1}}, (err, logRevive) => {
if (err) return next(err);
if (!logRevive || logRevive.length === 0) {
const err = notFoundError;
err.status = codes.notfound;
return next(err)
}
res.locals.items = logRevive;
next();
})
processLogRequest(LogReviveModel, filter, res, next);
})
.all(
routerHandling.httpMethodNotAllowed
@ -93,16 +78,7 @@ logsRouter.route('/:warId/kills')
if (req.query.friendlyFire) filter['friendlyFire'] = true;
if (req.query.noFriendlyFire) filter['friendlyFire'] = false;
if (req.query.fraction) filter['fraction'] = req.query.fraction;
LogKillModel.find(filter, {}, {sort: {time: 1}}, (err, logKill) => {
if (err) return next(err);
if (!logKill || logKill.length === 0) {
const err = notFoundError;
err.status = codes.notfound;
return next(err)
}
res.locals.items = logKill;
next();
})
processLogRequest(LogKillModel, filter, res, next);
})
.all(
routerHandling.httpMethodNotAllowed
@ -114,16 +90,7 @@ logsRouter.route('/:warId/transport')
if (req.query.driver) filter['driver'] = req.query.driver;
if (req.query.passenger) filter['passenger'] = req.query.passenger;
if (req.query.fraction) filter['fraction'] = req.query.fraction;
LogTransportModel.find(filter, {}, {sort: {time: 1}}, (err, logTransport) => {
if (err) return next(err);
if (!logTransport || logTransport.length === 0) {
const err = notFoundError;
err.status = codes.notfound;
return next(err)
}
res.locals.items = logTransport;
next();
})
processLogRequest(LogTransportModel, filter, res, next);
})
.all(
routerHandling.httpMethodNotAllowed
@ -136,16 +103,7 @@ logsRouter.route('/:warId/flag')
if (req.query.capture) filter['capture'] = true;
if (req.query.defend) filter['capture'] = false;
if (req.query.fraction) filter['fraction'] = req.query.fraction;
LogFlagModel.find(filter, {}, {sort: {time: 1}}, (err, logFlag) => {
if (err) return next(err);
if (!logFlag || logFlag.length === 0) {
const err = notFoundError;
err.status = codes.notfound;
return next(err)
}
res.locals.items = logFlag;
next();
})
processLogRequest(LogFlagModel, filter, res, next);
})
.all(
routerHandling.httpMethodNotAllowed
@ -155,16 +113,7 @@ logsRouter.route('/:warId/points')
.get((req, res, next) => {
const filter = {war: req.params.warId};
if (req.query.fraction) filter['fraction'] = req.query.fraction;
LogPointsModel.find(filter, {}, {sort: {time: 1}}, (err, logPoints) => {
if (err) return next(err);
if (!logPoints || logPoints.length === 0) {
const err = notFoundError;
err.status = codes.notfound;
return next(err)
}
res.locals.items = logPoints;
next();
})
processLogRequest(LogPointsModel, filter, res, next);
})
.all(
routerHandling.httpMethodNotAllowed