mirror of
https://github.com/azaion/satellite-provider.git
synced 2026-04-22 11:06:38 +00:00
geo fences - wip
This commit is contained in:
@@ -10,8 +10,9 @@ public interface IRouteRepository
|
||||
Task InsertRoutePointsAsync(IEnumerable<RoutePointEntity> points);
|
||||
Task<int> UpdateRouteAsync(RouteEntity route);
|
||||
Task<int> DeleteRouteAsync(Guid id);
|
||||
Task LinkRouteToRegionAsync(Guid routeId, Guid regionId);
|
||||
Task LinkRouteToRegionAsync(Guid routeId, Guid regionId, bool isGeofence = false);
|
||||
Task<IEnumerable<Guid>> GetRegionIdsByRouteAsync(Guid routeId);
|
||||
Task<IEnumerable<Guid>> GetGeofenceRegionIdsByRouteAsync(Guid routeId);
|
||||
Task<IEnumerable<RouteEntity>> GetRoutesWithPendingMapsAsync();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Npgsql;
|
||||
using SatelliteProvider.DataAccess.Models;
|
||||
|
||||
@@ -7,10 +8,12 @@ namespace SatelliteProvider.DataAccess.Repositories;
|
||||
public class RegionRepository : IRegionRepository
|
||||
{
|
||||
private readonly string _connectionString;
|
||||
private readonly ILogger<RegionRepository> _logger;
|
||||
|
||||
public RegionRepository(string connectionString)
|
||||
public RegionRepository(string connectionString, ILogger<RegionRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<RegionEntity?> GetByIdAsync(Guid id)
|
||||
@@ -26,7 +29,15 @@ public class RegionRepository : IRegionRepository
|
||||
FROM regions
|
||||
WHERE id = @Id";
|
||||
|
||||
return await connection.QuerySingleOrDefaultAsync<RegionEntity>(sql, new { Id = id });
|
||||
var region = await connection.QuerySingleOrDefaultAsync<RegionEntity>(sql, new { Id = id });
|
||||
|
||||
if (region != null)
|
||||
{
|
||||
_logger.LogInformation("RegionRepository - Read region {RegionId} from DB: Lat={Lat:F12}, Lon={Lon:F12}, Status={Status}",
|
||||
id, region.Latitude, region.Longitude, region.Status);
|
||||
}
|
||||
|
||||
return region;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<RegionEntity>> GetByStatusAsync(string status)
|
||||
@@ -60,6 +71,9 @@ public class RegionRepository : IRegionRepository
|
||||
@CreatedAt, @UpdatedAt)
|
||||
RETURNING id";
|
||||
|
||||
_logger.LogInformation("RegionRepository - Inserting region {RegionId} to DB: Lat={Lat:F12}, Lon={Lon:F12}, Status={Status}",
|
||||
region.Id, region.Latitude, region.Longitude, region.Status);
|
||||
|
||||
return await connection.ExecuteScalarAsync<Guid>(sql, region);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Npgsql;
|
||||
using SatelliteProvider.DataAccess.Models;
|
||||
|
||||
@@ -7,10 +8,12 @@ namespace SatelliteProvider.DataAccess.Repositories;
|
||||
public class RouteRepository : IRouteRepository
|
||||
{
|
||||
private readonly string _connectionString;
|
||||
private readonly ILogger<RouteRepository> _logger;
|
||||
|
||||
public RouteRepository(string connectionString)
|
||||
public RouteRepository(string connectionString, ILogger<RouteRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<RouteEntity?> GetByIdAsync(Guid id)
|
||||
@@ -40,7 +43,16 @@ public class RouteRepository : IRouteRepository
|
||||
WHERE route_id = @RouteId
|
||||
ORDER BY sequence_number";
|
||||
|
||||
return await connection.QueryAsync<RoutePointEntity>(sql, new { RouteId = routeId });
|
||||
var points = (await connection.QueryAsync<RoutePointEntity>(sql, new { RouteId = routeId })).ToList();
|
||||
|
||||
if (points.Any())
|
||||
{
|
||||
_logger.LogInformation("RouteRepository - Read {Count} points from DB for route {RouteId}. First: Lat={Lat:F12}, Lon={Lon:F12}, Last: Lat={LastLat:F12}, Lon={LastLon:F12}",
|
||||
points.Count, routeId, points[0].Latitude, points[0].Longitude,
|
||||
points[^1].Latitude, points[^1].Longitude);
|
||||
}
|
||||
|
||||
return points;
|
||||
}
|
||||
|
||||
public async Task<Guid> InsertRouteAsync(RouteEntity route)
|
||||
@@ -69,7 +81,15 @@ public class RouteRepository : IRouteRepository
|
||||
VALUES (@Id, @RouteId, @SequenceNumber, @Latitude, @Longitude,
|
||||
@PointType, @SegmentIndex, @DistanceFromPrevious, @CreatedAt)";
|
||||
|
||||
await connection.ExecuteAsync(sql, points);
|
||||
var pointsList = points.ToList();
|
||||
if (pointsList.Any())
|
||||
{
|
||||
_logger.LogInformation("RouteRepository - Inserting {Count} points to DB. First: Lat={Lat:F12}, Lon={Lon:F12}, Last: Lat={LastLat:F12}, Lon={LastLon:F12}",
|
||||
pointsList.Count, pointsList[0].Latitude, pointsList[0].Longitude,
|
||||
pointsList[^1].Latitude, pointsList[^1].Longitude);
|
||||
}
|
||||
|
||||
await connection.ExecuteAsync(sql, pointsList);
|
||||
}
|
||||
|
||||
public async Task<int> UpdateRouteAsync(RouteEntity route)
|
||||
@@ -101,15 +121,15 @@ public class RouteRepository : IRouteRepository
|
||||
return await connection.ExecuteAsync(sql, new { Id = id });
|
||||
}
|
||||
|
||||
public async Task LinkRouteToRegionAsync(Guid routeId, Guid regionId)
|
||||
public async Task LinkRouteToRegionAsync(Guid routeId, Guid regionId, bool isGeofence = false)
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString);
|
||||
const string sql = @"
|
||||
INSERT INTO route_regions (route_id, region_id, created_at)
|
||||
VALUES (@RouteId, @RegionId, @CreatedAt)
|
||||
INSERT INTO route_regions (route_id, region_id, is_geofence, created_at)
|
||||
VALUES (@RouteId, @RegionId, @IsGeofence, @CreatedAt)
|
||||
ON CONFLICT (route_id, region_id) DO NOTHING";
|
||||
|
||||
await connection.ExecuteAsync(sql, new { RouteId = routeId, RegionId = regionId, CreatedAt = DateTime.UtcNow });
|
||||
await connection.ExecuteAsync(sql, new { RouteId = routeId, RegionId = regionId, IsGeofence = isGeofence, CreatedAt = DateTime.UtcNow });
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Guid>> GetRegionIdsByRouteAsync(Guid routeId)
|
||||
@@ -118,7 +138,18 @@ public class RouteRepository : IRouteRepository
|
||||
const string sql = @"
|
||||
SELECT region_id
|
||||
FROM route_regions
|
||||
WHERE route_id = @RouteId";
|
||||
WHERE route_id = @RouteId AND (is_geofence = false OR is_geofence IS NULL)";
|
||||
|
||||
return await connection.QueryAsync<Guid>(sql, new { RouteId = routeId });
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Guid>> GetGeofenceRegionIdsByRouteAsync(Guid routeId)
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString);
|
||||
const string sql = @"
|
||||
SELECT region_id
|
||||
FROM route_regions
|
||||
WHERE route_id = @RouteId AND is_geofence = true";
|
||||
|
||||
return await connection.QueryAsync<Guid>(sql, new { RouteId = routeId });
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Npgsql;
|
||||
using SatelliteProvider.DataAccess.Models;
|
||||
|
||||
@@ -7,10 +8,12 @@ namespace SatelliteProvider.DataAccess.Repositories;
|
||||
public class TileRepository : ITileRepository
|
||||
{
|
||||
private readonly string _connectionString;
|
||||
private readonly ILogger<TileRepository> _logger;
|
||||
|
||||
public TileRepository(string connectionString)
|
||||
public TileRepository(string connectionString, ILogger<TileRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<TileEntity?> GetByIdAsync(Guid id)
|
||||
@@ -24,7 +27,15 @@ public class TileRepository : ITileRepository
|
||||
FROM tiles
|
||||
WHERE id = @Id";
|
||||
|
||||
return await connection.QuerySingleOrDefaultAsync<TileEntity>(sql, new { Id = id });
|
||||
var tile = await connection.QuerySingleOrDefaultAsync<TileEntity>(sql, new { Id = id });
|
||||
|
||||
if (tile != null)
|
||||
{
|
||||
_logger.LogInformation("TileRepository - Read tile {TileId} from DB: Lat={Lat:F12}, Lon={Lon:F12}, Zoom={Zoom}",
|
||||
id, tile.Latitude, tile.Longitude, tile.ZoomLevel);
|
||||
}
|
||||
|
||||
return tile;
|
||||
}
|
||||
|
||||
public async Task<TileEntity?> FindExistingTileAsync(double latitude, double longitude, double tileSizeMeters, int zoomLevel, int version)
|
||||
@@ -105,6 +116,9 @@ public class TileRepository : ITileRepository
|
||||
updated_at = EXCLUDED.updated_at
|
||||
RETURNING id";
|
||||
|
||||
_logger.LogInformation("TileRepository - Inserting tile {TileId} to DB: Lat={Lat:F12}, Lon={Lon:F12}, Zoom={Zoom}",
|
||||
tile.Id, tile.Latitude, tile.Longitude, tile.ZoomLevel);
|
||||
|
||||
return await connection.ExecuteScalarAsync<Guid>(sql, tile);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user