public IActionResult PartiallyUpdateActor(Guid actorId, JsonPatchDocument <ActorForUpdateDto> patchDocument) { var actorFromDb = _unitOfWork.Actors.GetActor(actorId); if (actorFromDb == null) { var actorDto = new ActorForUpdateDto(); patchDocument.ApplyTo(actorDto, ModelState); if (!TryValidateModel(actorDto)) { return(ValidationProblem(ModelState)); } return(UpsertActor(actorId, actorDto)); } var actorToPatch = _mapper.Map <ActorForUpdateDto>(actorFromDb); patchDocument.ApplyTo(actorToPatch, ModelState); if (!TryValidateModel(actorToPatch)) { return(ValidationProblem(ModelState)); } _mapper.Map(actorToPatch, actorFromDb); SaveActorInDB(actorFromDb); return(NoContent()); }
public async Task <IActionResult> Update(Guid movieId, Guid id, [FromBody] ActorForUpdateDto actor) { var movieExist = await this.cinemaService.MovieExists(movieId); if (!movieExist) { return(NotFound()); } var actorInMovieFromRepo = await this.cinemaService.GetActorInMovie(movieId, id); if (actorInMovieFromRepo == null) { //NO upserting return(NotFound()); } Mapper.Map(actor, actorInMovieFromRepo); this.cinemaService.UpdateActorInMovie(actorInMovieFromRepo); var saveResult = await this.cinemaService.Save(); if (!saveResult) { throw new Exception($"Updating book {id} for author {movieId} failed on save."); } return(NoContent()); }
public IActionResult UpdateActor(Guid actorId, ActorForUpdateDto actorToBeUpdated) { var actorFromDb = _unitOfWork.Actors.GetActor(actorId); if (actorFromDb == null) { return(UpsertActor(actorId, actorToBeUpdated)); } _mapper.Map(actorToBeUpdated, actorFromDb); SaveActorInDB(actorFromDb); return(NoContent()); }
private IActionResult UpsertActor(Guid actorId, ActorForUpdateDto actorToBeUpdated) { var actorToAdd = _mapper.Map <Actor>(actorToBeUpdated); if (_unitOfWork.Actors.ActorExists(actorToAdd)) { return(Conflict(new { message = $"This Actor already exists in the database!" })); } actorToAdd.Guid = actorId; SaveActorInDB(actorToAdd); var actorToReturn = _mapper.Map <ActorDto>(actorToAdd); return(CreatedAtRoute("GetActor", new { actorId = actorToReturn.Id }, actorToReturn)); }