Add edit war in API

pull/34/head
HardiReady 2018-04-29 11:12:09 +02:00
parent 4b617ec67a
commit 6bc6d14b6d
7 changed files with 57 additions and 8 deletions

View File

@ -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) {

View File

@ -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());
}
}

View File

@ -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;

View File

@ -13,6 +13,10 @@
<span (click)="delete(); $event.stopPropagation()"
title="Löschen"
class="glyphicon glyphicon-trash trash"></span>
<span (click)="edit(); $event.stopPropagation()"
style="padding-right: 10px;"
title="Bearbeiten"
class="glyphicon glyphicon-edit trash"></span>
</div>
</div>

View File

@ -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);
}

View File

@ -47,6 +47,7 @@
<div *ngFor="let war of campaign.wars">
<pjm-war-item
[war]="war"
(warEdit)="editWar($event)"
(warDelete)="deleteWar(war)"
(warSelected)="selectWar($event)"
[selected]="war._id == selectedWarId">

View File

@ -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)
})
});
}
}