示例#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)));
            }
        }
        public void UpdateAdministeredNutrientThrowsExceptionIfPlantDoesNotExist()
        {
            // Given
            UpdateAdministeredNutrientModel model = ModelFactory.UpdateModel(Guid.NewGuid());

            // When
            Func <Task> updatePlant = async() => await _commands.Update(Guid.NewGuid(), model);

            // Then
            updatePlant.Should().Throw <PlantNotFoundException>();
        }
        public async Task UpdateAdministeredNutrientReturnsNullIfAdministeredNutrientDoesNotExist()
        {
            // Given
            Plant plant   = Plants.ModelFactory.DomainModel();
            Guid  plantId = SeedDatabase(plant);
            UpdateAdministeredNutrientModel model = ModelFactory.UpdateModel();

            // When
            AdministeredNutrient administeredNutrient = await _commands.Update(plantId, model);

            // Then
            administeredNutrient.Should().BeNull();
        }
        public void UpdateAdministeredNutrientThrowsExceptionIfNutrientDoesNotExist()
        {
            // Given
            Plant plant   = Plants.ModelFactory.DomainModel();
            Guid  plantId = SeedDatabase(plant);
            AdministeredNutrient administeredNutrient = ModelFactory.DomainModel(plant: plant);
            Guid administeredNutrientId           = SeedDatabase(administeredNutrient);
            UpdateAdministeredNutrientModel model = ModelFactory.UpdateModel(administeredNutrientId, Guid.NewGuid());

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

            // Then
            updatePlant.Should().Throw <NutrientNotFoundException>();
        }
        public void UpdateAdministeredNutrientThrowsExceptionIfAdministrationDateIsEarlierThanPlantDate()
        {
            // Given
            Plant                           plant                  = Plants.ModelFactory.DomainModel();
            Guid                            plantId                = SeedDatabase(plant);
            Nutrient                        nutrient               = Tests.Nutrients.ModelFactory.DomainModel();
            Guid                            nutrientId             = SeedDatabase(nutrient);
            AdministeredNutrient            administeredNutrient   = ModelFactory.DomainModel(nutrient, plant);
            Guid                            administeredNutrientId = SeedDatabase(administeredNutrient);
            UpdateAdministeredNutrientModel model                  = ModelFactory.UpdateModel(administeredNutrientId, nutrientId, plant.Planted.AddDays(-1));

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

            // Then
            updatePlant.Should().Throw <NutrientAdministrationDateBeforePlantDateException>();
        }
        public async Task UpdateAdministeredNutrientReturnsAdministeredNutrientOnSuccess()
        {
            // Given
            Plant                plant                  = Plants.ModelFactory.DomainModel();
            Guid                 plantId                = SeedDatabase(plant);
            Nutrient             nutrient               = Tests.Nutrients.ModelFactory.DomainModel();
            Guid                 nutrientId             = SeedDatabase(nutrient);
            AdministeredNutrient administeredNutrient   = ModelFactory.DomainModel(plant: plant);
            Guid                 administeredNutrientId = SeedDatabase(administeredNutrient);

            UpdateAdministeredNutrientModel model = ModelFactory.UpdateModel(administeredNutrientId, nutrientId);

            // When
            administeredNutrient = await _commands.Update(plantId, model);

            // Then
            administeredNutrient.Should().NotBeNull();
            administeredNutrient.Id.Should().Be(administeredNutrientId);
            administeredNutrient.Plant.Id.Should().Be(plant.Id);
            administeredNutrient.Nutrient.Id.Should().Be(nutrient.Id);
        }
示例#7
0
        public async Task <AdministeredNutrient> Update(Guid plantId, UpdateAdministeredNutrientModel model)
        {
            Plant plant = await _database.Plants
                          .Include(x => x.AdministeredNutrients)
                          .FirstOrDefaultAsync(x => x.Id == plantId);

            if (plant is null)
            {
                throw new PlantNotFoundException(plantId);
            }

            AdministeredNutrient administeredNutrient =
                plant.AdministeredNutrients.FirstOrDefault(x => x.Id == model.Id);

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

            Nutrient nutrient = await _database.Nutrients.FindAsync(model.NutrientId);

            if (nutrient is null)
            {
                throw new NutrientNotFoundException(model.NutrientId);
            }

            if (model.Date < plant.Planted)
            {
                throw new NutrientAdministrationDateBeforePlantDateException();
            }

            administeredNutrient.Plant    = plant;
            administeredNutrient.Nutrient = nutrient;
            _database.AdministeredNutrients.Update(administeredNutrient);
            await _database.SaveAsync();

            return(administeredNutrient);
        }