public Task <string> GetName(GridUnitIdentity identity)
 {
     return(this.ExecuteInContextAsync(async dbContext =>
     {
         var name = await dbContext.GridUnits.GetByIdentity(identity).Select(x => x.Name).SingleAsync();
         return name;
     }));
 }
        public Task <LngLat[]> GetBoundary(GridUnitIdentity identity)
        {
            return(this.ExecuteInContextAsync(async dbContext =>
            {
                var entity = await dbContext.GridUnits.GetByIdentity(identity).SingleAsync();

                var lngLats = entity.Boundary.ToLngLats();
                return lngLats;
            }));
        }
        // Getters and setters for catchment components
        public async Task SetName(GridUnitIdentity identity, string name)
        {
            await this.ExecuteInContextAsync(async dbContext =>
            {
                var entity = await dbContext.GridUnits.GetByIdentity(identity).SingleAsync();

                entity.Name = name;

                await dbContext.SaveChangesAsync();
            });
        }
        public async Task SetBoundary(GridUnitIdentity identity, IEnumerable <LngLat> boundaryVertices)
        {
            var geometryFactory = await this.GeometryFactoryProvider.GetGeometryFactoryAsync();

            var polygon = boundaryVertices.ToPolygon(geometryFactory);

            await this.ExecuteInContextAsync(async dbContext =>
            {
                var entity = await dbContext.GridUnits.GetByIdentity(identity).SingleAsync();

                entity.Boundary = polygon;

                await dbContext.SaveChangesAsync();
            });
        }
        public async Task SetGridUnitsForCatchment(CatchmentIdentity catchmentIdentity)
        {
            await this.ExecuteInContextAsync(async dbContext =>
            {
                // Get the right catchment (also verifies that it's in the database)
                var catchmentEntity = await dbContext.Catchments
                                      .Where(x => x.Identity == catchmentIdentity.Value)
                                      .SingleAsync();

                // Delete existing grid unit affiliations (but NOT the grid units!!)
                var matchingGridUnitAffiliations = await dbContext.CatchmentGridUnits
                                                   .Where(x => x.CatchmentIdentity == catchmentIdentity.Value)
                                                   .ToListAsync();
                if (matchingGridUnitAffiliations.Count > 0)
                {
                    dbContext.RemoveRange(matchingGridUnitAffiliations);
                    // TODO: do we need this save changes before adding the new entities?
                    await dbContext.SaveChangesAsync();
                }

                // Find its intersecting grid units
                var gridUnitIdentities = await dbContext.GridUnits
                                         .Where(x => x.Boundary.Intersects(catchmentEntity.Boundary))
                                         .Select(x => GridUnitIdentity.From(x.GUID))
                                         .ToListAsync();

                // Add them to the database
                foreach (var gridUnitIdentity in gridUnitIdentities)
                {
                    // TODO: update to addrange once I have time to check that it works
                    // (which should speed this sucker up a bit.)
                    var newCatchmentGridUnitEntity = new Entities.CatchmentGridUnit
                    {
                        GridUnitIdentity  = gridUnitIdentity.Value,
                        CatchmentIdentity = catchmentIdentity.Value
                    };
                    dbContext.Add(newCatchmentGridUnitEntity);
                }

                await dbContext.SaveChangesAsync();
            });
        }
        public static IQueryable <GridUnitEntity> GetByIdentity(this IQueryable <GridUnitEntity> gridUnits, GridUnitIdentity gridUnitIdentity)
        {
            var queryable = gridUnits.Where(x => x.GUID == gridUnitIdentity.Value);

            return(queryable);
        }
 public Task <GridUnit> Get(GridUnitIdentity identity)
 {
     throw new NotImplementedException();
 }
 public Task <bool> Exists(GridUnitIdentity identity)
 {
     throw new NotImplementedException();
 }
 public Task Delete(GridUnitIdentity identity)
 {
     throw new NotImplementedException();
 }