public async Task <Guid> Create(CreatePlantModel model) { Field field = await _database.Fields.FirstOrDefaultAsync(x => x.Id == model.FieldId); if (field is null) { throw new FieldNotFoundException(model.FieldId); } IQueryable <Plant> plantsExistingAtCoordinates = from existingPlantsAtCoordinates in _database.Plants where (existingPlantsAtCoordinates.Field.Id == field.Id) && (existingPlantsAtCoordinates.Row == model.Row) && (existingPlantsAtCoordinates.Column == model.Column) && (existingPlantsAtCoordinates.Field.Id == model.FieldId) select existingPlantsAtCoordinates; if (await plantsExistingAtCoordinates.AnyAsync()) { throw new PlantExistsAtCoordinatesException(model.Row, model.Column); } Domain.Species species = await _database.Species.FindAsync(model.SpeciesId); if (species is null) { throw new SpeciesNotFoundException(model.SpeciesId); } Plant plant = new Plant(model.Name, species, field, model.Row, model.Column, model.State); await _database.Plants.AddAsync(plant); await _database.SaveAsync(); return(plant.Id); }
public void CreatePlantThrowsExceptionIfFieldDoesNotExist() { // Given SeedDatabase(ModelFactory.DomainModel()); CreatePlantModel model = ModelFactory.CreationModel(); // When Func <Task> createPlant = async() => await _commands.Create(model); // Then createPlant.Should().Throw <FieldNotFoundException>(); }
public void CreatePlantThrowsExceptionIfSpeciesDoesNotExist() { // Given Field field = Fields.ModelFactory.DomainModel(); Guid fieldId = SeedDatabase(field); CreatePlantModel model = ModelFactory.CreationModel(fieldId); // When Func <Task> createPlant = async() => await _commands.Create(model); // Then createPlant.Should().Throw <SpeciesNotFoundException>(); }
public void CreatePlantThrowsExceptionIfPlantExistsAtCoordinates() { // Given Field field = Fields.ModelFactory.DomainModel(); Guid fieldId = SeedDatabase(field); SeedDatabase(ModelFactory.DomainModel(field)); CreatePlantModel model = ModelFactory.CreationModel(fieldId); // When Func <Task> createPlant = async() => await _commands.Create(model); // Then createPlant.Should().Throw <PlantExistsAtCoordinatesException>(); }
public async Task CreatePlantReturnsGuidOnSuccess() { // Given Guid fieldId = SeedDatabase(Fields.ModelFactory.DomainModel()); Guid speciesId = SeedDatabase(Species.ModelFactory.DomainModel()); CreatePlantModel model = ModelFactory.CreationModel(fieldId, speciesId); // When Guid id = await _commands.Create(model); Plant createdPlant = await DatabaseContext.Plants.Include(x => x.Events).FirstOrDefaultAsync(x => x.Id == id); // Then id.Should().NotBe(Guid.Empty); createdPlant.Events.Should().NotBeNullOrEmpty(); }
public async Task <ActionResult> Post([FromBody] CreatePlantViewModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { CreatePlantModel createPlantModel = _mapper.Map <CreatePlantModel>(model); Guid plantId = await _commands.Create(createPlantModel); return(CreatedAtRoute(nameof(GetPlant), new { id = plantId }, 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))); } }