Exemplo n.º 1
0
        public async Task <ActionResult> Put(Guid id, [FromBody] UpdatePlantViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                UpdatePlantModel updatePlantModel = _mapper.Map <UpdatePlantModel>(model);
                updatePlantModel.Id = id;

                Plant plant = await _commands.Update(updatePlantModel);

                if (plant is null)
                {
                    return(NotFound());
                }

                return(Ok(_mapper.Map <PlantDetailsViewModel>(plant)));
            }
            catch (Exception ex) when(ex is ResourceNotFoundException)
            {
                return(NotFound(new ErrorViewModel(ex)));
            }
            catch (Exception ex) when(ex is ResourceStateException)
            {
                return(Conflict(new ErrorViewModel(ex)));
            }
        }
Exemplo n.º 2
0
        public async Task PutPlantReturnsConflictIfResourceStateExceptionOccured()
        {
            // Given
            A.CallTo(() => _fakeCommand.Update(A <UpdatePlantModel> .Ignored)).Throws(A.Fake <ResourceStateException>());

            // When
            HttpResponseMessage response = await Client.PutAsJsonAsync(EndPointFactory.UpdateEndpoint(), ViewModelFactory.CreateValidUpdateModel());

            // Then
            response.StatusCode.Should().Be(HttpStatusCode.Conflict);
            response.Content.Headers.ContentType.ToString().Should().Be("application/json; charset=utf-8");
            A.CallTo(() => _fakeCommand.Update(A <UpdatePlantModel> .Ignored)).MustHaveHappenedOnceExactly();
        }