simplify log router
parent
75019a619f
commit
a8960506c6
|
@ -18,26 +18,29 @@ const LogTransportModel = require('../models/logs/transport');
|
||||||
const LogFlagModel = require('../models/logs/flag');
|
const LogFlagModel = require('../models/logs/flag');
|
||||||
const LogPointsModel = require('../models/logs/points');
|
const LogPointsModel = require('../models/logs/points');
|
||||||
|
|
||||||
|
|
||||||
const notFoundError = new Error('No logs found');
|
const notFoundError = new Error('No logs found');
|
||||||
|
|
||||||
const logsRouter = express.Router();
|
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 **********************
|
// routes **********************
|
||||||
logsRouter.route('/:warId/budget')
|
logsRouter.route('/:warId/budget')
|
||||||
.get((req, res, next) => {
|
.get((req, res, next) => {
|
||||||
const filter = {war: req.params.warId};
|
const filter = {war: req.params.warId};
|
||||||
if (req.query.fraction) filter['fraction'] = req.query.fraction;
|
if (req.query.fraction) filter['fraction'] = req.query.fraction;
|
||||||
LogBudgetModel.find(filter, {}, {sort: {time: 1}}, (err, logBudget) => {
|
processLogRequest(LogBudgetModel, filter, res, next);
|
||||||
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();
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
.all(
|
.all(
|
||||||
routerHandling.httpMethodNotAllowed
|
routerHandling.httpMethodNotAllowed
|
||||||
|
@ -47,16 +50,7 @@ logsRouter.route('/:warId/respawn')
|
||||||
.get((req, res, next) => {
|
.get((req, res, next) => {
|
||||||
const filter = {war: req.params.warId};
|
const filter = {war: req.params.warId};
|
||||||
if (req.query.player) filter['player'] = req.query.player;
|
if (req.query.player) filter['player'] = req.query.player;
|
||||||
LogRespawnModel.find(filter, {}, {sort: {time: 1}}, (err, logRespawn) => {
|
processLogRequest(LogRespawnModel, filter, res, next);
|
||||||
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();
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
.all(
|
.all(
|
||||||
routerHandling.httpMethodNotAllowed
|
routerHandling.httpMethodNotAllowed
|
||||||
|
@ -70,16 +64,7 @@ logsRouter.route('/:warId/revive')
|
||||||
if (req.query.stabilized) filter['stabilized'] = true;
|
if (req.query.stabilized) filter['stabilized'] = true;
|
||||||
if (req.query.revive) filter['stabilized'] = false;
|
if (req.query.revive) filter['stabilized'] = false;
|
||||||
if (req.query.fraction) filter['fraction'] = req.query.fraction;
|
if (req.query.fraction) filter['fraction'] = req.query.fraction;
|
||||||
LogReviveModel.find(filter, {}, {sort: {time: 1}}, (err, logRevive) => {
|
processLogRequest(LogReviveModel, filter, res, next);
|
||||||
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();
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
.all(
|
.all(
|
||||||
routerHandling.httpMethodNotAllowed
|
routerHandling.httpMethodNotAllowed
|
||||||
|
@ -93,16 +78,7 @@ logsRouter.route('/:warId/kills')
|
||||||
if (req.query.friendlyFire) filter['friendlyFire'] = true;
|
if (req.query.friendlyFire) filter['friendlyFire'] = true;
|
||||||
if (req.query.noFriendlyFire) filter['friendlyFire'] = false;
|
if (req.query.noFriendlyFire) filter['friendlyFire'] = false;
|
||||||
if (req.query.fraction) filter['fraction'] = req.query.fraction;
|
if (req.query.fraction) filter['fraction'] = req.query.fraction;
|
||||||
LogKillModel.find(filter, {}, {sort: {time: 1}}, (err, logKill) => {
|
processLogRequest(LogKillModel, filter, res, next);
|
||||||
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();
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
.all(
|
.all(
|
||||||
routerHandling.httpMethodNotAllowed
|
routerHandling.httpMethodNotAllowed
|
||||||
|
@ -114,16 +90,7 @@ logsRouter.route('/:warId/transport')
|
||||||
if (req.query.driver) filter['driver'] = req.query.driver;
|
if (req.query.driver) filter['driver'] = req.query.driver;
|
||||||
if (req.query.passenger) filter['passenger'] = req.query.passenger;
|
if (req.query.passenger) filter['passenger'] = req.query.passenger;
|
||||||
if (req.query.fraction) filter['fraction'] = req.query.fraction;
|
if (req.query.fraction) filter['fraction'] = req.query.fraction;
|
||||||
LogTransportModel.find(filter, {}, {sort: {time: 1}}, (err, logTransport) => {
|
processLogRequest(LogTransportModel, filter, res, next);
|
||||||
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();
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
.all(
|
.all(
|
||||||
routerHandling.httpMethodNotAllowed
|
routerHandling.httpMethodNotAllowed
|
||||||
|
@ -136,16 +103,7 @@ logsRouter.route('/:warId/flag')
|
||||||
if (req.query.capture) filter['capture'] = true;
|
if (req.query.capture) filter['capture'] = true;
|
||||||
if (req.query.defend) filter['capture'] = false;
|
if (req.query.defend) filter['capture'] = false;
|
||||||
if (req.query.fraction) filter['fraction'] = req.query.fraction;
|
if (req.query.fraction) filter['fraction'] = req.query.fraction;
|
||||||
LogFlagModel.find(filter, {}, {sort: {time: 1}}, (err, logFlag) => {
|
processLogRequest(LogFlagModel, filter, res, next);
|
||||||
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();
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
.all(
|
.all(
|
||||||
routerHandling.httpMethodNotAllowed
|
routerHandling.httpMethodNotAllowed
|
||||||
|
@ -155,16 +113,7 @@ logsRouter.route('/:warId/points')
|
||||||
.get((req, res, next) => {
|
.get((req, res, next) => {
|
||||||
const filter = {war: req.params.warId};
|
const filter = {war: req.params.warId};
|
||||||
if (req.query.fraction) filter['fraction'] = req.query.fraction;
|
if (req.query.fraction) filter['fraction'] = req.query.fraction;
|
||||||
LogPointsModel.find(filter, {}, {sort: {time: 1}}, (err, logPoints) => {
|
processLogRequest(LogPointsModel, filter, res, next);
|
||||||
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();
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
.all(
|
.all(
|
||||||
routerHandling.httpMethodNotAllowed
|
routerHandling.httpMethodNotAllowed
|
||||||
|
|
Loading…
Reference in New Issue