コード例 #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)));
            }
        }
コード例 #2
0
        public async Task <Guid> Create(Guid plantId, CreateAdministeredNutrientModel model)
        {
            Plant plant = await _database.Plants.FindAsync(plantId);

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

            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 administeredNutrient = plant.AdministerNutrient(nutrient, model.Amount, model.Date, model.CreateEvent);

            _database.Plants.Update(plant);
            await _database.SaveAsync();

            return(administeredNutrient.Id);
        }
コード例 #3
0
ファイル: QueryPlantsTests.cs プロジェクト: JachuPL/Spice
        private Guid SeedDatabaseForGetByIdTesting()
        {
            using (SpiceContext ctx = SetupInMemoryDatabase())
            {
                Field field = Fields.ModelFactory.DomainModel();
                ctx.Fields.Add(field);

                Domain.Species species = Species.ModelFactory.DomainModel();
                ctx.Species.Add(species);

                Plant plant = ModelFactory.DomainModel(field, species);

                Nutrient nutrient = Tests.Nutrients.ModelFactory.DomainModel();
                ctx.Nutrients.Add(nutrient);

                AdministeredNutrient administeredNutrient = Nutrients.ModelFactory.DomainModel(nutrient, plant);
                plant.AdministeredNutrients.Add(administeredNutrient);
                ctx.AdministeredNutrients.Add(administeredNutrient);

                Event @event = Events.ModelFactory.DomainModel(plant);
                plant.Events.Add(@event);
                ctx.Events.Add(@event);
                ctx.Plants.Add(plant);
                ctx.Save();
                return(plant.Id);
            }
        }
コード例 #4
0
        protected Guid SeedDatabase(AdministeredNutrient administeredNutrient)
        {
            DatabaseContext.AdministeredNutrients.Add(administeredNutrient);
            DatabaseContext.Save();

            return(administeredNutrient.Id);
        }
コード例 #5
0
        public async Task <ActionResult <AdministeredNutrientDetailsViewModel> > GetAdministeredNutrient([FromRoute] Guid plantId, Guid id)
        {
            AdministeredNutrient administeredNutrient = await _queries.Get(plantId, id);

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

            return(Ok(_mapper.Map <AdministeredNutrientDetailsViewModel>(administeredNutrient)));
        }
コード例 #6
0
        public async Task GetByPlantReturnsNullWhenNotFound()
        {
            // Given
            Plant plant = SeedDatabaseForGetByPlantTesting();

            // When
            AdministeredNutrient administeredNutrient = await _queries.Get(plant.Id, Guid.NewGuid());

            // Then
            administeredNutrient.Should().BeNull();
        }
コード例 #7
0
        public async Task GetByPlantReturnsAdministeredNutrients()
        {
            // Given
            Plant plant = SeedDatabaseForGetByPlantTesting();
            AdministeredNutrient administeredNutrient = plant.AdministeredNutrients.First();

            // When
            AdministeredNutrient administeredNutrientsFromDatabase = await _queries.Get(plant.Id, administeredNutrient.Id);

            // Then
            administeredNutrientsFromDatabase.Should().NotBeNull();
        }
コード例 #8
0
        public async Task GetByPlantReturnsNullIfPlantDoesNotExist()
        {
            // Given
            Guid plantId = Guid.NewGuid();
            Guid id      = Guid.NewGuid();

            // When
            AdministeredNutrient administeredNutrient = await _queries.Get(plantId, id);

            // Then
            administeredNutrient.Should().BeNull();
        }
コード例 #9
0
 private Plant SeedDatabaseForGetByPlantTesting()
 {
     using (SpiceContext ctx = SetupInMemoryDatabase())
     {
         Plant plant = Plants.ModelFactory.DomainModel();
         AdministeredNutrient administeredNutrient = ModelFactory.DomainModel();
         plant.AdministeredNutrients.Add(administeredNutrient);
         ctx.Plants.Add(plant);
         ctx.Save();
         return(plant);
     }
 }
コード例 #10
0
        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();
        }
コード例 #11
0
        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>();
        }
コード例 #12
0
        public async Task DeleteAdministeredNutrientSucceeds()
        {
            // Given
            Plant plant   = Plants.ModelFactory.DomainModel();
            Guid  plantId = SeedDatabase(plant);
            AdministeredNutrient administeredNutrient = ModelFactory.DomainModel(plant: plant);
            Guid id = SeedDatabase(administeredNutrient);

            // When
            await _commands.Delete(plantId, id);

            // Then
            administeredNutrient = await DatabaseContext.AdministeredNutrients.FindAsync(id);

            administeredNutrient.Should().BeNull();
        }
コード例 #13
0
        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>();
        }
コード例 #14
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>();
        }
コード例 #15
0
        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);
        }
コード例 #16
0
 private Plant SeedDatabaseForGetNutrientSummaryTesting()
 {
     using (SpiceContext ctx = SetupInMemoryDatabase())
     {
         Plant    plant = Plants.ModelFactory.DomainModel();
         Nutrient water = new Nutrient {
             Name = "Water"
         };
         AdministeredNutrient administeredWater1 =
             ModelFactory.DomainModel(water, date: new DateTime(2018, 1, 1, 0, 0, 0));
         AdministeredNutrient administeredWater2 =
             ModelFactory.DomainModel(water, date: new DateTime(2018, 2, 1, 0, 0, 0));
         AdministeredNutrient administeredWater3 =
             ModelFactory.DomainModel(water, date: new DateTime(2018, 3, 1, 0, 0, 0));
         AdministeredNutrient administeredWater4 =
             ModelFactory.DomainModel(water, date: new DateTime(2018, 4, 1, 0, 0, 0));
         AdministeredNutrient administeredWater5 =
             ModelFactory.DomainModel(water, date: new DateTime(2018, 5, 1, 0, 0, 0));
         AdministeredNutrient administerdWater6 =
             ModelFactory.DomainModel(water, date: new DateTime(2019, 1, 1, 0, 0, 0));
         Nutrient fertilizer = new Nutrient {
             Name = "Fertilizer"
         };
         AdministeredNutrient administeredFertilizer =
             ModelFactory.DomainModel(fertilizer, date: new DateTime(2018, 1, 1, 0, 0, 0));
         plant.AdministeredNutrients.Add(administeredWater1);
         plant.AdministeredNutrients.Add(administeredWater2);
         plant.AdministeredNutrients.Add(administeredWater3);
         plant.AdministeredNutrients.Add(administeredWater4);
         plant.AdministeredNutrients.Add(administeredWater5);
         plant.AdministeredNutrients.Add(administerdWater6);
         plant.AdministeredNutrients.Add(administeredFertilizer);
         ctx.Plants.Add(plant);
         ctx.Nutrients.Add(water);
         ctx.Nutrients.Add(fertilizer);
         ctx.Save();
         return(plant);
     }
 }
コード例 #17
0
        public async Task Delete(Guid plantId, Guid id)
        {
            Plant plant = await _database.Plants
                          .Include(x => x.AdministeredNutrients)
                          .FirstOrDefaultAsync(x => x.Id == plantId);

            if (plant is null)
            {
                return;
            }

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

            if (administeredNutrient is null)
            {
                return;
            }

            plant.AdministeredNutrients.Remove(administeredNutrient);
            _database.AdministeredNutrients.Remove(administeredNutrient);
            await _database.SaveAsync();
        }
コード例 #18
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);
        }
コード例 #19
0
        private Domain.Species SeedDatabaseForGetSpeciesSummaryTesting()
        {
            using (SpiceContext ctx = SetupInMemoryDatabase())
            {
                // test data overview: three plants of one species growing on three different fields
                Domain.Species species = ModelFactory.DomainModel();
                Plant          plant1  = Plants.ModelFactory.DomainModel(species: species);
                Plant          plant2  = Plants.ModelFactory.DomainModel(species: species);
                Plant          plant3  = Plants.ModelFactory.DomainModel(species: species);

                // two nutrients - water and fertilizer
                Nutrient water      = Nutrients.ModelFactory.DomainModel("Water");
                Nutrient fertilizer = Nutrients.ModelFactory.DomainModel("Fertilizer", dosageUnits: "g");

                // plant #1 was watered 3 times with 1ml of water and fertilized once with 1 gram of fertilizer
                AdministeredNutrient waterPlant11 =
                    Plants.Nutrients.ModelFactory.DomainModel(water, plant1, new DateTime(2017, 01, 01, 0, 0, 0));
                AdministeredNutrient waterPlant12 =
                    Plants.Nutrients.ModelFactory.DomainModel(water, plant1, new DateTime(2018, 01, 01, 0, 0, 0));
                AdministeredNutrient waterPlant13 =
                    Plants.Nutrients.ModelFactory.DomainModel(water, plant1, new DateTime(2019, 01, 01, 0, 0, 0));
                AdministeredNutrient fertilizerPlant1 =
                    Plants.Nutrients.ModelFactory.DomainModel(fertilizer, plant1, new DateTime(2018, 01, 01, 0, 0, 0));
                plant1.AdministeredNutrients.Add(waterPlant11);
                plant1.AdministeredNutrients.Add(waterPlant12);
                plant1.AdministeredNutrients.Add(waterPlant13);
                plant1.AdministeredNutrients.Add(fertilizerPlant1);

                // plant #2 was watered 2 times with 1ml of water and fertilized twice with 1 gram of fertilizer
                AdministeredNutrient waterPlant21 =
                    Plants.Nutrients.ModelFactory.DomainModel(water, plant2, new DateTime(2017, 01, 01, 0, 0, 0));
                AdministeredNutrient waterPlant22 =
                    Plants.Nutrients.ModelFactory.DomainModel(water, plant2, new DateTime(2018, 01, 02, 0, 0, 0));
                AdministeredNutrient fertilizerPlant21 =
                    Plants.Nutrients.ModelFactory.DomainModel(fertilizer, plant2, new DateTime(2018, 02, 01, 0, 0, 0));
                AdministeredNutrient fertilizerPlant22 =
                    Plants.Nutrients.ModelFactory.DomainModel(fertilizer, plant2, new DateTime(2019, 01, 01, 0, 0, 0));
                plant2.AdministeredNutrients.Add(waterPlant21);
                plant2.AdministeredNutrients.Add(waterPlant22);
                plant2.AdministeredNutrients.Add(fertilizerPlant21);
                plant2.AdministeredNutrients.Add(fertilizerPlant22);

                // plant #3 was not watered and was fertilized four times with 1 gram of fertilizer
                AdministeredNutrient fertilizerPlant31 =
                    Plants.Nutrients.ModelFactory.DomainModel(fertilizer, plant3, new DateTime(2017, 01, 01, 0, 0, 0));
                AdministeredNutrient fertilizerPlant32 =
                    Plants.Nutrients.ModelFactory.DomainModel(fertilizer, plant3, new DateTime(2018, 01, 01, 0, 0, 0));
                AdministeredNutrient fertilizerPlant33 =
                    Plants.Nutrients.ModelFactory.DomainModel(fertilizer, plant3, new DateTime(2018, 02, 02, 0, 0, 0));
                AdministeredNutrient fertilizerPlant34 =
                    Plants.Nutrients.ModelFactory.DomainModel(fertilizer, plant3, new DateTime(2019, 01, 01, 0, 0, 0));
                plant3.AdministeredNutrients.Add(fertilizerPlant31);
                plant3.AdministeredNutrients.Add(fertilizerPlant32);
                plant3.AdministeredNutrients.Add(fertilizerPlant33);
                plant3.AdministeredNutrients.Add(fertilizerPlant34);

                // Water used in 2017: 2
                // Water used in 2018: 2
                // Water used in 2019: 1
                // Fertilizer used in 2017: 1
                // Fertilizer used in 2018: 4
                // Fertilizer used in 2019: 2
                // Total used water: 5 ml
                // Total used fertilizer: 7 g
                ctx.Species.Add(species);
                ctx.Nutrients.Add(water);
                ctx.Nutrients.Add(fertilizer);
                ctx.Plants.Add(plant1);
                ctx.Plants.Add(plant2);
                ctx.Plants.Add(plant3);
                ctx.Save();

                return(species);
            }
        }