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

            try
            {
                UpdatePlantModel updatePlantModel = _mapper.Map <UpdatePlantModel>(model);
                updatePlantModel.Id = id;

                Plant plant = await _commands.Update(updatePlantModel);

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

                return(Ok(_mapper.Map <PlantDetailsViewModel>(plant)));
            }
            catch (Exception ex) when(ex is ResourceNotFoundException)
            {
                return(NotFound(new ErrorViewModel(ex)));
            }
            catch (Exception ex) when(ex is ResourceStateException)
            {
                return(Conflict(new ErrorViewModel(ex)));
            }
        }
Example #2
0
        public async Task UpdatePlantReturnsPlantOnSuccess()
        {
            // Given
            Field newField   = Fields.ModelFactory.DomainModel();
            Guid  newFieldId = SeedDatabase(newField);

            Domain.Species newSpecies   = Species.ModelFactory.DomainModel();
            Guid           newSpeciesId = SeedDatabase(newSpecies);
            Plant          plant        = ModelFactory.DomainModel(row: 13, col: 37);
            Guid           plantId      = SeedDatabase(plant);

            UpdatePlantModel model = ModelFactory.UpdateModel(plantId, newFieldId, newSpeciesId);

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

            // Then
            plant.Should().NotBeNull();
            plant.Id.Should().Be(plantId);
            plant.Name.Should().Contain("Red");
            plant.Field.Should().Be(newField);
            plant.Species.Should().Be(newSpecies);
            plant.Column.Should().Be(0);
            plant.Row.Should().Be(0);
            plant.Planted.Day.Should().Be(DateTime.Now.Day);
            plant.State.Should().Be(PlantState.Healthy);
            plant.Events.Should().NotBeNullOrEmpty();
        }
Example #3
0
        public async Task UpdatePlantReturnsNullIfPlantDoesNotExist()
        {
            // Given
            UpdatePlantModel model = ModelFactory.UpdateModel();

            // When
            Plant plant = await _commands.Update(model);

            // Then
            plant.Should().BeNull();
        }
Example #4
0
        public void UpdatePlantThrowsExceptionIfFieldDoesNotExist()
        {
            // Given
            SeedDatabase(ModelFactory.DomainModel());   // existing plant
            Guid             id    = SeedDatabase(ModelFactory.DomainModel());
            UpdatePlantModel model = ModelFactory.UpdateModel(id);

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

            // Then
            updatePlant.Should().Throw <FieldNotFoundException>();
        }
        public IHttpActionResult Put(int plantID, UpdatePlantModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var service = CreatePlantService();

            if (!service.UpdatePlant(plantID, model))
            {
                return(InternalServerError());
            }

            return(Ok("Plant has been updated"));
        }
Example #6
0
        public void UpdatePlantThrowsExceptionIfPlantExistsAtCoordinates()
        {
            // Given
            Field field   = Fields.ModelFactory.DomainModel();
            Guid  fieldId = SeedDatabase(field);

            SeedDatabase(ModelFactory.DomainModel(field));   // existing plant on existing field
            Guid             id    = SeedDatabase(ModelFactory.DomainModel(field));
            UpdatePlantModel model = ModelFactory.UpdateModel(id, fieldId);

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

            // Then
            updatePlant.Should().Throw <PlantExistsAtCoordinatesException>();
        }
Example #7
0
        public async Task <Plant> Update(UpdatePlantModel model)
        {
            Plant plant = await _database.Plants
                          .Include(x => x.Field)
                          .FirstOrDefaultAsync(x => x.Id == model.Id);

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

            Field field = await _database.Fields
                          .Include(x => x.Plants)
                          .FirstOrDefaultAsync(x => x.Id == model.FieldId);

            if (field is null)
            {
                throw new FieldNotFoundException(model.FieldId);
            }

            if (field.Plants.Any(x => (x.Row == model.Row) && (x.Column == model.Column) && (x.Id != model.Id)))
            {
                throw new PlantExistsAtCoordinatesException(model.Row, model.Column);
            }

            Domain.Species species = await _database.Species.FirstOrDefaultAsync(x => x.Id == model.SpeciesId);

            if (species is null)
            {
                throw new SpeciesNotFoundException(model.SpeciesId);
            }

            _mapper.Map(model, plant);
            plant.ChangeField(field);
            plant.Species = species;
            _database.Plants.Update(plant);
            await _database.SaveAsync();

            return(plant);
        }
Example #8
0
        // UpdatePlant takes in the plantID of the plant you would like to update and the new information you want updated. The values must be within the set
        // values of the variables (SeasonID between 1-4, etc.).
        public bool UpdatePlant(int plantID, UpdatePlantModel model)
        {
            int       plantCareID = ctx.Plants.Single(e => e.PlantID == plantID).PlantCareID;
            PlantCare plantCare   = ctx.PlantCare.FirstOrDefault(e => e.PlantCareID == plantCareID);


            plantCare.SunExposureID = model.SunExposureID;
            plantCare.WaterNeedID   = model.WaterNeedID;
            plantCare.Temperature   = model.Temperature;
            plantCare.Description   = model.Description;
            plantCare.ModifiedDate  = DateTimeOffset.UtcNow;



            Plants plants = ctx.Plants.Single(e => e.PlantID == plantID);

            plants.Name           = model.Name;
            plants.ScientificName = model.ScientificName;
            plants.ZoneID         = model.ZoneID;
            plants.SeasonID       = model.SeasonID;
            plants.PlantTypeID    = model.PlantTypeID;
            plants.ModifiedDate   = DateTimeOffset.UtcNow;
            return(ctx.SaveChanges() == 2);
        }