2018-03-12 09:26:44 +01:00
|
|
|
'use strict';
|
2017-08-12 16:28:18 +02:00
|
|
|
|
|
|
|
// modules
|
|
|
|
const express = require('express');
|
|
|
|
|
|
|
|
// HTTP status codes by name
|
|
|
|
const codes = require('./http-codes');
|
|
|
|
|
2017-08-12 23:13:39 +02:00
|
|
|
const apiAuthenticationMiddleware = require('../middleware/auth-middleware');
|
|
|
|
const checkMT = require('../middleware/permission-check').checkMT;
|
2017-08-12 16:28:18 +02:00
|
|
|
|
2018-02-04 16:36:42 +01:00
|
|
|
const routerHandling = require('../middleware/router-handling');
|
|
|
|
const idValidator = require('../middleware/validators').idValidator;
|
|
|
|
|
2017-08-12 16:28:18 +02:00
|
|
|
// Mongoose Model using mongoDB
|
|
|
|
const CampaignModel = require('../models/campaign');
|
2017-09-14 12:01:16 +02:00
|
|
|
const WarModel = require('../models/war');
|
|
|
|
|
2018-03-12 10:39:56 +01:00
|
|
|
const campaigns = new express.Router();
|
2017-08-12 16:28:18 +02:00
|
|
|
|
|
|
|
// routes **********************
|
|
|
|
campaigns.route('/')
|
2018-02-26 09:04:27 +01:00
|
|
|
.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
|
|
|
|
);
|
2017-08-12 16:28:18 +02:00
|
|
|
|
2017-08-12 23:13:39 +02:00
|
|
|
campaigns.route('/:id')
|
2018-02-26 09:04:27 +01:00
|
|
|
.get(idValidator, (req, res, next) => {
|
|
|
|
CampaignModel.findById(req.params.id, (err, item) => {
|
|
|
|
if (err) {
|
|
|
|
err.status = codes.servererror;
|
|
|
|
return next(err);
|
2018-03-12 09:26:44 +01:00
|
|
|
} else if (!item) {
|
|
|
|
err = new Error('item not found');
|
2018-02-26 09:04:27 +01:00
|
|
|
err.status = codes.notfound;
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
res.locals.items = item;
|
|
|
|
return next();
|
|
|
|
});
|
|
|
|
})
|
|
|
|
|
2018-04-28 11:55:58 +02:00
|
|
|
.patch(apiAuthenticationMiddleware, checkMT, (req, res, next) => {
|
|
|
|
if (!req.body || (req.body._id && req.body._id !== req.params.id)) {
|
|
|
|
// little bit different as in PUT. :id does not need to be in data, but if the _id and url id must match
|
|
|
|
const err = new Error('id of PATCH resource and send JSON body are not equal ' + req.params.id + ' ' +
|
|
|
|
req.body._id);
|
|
|
|
err.status = codes.notfound;
|
|
|
|
next(err);
|
|
|
|
return; // prevent node to process this function further after next() has finished.
|
|
|
|
}
|
|
|
|
|
|
|
|
req.body.updatedAt = new Date();
|
2018-04-29 09:51:28 +02:00
|
|
|
req.body.$inc = {__v: 1};
|
2018-04-28 11:55:58 +02:00
|
|
|
if (req.body.hasOwnProperty('__v')) {
|
|
|
|
delete req.body.__v;
|
|
|
|
}
|
|
|
|
|
|
|
|
// PATCH is easier with mongoose than PUT. You simply update by all data that comes from outside. no need to
|
|
|
|
// reset attributes that are missing.
|
2018-04-29 09:51:28 +02:00
|
|
|
CampaignModel.findByIdAndUpdate(req.params.id, req.body, {new: true}, (err, item) => {
|
2018-04-28 11:55:58 +02:00
|
|
|
if (err) {
|
|
|
|
err.status = codes.wrongrequest;
|
|
|
|
} else if (!item) {
|
|
|
|
err = new Error('item not found');
|
|
|
|
err.status = codes.notfound;
|
|
|
|
} else {
|
|
|
|
res.locals.items = item;
|
|
|
|
}
|
|
|
|
next(err);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
|
2018-02-26 09:04:27 +01:00
|
|
|
.delete((req, res, next) => {
|
|
|
|
CampaignModel.findByIdAndRemove(req.params.id, (err, item) => {
|
|
|
|
if (err) {
|
|
|
|
err.status = codes.wrongrequest;
|
|
|
|
return next(err);
|
2018-03-12 09:26:44 +01:00
|
|
|
} else if (!item) {
|
|
|
|
err = new Error('item not found');
|
2018-02-26 09:04:27 +01:00
|
|
|
err.status = codes.notfound;
|
|
|
|
return next(err);
|
|
|
|
}
|
2018-04-29 09:51:28 +02:00
|
|
|
WarModel.find({campaign: req.params.id}).remove().exec();
|
2018-03-30 20:28:31 +02:00
|
|
|
// TODO: remove all the war logs from fs here!!!
|
2018-02-26 09:04:27 +01:00
|
|
|
res.locals.processed = true;
|
|
|
|
next();
|
2018-03-12 09:26:44 +01:00
|
|
|
});
|
2018-02-26 09:04:27 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
.all(
|
|
|
|
routerHandling.httpMethodNotAllowed
|
|
|
|
);
|
2017-08-12 23:13:39 +02:00
|
|
|
|
2017-08-12 16:28:18 +02:00
|
|
|
// 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;
|