From 6bc6d14b6d43b972e5969f3d4ba4aa6898a797ad Mon Sep 17 00:00:00 2001 From: HardiReady Date: Sun, 29 Apr 2018 11:12:09 +0200 Subject: [PATCH] Add edit war in API --- api/routes/wars.js | 31 +++++++++++++++++++ static/src/app/services/logs/war.service.ts | 2 +- .../war/war-edit/war-edit.component.ts | 2 +- .../war/war-list/war-item.component.html | 4 +++ .../war/war-list/war-item.component.ts | 6 ++++ .../war/war-list/war-list.component.html | 1 + .../war/war-list/war-list.component.ts | 19 ++++++++---- 7 files changed, 57 insertions(+), 8 deletions(-) diff --git a/api/routes/wars.js b/api/routes/wars.js index 15d3e44..eb42cf9 100644 --- a/api/routes/wars.js +++ b/api/routes/wars.js @@ -168,6 +168,37 @@ wars.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. + WarModel.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(apiAuthenticationMiddleware, checkMT, (req, res, next) => { WarModel.findByIdAndRemove(req.params.id, (err, item) => { if (err) { diff --git a/static/src/app/services/logs/war.service.ts b/static/src/app/services/logs/war.service.ts index 8072a41..b434a2e 100644 --- a/static/src/app/services/logs/war.service.ts +++ b/static/src/app/services/logs/war.service.ts @@ -39,7 +39,7 @@ export class WarService { } updateWar(war: War) { - return this.http.patch(this.config.apiWarPath, war) + return this.http.patch(this.config.apiWarPath + '/' + war._id, war) .map(res => res.json()); } } diff --git a/static/src/app/statistic/war/war-edit/war-edit.component.ts b/static/src/app/statistic/war/war-edit/war-edit.component.ts index a0004ad..254241b 100644 --- a/static/src/app/statistic/war/war-edit/war-edit.component.ts +++ b/static/src/app/statistic/war/war-edit/war-edit.component.ts @@ -43,7 +43,7 @@ export class WarEditComponent { updateWar() { this.warService.updateWar(this.war) .subscribe(war => { - this.router.navigate(['../war/' + war._id], {relativeTo: this.route}); + this.router.navigate(['../../war/' + war._id], {relativeTo: this.route}); }, error => { this.error = error._body.error.message; diff --git a/static/src/app/statistic/war/war-list/war-item.component.html b/static/src/app/statistic/war/war-list/war-item.component.html index 0545d74..1672969 100644 --- a/static/src/app/statistic/war/war-list/war-item.component.html +++ b/static/src/app/statistic/war/war-list/war-item.component.html @@ -13,6 +13,10 @@ + diff --git a/static/src/app/statistic/war/war-list/war-item.component.ts b/static/src/app/statistic/war/war-list/war-item.component.ts index 20c874b..dcc33d0 100644 --- a/static/src/app/statistic/war/war-list/war-item.component.ts +++ b/static/src/app/statistic/war/war-list/war-item.component.ts @@ -16,6 +16,8 @@ export class WarItemComponent implements OnInit { @Output() warSelected = new EventEmitter(); + @Output() warEdit = new EventEmitter(); + @Output() warDelete = new EventEmitter(); constructor(public loginService: LoginService) { @@ -28,6 +30,10 @@ export class WarItemComponent implements OnInit { this.warSelected.emit(this.war._id); } + edit() { + this.warEdit.emit(this.war._id); + } + delete() { this.warDelete.emit(this.war); } diff --git a/static/src/app/statistic/war/war-list/war-list.component.html b/static/src/app/statistic/war/war-list/war-list.component.html index c4c1d73..8d604ad 100644 --- a/static/src/app/statistic/war/war-list/war-list.component.html +++ b/static/src/app/statistic/war/war-list/war-list.component.html @@ -47,6 +47,7 @@
diff --git a/static/src/app/statistic/war/war-list/war-list.component.ts b/static/src/app/statistic/war/war-list/war-list.component.ts index e18d329..bb18ed0 100644 --- a/static/src/app/statistic/war/war-list/war-list.component.ts +++ b/static/src/app/statistic/war/war-list/war-list.component.ts @@ -49,11 +49,6 @@ export class WarListComponent implements OnInit { this.router.navigate([{outlets: {'right': ['campaign']}}], {relativeTo: this.route}); } - selectNewWar() { - this.selectedWarId = null; - this.router.navigate([{outlets: {'right': ['submit-war']}}], {relativeTo: this.route}); - } - selectWar(warId) { if (this.selectedWarId !== warId) { this.selectedWarId = warId; @@ -75,6 +70,16 @@ export class WarListComponent implements OnInit { } } + selectNewWar() { + this.selectedWarId = null; + this.router.navigate([{outlets: {'right': ['submit-war']}}], {relativeTo: this.route}); + } + + editWar(warId) { + this.selectedWarId = warId; + this.router.navigate([{outlets: {'right': ['submit-war', warId]}}], {relativeTo: this.route}); + } + deleteWar(war: War) { if (confirm('Soll die Schlacht ' + war.title + ' wirklich gelöscht werden?')) { this.warService.deleteWar(war._id) @@ -82,7 +87,9 @@ export class WarListComponent implements OnInit { if (this.selectedWarId === war._id) { this.selectOverview('all'); } - this.campaigns.splice(this.campaigns.indexOf(war), 1); + this.campaigns.forEach(campaign => { + campaign.wars.splice(campaign.wars.indexOf(war),1) + }) }); } }