better geo fences and points on the map

This commit is contained in:
Anton Martynenko
2025-11-19 17:59:52 +01:00
parent a148df1697
commit eff7ca4dba
5 changed files with 135 additions and 53 deletions
@@ -0,0 +1,2 @@
ALTER TABLE route_regions ADD COLUMN geofence_polygon_index INTEGER;
@@ -10,9 +10,10 @@ public interface IRouteRepository
Task InsertRoutePointsAsync(IEnumerable<RoutePointEntity> points);
Task<int> UpdateRouteAsync(RouteEntity route);
Task<int> DeleteRouteAsync(Guid id);
Task LinkRouteToRegionAsync(Guid routeId, Guid regionId, bool isGeofence = false);
Task LinkRouteToRegionAsync(Guid routeId, Guid regionId, bool isGeofence = false, int? geofencePolygonIndex = null);
Task<IEnumerable<Guid>> GetRegionIdsByRouteAsync(Guid routeId);
Task<IEnumerable<Guid>> GetGeofenceRegionIdsByRouteAsync(Guid routeId);
Task<Dictionary<int, List<Guid>>> GetGeofenceRegionsByPolygonAsync(Guid routeId);
Task<IEnumerable<RouteEntity>> GetRoutesWithPendingMapsAsync();
}
@@ -121,15 +121,15 @@ public class RouteRepository : IRouteRepository
return await connection.ExecuteAsync(sql, new { Id = id });
}
public async Task LinkRouteToRegionAsync(Guid routeId, Guid regionId, bool isGeofence = false)
public async Task LinkRouteToRegionAsync(Guid routeId, Guid regionId, bool isGeofence = false, int? geofencePolygonIndex = null)
{
using var connection = new NpgsqlConnection(_connectionString);
const string sql = @"
INSERT INTO route_regions (route_id, region_id, is_geofence, created_at)
VALUES (@RouteId, @RegionId, @IsGeofence, @CreatedAt)
INSERT INTO route_regions (route_id, region_id, is_geofence, geofence_polygon_index, created_at)
VALUES (@RouteId, @RegionId, @IsGeofence, @GeofencePolygonIndex, @CreatedAt)
ON CONFLICT (route_id, region_id) DO NOTHING";
await connection.ExecuteAsync(sql, new { RouteId = routeId, RegionId = regionId, IsGeofence = isGeofence, CreatedAt = DateTime.UtcNow });
await connection.ExecuteAsync(sql, new { RouteId = routeId, RegionId = regionId, IsGeofence = isGeofence, GeofencePolygonIndex = geofencePolygonIndex, CreatedAt = DateTime.UtcNow });
}
public async Task<IEnumerable<Guid>> GetRegionIdsByRouteAsync(Guid routeId)
@@ -169,5 +169,29 @@ public class RouteRepository : IRouteRepository
return await connection.QueryAsync<RouteEntity>(sql);
}
public async Task<Dictionary<int, List<Guid>>> GetGeofenceRegionsByPolygonAsync(Guid routeId)
{
using var connection = new NpgsqlConnection(_connectionString);
const string sql = @"
SELECT region_id, geofence_polygon_index
FROM route_regions
WHERE route_id = @RouteId AND is_geofence = true AND geofence_polygon_index IS NOT NULL
ORDER BY geofence_polygon_index";
var results = await connection.QueryAsync<(Guid RegionId, int PolygonIndex)>(sql, new { RouteId = routeId });
var grouped = new Dictionary<int, List<Guid>>();
foreach (var (regionId, polygonIndex) in results)
{
if (!grouped.ContainsKey(polygonIndex))
{
grouped[polygonIndex] = new List<Guid>();
}
grouped[polygonIndex].Add(regionId);
}
return grouped;
}
}