Exemplo n.º 1
0
        public async Task <Nutrient> Update(UpdateNutrientModel model)
        {
            Nutrient nutrient = await _database.Nutrients
                                .Include(x => x.AdministeredToPlants)
                                .FirstOrDefaultAsync(x => x.Id == model.Id);

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

            if (nutrient.AdministeredToPlants.Any())
            {
                throw new NutrientAdministeredToAPlantException();
            }

            IQueryable <Nutrient> existingNutrientsWithName = from existingNutrient in _database.Nutrients
                                                              where (existingNutrient.Name == model.Name) && (existingNutrient.Id != model.Id)
                                                              select existingNutrient;

            if (await existingNutrientsWithName.AnyAsync())
            {
                throw new NutrientWithNameAlreadyExistsException(model.Name);
            }

            _mapper.Map(model, nutrient);
            _database.Nutrients.Update(nutrient);
            await _database.SaveAsync();

            return(nutrient);
        }
Exemplo n.º 2
0
        public Task UpdateNutrientAsync(NutrientModel model)
        {
            var uri     = UrisProvider.UpdateNutrient;
            var content = new UpdateNutrientModel(model.NutrientId, model.Title);

            return(this.httpClient.PutAsJsonAsync(uri, content));
        }
Exemplo n.º 3
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)));
            }
        }
Exemplo n.º 4
0
        public Task Delete([FromBody] UpdateNutrientModel model)
        {
            this.TryValidateModel(model);

            var command = new UpdateNutrientTitle(model.NutrientId, model.Title);

            return(this.updateNutrientTitleHandler.HandleCommandAsync(command));
        }
Exemplo n.º 5
0
        public async Task UpdateNutrientReturnsNullIfNutrientDoesNotExist()
        {
            // Given
            UpdateNutrientModel model = ModelFactory.UpdateModel(Guid.NewGuid());

            // When
            Nutrient nutrient = await _commands.Update(model);

            // Then
            nutrient.Should().BeNull();
        }
Exemplo n.º 6
0
        public void UpdateNutrientThrowsExceptionOnNameConflict()
        {
            // Given
            Nutrient existingNutrient = ModelFactory.DomainModel();

            SeedDatabase(existingNutrient);
            Nutrient            updatedNutrient   = ModelFactory.DomainModel("Nutrient B");
            Guid                updatedNutrientId = SeedDatabase(updatedNutrient);
            UpdateNutrientModel model             = ModelFactory.UpdateModel(updatedNutrientId);

            // When
            Func <Task> updateNutrient = async() => await _commands.Update(model);

            // Then
            updateNutrient.Should().Throw <NutrientWithNameAlreadyExistsException>();
        }
Exemplo n.º 7
0
        public async Task UpdateNutrientReturnsNutrientOnSuccess()
        {
            // Given
            Nutrient            nutrient   = ModelFactory.DomainModel("Nutrient B", dosageUnits: "ml");
            Guid                nutrientId = SeedDatabase(nutrient);
            UpdateNutrientModel model      = ModelFactory.UpdateModel(nutrientId);

            model.DosageUnits = "g";

            // When
            nutrient = await _commands.Update(model);

            // Then
            nutrient.Should().NotBeNull();
            nutrient.Name.Should().Be("Nutrient A");
            nutrient.DosageUnits.Should().Be("g");
        }
Exemplo n.º 8
0
        public void UpdateNutrientThrowsExceptionIfItWasAdministeredToAnyPlant()
        {
            // Given
            Nutrient nutrient   = ModelFactory.DomainModel("Nutrient B");
            Guid     nutrientId = SeedDatabase(nutrient);
            Plant    plant      = Plants.ModelFactory.DomainModel();

            SeedDatabase(plant);
            AdministeredNutrient administeredNutrient = Plants.Nutrients.ModelFactory.DomainModel(nutrient, plant);

            SeedDatabase(administeredNutrient);
            UpdateNutrientModel model = ModelFactory.UpdateModel(nutrientId);

            // When
            Func <Task> updateNutrient = async() => await _commands.Update(model);

            // Then
            updateNutrient.Should().Throw <NutrientAdministeredToAPlantException>();
        }