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); }
public void CreateAdministeredNutrientThrowsExceptionIfPlantDoesNotExist() { // Given CreateAdministeredNutrientModel model = ModelFactory.CreationModel(Guid.NewGuid()); // When Func <Task> createPlant = async() => await _commands.Create(Guid.NewGuid(), model); // Then createPlant.Should().Throw <PlantNotFoundException>(); }
public void CreateAdministeredNutrientThrowsExceptionIfNutrientDoesNotExist() { // Given Guid plantId = SeedDatabase(Plants.ModelFactory.DomainModel()); CreateAdministeredNutrientModel model = ModelFactory.CreationModel(); // When Func <Task> createPlant = async() => await _commands.Create(plantId, model); // Then createPlant.Should().Throw <NutrientNotFoundException>(); }
public void CreateAdministeredNutrientThrowsExceptionIfAdministrationDateIsEarlierThanPlantDate() { // Given Plant plant = Plants.ModelFactory.DomainModel(); Guid plantId = SeedDatabase(plant); Guid nutrientId = SeedDatabase(Tests.Nutrients.ModelFactory.DomainModel()); CreateAdministeredNutrientModel model = ModelFactory.CreationModel(nutrientId, plant.Planted.AddDays(-1)); // When Func <Task> createPlant = async() => await _commands.Create(plantId, model); // Then createPlant.Should().Throw <NutrientAdministrationDateBeforePlantDateException>(); }
public async Task CreateAdministeredNutrientReturnsGuidOnSuccessAndCreatesEvent() { // Given Plant plant = Plants.ModelFactory.DomainModel(); plant.Planted = DateTime.Now.AddDays(-1); Guid plantId = SeedDatabase(plant); Guid nutrientId = SeedDatabase(Tests.Nutrients.ModelFactory.DomainModel()); CreateAdministeredNutrientModel model = ModelFactory.CreationModel(nutrientId, createEvent: true); // When Guid id = await _commands.Create(plantId, model); plant = await DatabaseContext.Plants.Include(x => x.Events).FirstOrDefaultAsync(x => x.Id == plantId); // Then id.Should().NotBe(Guid.Empty); plant.Events.Should().Contain(x => x.Type == EventType.Nutrition); }
public async Task <ActionResult> Post([FromRoute] Guid plantId, [FromBody] CreateAdministeredNutrientViewModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { CreateAdministeredNutrientModel createAdministeredNutrientModel = _mapper.Map <CreateAdministeredNutrientModel>(model); Guid nutrientId = await _commands.Create(plantId, createAdministeredNutrientModel); return(CreatedAtRoute(nameof(GetAdministeredNutrient), new { plantId = plantId, id = nutrientId }, null)); } catch (Exception ex) when(ex is ResourceNotFoundException) { return(NotFound(new ErrorViewModel(ex))); } catch (Exception ex) when(ex is ResourceStateException) { return(Conflict(new ErrorViewModel(ex))); } }