add functional endpoint for update campaign and unhappy frontend processing

pull/34/head
Florian Hartwich 2018-04-28 11:55:58 +02:00
parent 3754974bfb
commit b40c664bce
5 changed files with 63 additions and 9 deletions

View File

@ -1,2 +1,3 @@
{"_id":{"$oid":"5abd55ea9e30a76bfef747d6"},"title":"Ein Kessel Buntes","timestamp":{"$date":"2018-03-29T21:08:58.123Z"},"updatedAt":{"$date":"2018-03-29T21:08:58.123Z"},"__v":0} {"_id":{"$oid":"5abd55ea9e30a76bfef747d6"},"title":"Ein Kessel Buntes","timestamp":{"$date":"2018-03-29T21:08:58.123Z"},"updatedAt":{"$date":"2018-03-29T21:08:58.123Z"},"__v":0}
{"_id":{"$oid":"5abd58989e30a76bfef747e6"},"title":"This Is The End","timestamp":{"$date":"2018-03-29T21:20:24.558Z"},"updatedAt":{"$date":"2018-03-29T21:20:24.558Z"},"__v":0} {"_id":{"$oid":"5abd58989e30a76bfef747e6"},"title":"This Is The End","timestamp":{"$date":"2018-03-29T21:20:24.558Z"},"updatedAt":{"$date":"2018-03-29T21:20:24.558Z"},"__v":0}
{"_id":{"$oid":"5abd58es9e30a76bfef347e4"},"title":"Edit me","timestamp":{"$date":"2018-03-29T21:20:24.558Z"},"updatedAt":{"$date":"2018-03-29T21:20:24.558Z"},"__v":0}

View File

@ -24,6 +24,25 @@ Create a new campaign
+ Attributes (Campaign, fixed-type) + Attributes (Campaign, fixed-type)
### Update Campaign [PUT /campaigns/{id}]
Update a campaign, identified by its id
**Permission: 3**
+ Parameters
+ id: `5abd58es9e30a76bfef347e4` (string, required) - unique id of campaign
+ Request (application/json)
+ Attributes
+ _id: `5abd58es9e30a76bfef347e4` (string, required) - unique id of campaign
+ title: `Operation Pandora` (string, optional) - display name of the campaign
+ Response 200 (application/json; charset=utf-8)
+ Attributes (Campaign, fixed-type)
### Delete Campaign [DELETE /campaigns/{id}] ### Delete Campaign [DELETE /campaigns/{id}]
Delete a campaign Delete a campaign

View File

@ -16,12 +16,10 @@ const idValidator = require('../middleware/validators').idValidator;
const CampaignModel = require('../models/campaign'); const CampaignModel = require('../models/campaign');
const WarModel = require('../models/war'); const WarModel = require('../models/war');
const campaigns = new express.Router(); const campaigns = new express.Router();
// routes ********************** // routes **********************
campaigns.route('/') campaigns.route('/')
.post(apiAuthenticationMiddleware, checkMT, (req, res, next) => { .post(apiAuthenticationMiddleware, checkMT, (req, res, next) => {
const campaign = new CampaignModel(req.body); const campaign = new CampaignModel(req.body);
// timestamp and default are set automatically by Mongoose Schema Validation // timestamp and default are set automatically by Mongoose Schema Validation
@ -57,6 +55,37 @@ campaigns.route('/:id')
}); });
}) })
.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();
req.body.$inc = { __v: 1 };
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.
CampaignModel.findByIdAndUpdate(req.params.id, req.body, { new: true }, (err, item) => {
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);
});
})
.delete((req, res, next) => { .delete((req, res, next) => {
CampaignModel.findByIdAndRemove(req.params.id, (err, item) => { CampaignModel.findByIdAndRemove(req.params.id, (err, item) => {
if (err) { if (err) {
@ -67,7 +96,7 @@ campaigns.route('/:id')
err.status = codes.notfound; err.status = codes.notfound;
return next(err); return next(err);
} }
WarModel.find({campaign: req.params.id}).remove().exec(); WarModel.find({ campaign: req.params.id }).remove().exec();
// TODO: remove all the war logs from fs here!!! // TODO: remove all the war logs from fs here!!!
res.locals.processed = true; res.locals.processed = true;
next(); next();

View File

@ -19,7 +19,8 @@ export class CampaignService {
submitCampaign(campaign: Campaign) { submitCampaign(campaign: Campaign) {
if (campaign._id) { if (campaign._id) {
return this.http.patch(this.config.apiCampaignPath + '/' + campaign._id, campaign); return this.http.patch(this.config.apiCampaignPath + '/' + campaign._id, campaign)
.map(res => res.json());
} else { } else {
return this.http.post(this.config.apiCampaignPath, campaign) return this.http.post(this.config.apiCampaignPath, campaign)
.map(res => res.json()); .map(res => res.json());

View File

@ -39,7 +39,11 @@ export class CampaignSubmitComponent {
saveCampaign() { saveCampaign() {
this.campaignService.submitCampaign(this.campaign) this.campaignService.submitCampaign(this.campaign)
.subscribe(campaign => { .subscribe(campaign => {
this.router.navigate(['../overview/' + campaign._id], {relativeTo: this.route}); let redirectSuccessUrl = '../overview/';
if (this.campaign._id) {
redirectSuccessUrl = '../' + redirectSuccessUrl
}
this.router.navigate([redirectSuccessUrl + campaign._id], {relativeTo: this.route});
}, },
error => { error => {
this.error = error._body.error.message; this.error = error._body.error.message;