mirror of
https://github.com/azaion/satellite-provider.git
synced 2026-04-22 09:06:38 +00:00
geo fences - wip
This commit is contained in:
@@ -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 });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user