Exemple #1
0
        public async Task PostNewAdministeredPlantNutrientReturnsNotFoundIfResourceNotFoundExceptionOccured()
        {
            // Given
            A.CallTo(() => _fakeCommand.Create(A <Guid> .Ignored, A <CreateAdministeredNutrientModel> .Ignored))
            .Throws(A.Fake <ResourceNotFoundException>());

            // When
            HttpResponseMessage response = await Client.PostAsJsonAsync(EndPointFactory.CreateEndpoint(), ViewModelFactory.CreateValidCreationModel());

            // Then
            response.StatusCode.Should().Be(HttpStatusCode.NotFound);
            response.Content.Headers.ContentType.ToString().Should().Be("application/json; charset=utf-8");
            A.CallTo(() => _fakeCommand.Create(A <Guid> .Ignored, A <CreateAdministeredNutrientModel> .Ignored)).MustHaveHappenedOnceExactly();
        }
        public async Task <ActionResult> Post([FromRoute] Guid plantId, [FromBody] CreateAdministeredNutrientViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                CreateAdministeredNutrientModel createAdministeredNutrientModel = _mapper.Map <CreateAdministeredNutrientModel>(model);
                Guid nutrientId = await _commands.Create(plantId, createAdministeredNutrientModel);

                return(CreatedAtRoute(nameof(GetAdministeredNutrient), new { plantId = plantId, id = nutrientId }, null));
            }
            catch (Exception ex) when(ex is ResourceNotFoundException)
            {
                return(NotFound(new ErrorViewModel(ex)));
            }
            catch (Exception ex) when(ex is ResourceStateException)
            {
                return(Conflict(new ErrorViewModel(ex)));
            }
        }