Beispiel #1
0
        public async Task PostNewNutrientReturnsConflictIfResourceStateExceptionOccured()
        {
            // Given
            A.CallTo(() => _fakeCommands.Create(A <CreateNutrientModel> .Ignored))
            .Throws(A.Fake <ResourceStateException>());

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

            // Then
            response.StatusCode.Should().Be(HttpStatusCode.Conflict);
            response.Content.Headers.ContentType.ToString().Should().Be("application/json; charset=utf-8");
            A.CallTo(() => _fakeCommands.Create(A <CreateNutrientModel> .Ignored)).MustHaveHappenedOnceExactly();
        }
Beispiel #2
0
        public async Task <ActionResult> Post([FromBody] CreateNutrientViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                CreateNutrientModel createNutrientModel = _mapper.Map <CreateNutrientModel>(model);
                Guid nutrientId = await _commands.Create(createNutrientModel);

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