159 lines
5.7 KiB
JavaScript
159 lines
5.7 KiB
JavaScript
'use strict';
|
|
|
|
// modules
|
|
const express = require('express');
|
|
|
|
// HTTP status codes by name
|
|
const codes = require('./http-codes');
|
|
|
|
const apiAuthenticationMiddleware = require('../middleware/auth-middleware');
|
|
const checkMT = require('../middleware/permission-check').checkMT;
|
|
|
|
const routerHandling = require('../middleware/router-handling');
|
|
const idValidator = require('../middleware/validators').idValidator;
|
|
|
|
const fs = require('fs');
|
|
const resourceLocation = require('../middleware/resource-location').resourceLocation().concat('/logs/');
|
|
|
|
// Mongoose Model using mongoDB
|
|
const CampaignModel = require('../models/campaign');
|
|
const WarModel = require('../models/war');
|
|
const PlayerModel = require('../models/player');
|
|
const LogKillModel = require('../models/logs/kill');
|
|
const LogVehicleKillModel = require('../models/logs/vehicle');
|
|
const LogRespawnModel = require('../models/logs/respawn');
|
|
const LogReviveModel = require('../models/logs/revive');
|
|
const LogTransportModel = require('../models/logs/transport');
|
|
const LogFlagModel = require('../models/logs/flag');
|
|
const LogBudgetModel = require('../models/logs/budget');
|
|
const LogPointsModel = require('../models/logs/points');
|
|
const LogPlayerCountModel = require('../models/logs/player-count');
|
|
|
|
// util
|
|
const genericGetById = require('./_generic').genericGetById;
|
|
const genericPatch = require('./_generic').genericPatch;
|
|
|
|
const campaigns = new express.Router();
|
|
|
|
// routes **********************
|
|
campaigns.route('/')
|
|
.get((req, res, next) => {
|
|
CampaignModel.find({}, {}, {
|
|
sort: {
|
|
timestamp: 'desc',
|
|
},
|
|
}, (err, items) => {
|
|
if (err) {
|
|
err.status = codes.servererror;
|
|
return next(err);
|
|
}
|
|
if (items && items.length > 0) {
|
|
res.locals.items = items;
|
|
} else {
|
|
res.locals.items = [];
|
|
}
|
|
res.locals.processed = true;
|
|
next();
|
|
});
|
|
})
|
|
|
|
.post(apiAuthenticationMiddleware, checkMT, (req, res, next) => {
|
|
const campaign = new CampaignModel(req.body);
|
|
// timestamp and default are set automatically by Mongoose Schema Validation
|
|
campaign.save((err) => {
|
|
if (err) {
|
|
err.status = codes.wrongrequest;
|
|
err.message += ' in fields: ' + Object.getOwnPropertyNames(err.errors);
|
|
return next(err);
|
|
}
|
|
res.status(codes.created);
|
|
res.locals.items = campaign;
|
|
next();
|
|
});
|
|
})
|
|
|
|
.all(
|
|
routerHandling.httpMethodNotAllowed
|
|
);
|
|
|
|
campaigns.route('/with/war/:id')
|
|
.get((req, res, next) => {
|
|
WarModel.findById(req.params.id, (err, item) => {
|
|
if (err) {
|
|
err.status = codes.servererror;
|
|
return next(err);
|
|
}
|
|
return genericGetById({params: {id: item.campaign}}, res, next, CampaignModel);
|
|
});
|
|
})
|
|
|
|
.all(
|
|
routerHandling.httpMethodNotAllowed
|
|
);
|
|
|
|
campaigns.route('/:id')
|
|
.get(idValidator, (req, res, next) => {
|
|
return genericGetById(req, res, next, CampaignModel);
|
|
})
|
|
|
|
.patch(apiAuthenticationMiddleware, checkMT, (req, res, next) => {
|
|
return genericPatch(req, res, next, CampaignModel);
|
|
})
|
|
|
|
.delete((req, res, next) => {
|
|
WarModel.find({campaign: req.params.id}, (err, wars) => {
|
|
wars.forEach((item) => {
|
|
// delete linked appearances
|
|
PlayerModel.find({warId: item._id}).remove().exec();
|
|
LogKillModel.find({war: item._id}).remove().exec();
|
|
LogVehicleKillModel.find({war: item._id}).remove().exec();
|
|
LogRespawnModel.find({war: item._id}).remove().exec();
|
|
LogReviveModel.find({war: item._id}).remove().exec();
|
|
LogFlagModel.find({war: item._id}).remove().exec();
|
|
LogBudgetModel.find({war: item._id}).remove().exec();
|
|
LogTransportModel.find({war: item._id}).remove().exec();
|
|
LogPointsModel.find({war: item._id}).remove().exec();
|
|
LogPlayerCountModel.find({war: item._id}).remove().exec();
|
|
|
|
// check if logfiles exist and delete from fs
|
|
const warDir = resourceLocation + item.id;
|
|
if (fs.existsSync(warDir)) {
|
|
const cleanLog = warDir + '/clean.log';
|
|
if (fs.existsSync(cleanLog)) {
|
|
fs.unlink(cleanLog, (err) => {
|
|
});
|
|
}
|
|
const sourceLog = warDir + '/war.log';
|
|
if (fs.existsSync(sourceLog)) {
|
|
fs.unlink(sourceLog, (err) => {
|
|
});
|
|
}
|
|
fs.rmdir(warDir, (err) => {
|
|
});
|
|
}
|
|
});
|
|
CampaignModel.findByIdAndRemove(req.params.id, (err, item) => {
|
|
if (err) {
|
|
err.status = codes.wrongrequest;
|
|
return next(err);
|
|
} else if (!item) {
|
|
err = new Error('item not found');
|
|
err.status = codes.notfound;
|
|
return next(err);
|
|
}
|
|
res.locals.processed = true;
|
|
next();
|
|
});
|
|
});
|
|
})
|
|
|
|
.all(
|
|
routerHandling.httpMethodNotAllowed
|
|
);
|
|
|
|
// this middleware function can be used, if you like or remove it
|
|
// it looks for object(s) in res.locals.items and if they exist, they are send to the client as json
|
|
campaigns.use(routerHandling.emptyResponse);
|
|
|
|
module.exports = campaigns;
|