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

            try
            {
                UpdateNutrientModel updateNutrientModel = _mapper.Map <UpdateNutrientModel>(model);
                updateNutrientModel.Id = id;

                Nutrient nutrient = await _commands.Update(updateNutrientModel);

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

                return(Ok(_mapper.Map <NutrientDetailsViewModel>(nutrient)));
            }
            catch (Exception ex) when(ex is ResourceStateException)
            {
                return(Conflict(new ErrorViewModel(ex)));
            }
        }
Ejemplo n.º 2
0
        public async Task PutNutrientReturnsConflictIfResourceStateExceptionOccured()
        {
            // Given
            A.CallTo(() => _fakeCommands.Update(A <UpdateNutrientModel> .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(() => _fakeCommands.Update(A <UpdateNutrientModel> .Ignored)).MustHaveHappenedOnceExactly();
        }