コード例 #1
0
        public async Task <ActionResult> Put([FromRoute] Guid plantId, Guid id, [FromBody] UpdateAdministeredNutrientViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                UpdateAdministeredNutrientModel updateAdministeredNutrientModel = _mapper.Map <UpdateAdministeredNutrientModel>(model);
                updateAdministeredNutrientModel.Id = id;

                AdministeredNutrient administeredNutrient = await _commands.Update(plantId, updateAdministeredNutrientModel);

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

                return(Ok(_mapper.Map <UpdateAdministeredNutrientViewModel>(administeredNutrient)));
            }
            catch (Exception ex) when(ex is ResourceNotFoundException)
            {
                return(NotFound(new ErrorViewModel(ex)));
            }
            catch (Exception ex) when(ex is ResourceStateException)
            {
                return(Conflict(new ErrorViewModel(ex)));
            }
        }
コード例 #2
0
        public async Task PutAdministeredPlantNutrientReturnsNotFoundIfResourceNotFoundExceptionOccured()
        {
            // Given
            A.CallTo(() => _fakeCommand.Update(A <Guid> .Ignored, A <UpdateAdministeredNutrientModel> .Ignored))
            .Throws(A.Fake <ResourceNotFoundException>());

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

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