mirror of
https://github.com/azaion/satellite-provider.git
synced 2026-04-22 23:46:38 +00:00
route in progress, region stitching is disabled by default
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
using Dapper;
|
||||
using Npgsql;
|
||||
using SatelliteProvider.DataAccess.Models;
|
||||
|
||||
namespace SatelliteProvider.DataAccess.Repositories;
|
||||
|
||||
public class RouteRepository : IRouteRepository
|
||||
{
|
||||
private readonly string _connectionString;
|
||||
|
||||
public RouteRepository(string connectionString)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
}
|
||||
|
||||
public async Task<RouteEntity?> GetByIdAsync(Guid id)
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString);
|
||||
const string sql = @"
|
||||
SELECT id, name, description, region_size_meters as RegionSizeMeters,
|
||||
zoom_level as ZoomLevel, total_distance_meters as TotalDistanceMeters,
|
||||
total_points as TotalPoints, created_at as CreatedAt, updated_at as UpdatedAt
|
||||
FROM routes
|
||||
WHERE id = @Id";
|
||||
|
||||
return await connection.QuerySingleOrDefaultAsync<RouteEntity>(sql, new { Id = id });
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<RoutePointEntity>> GetRoutePointsAsync(Guid routeId)
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString);
|
||||
const string sql = @"
|
||||
SELECT id, route_id as RouteId, sequence_number as SequenceNumber,
|
||||
latitude, longitude, point_type as PointType, segment_index as SegmentIndex,
|
||||
distance_from_previous as DistanceFromPrevious, created_at as CreatedAt
|
||||
FROM route_points
|
||||
WHERE route_id = @RouteId
|
||||
ORDER BY sequence_number";
|
||||
|
||||
return await connection.QueryAsync<RoutePointEntity>(sql, new { RouteId = routeId });
|
||||
}
|
||||
|
||||
public async Task<Guid> InsertRouteAsync(RouteEntity route)
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString);
|
||||
const string sql = @"
|
||||
INSERT INTO routes (id, name, description, region_size_meters, zoom_level,
|
||||
total_distance_meters, total_points, created_at, updated_at)
|
||||
VALUES (@Id, @Name, @Description, @RegionSizeMeters, @ZoomLevel,
|
||||
@TotalDistanceMeters, @TotalPoints, @CreatedAt, @UpdatedAt)
|
||||
RETURNING id";
|
||||
|
||||
return await connection.ExecuteScalarAsync<Guid>(sql, route);
|
||||
}
|
||||
|
||||
public async Task InsertRoutePointsAsync(IEnumerable<RoutePointEntity> points)
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString);
|
||||
const string sql = @"
|
||||
INSERT INTO route_points (id, route_id, sequence_number, latitude, longitude,
|
||||
point_type, segment_index, distance_from_previous, created_at)
|
||||
VALUES (@Id, @RouteId, @SequenceNumber, @Latitude, @Longitude,
|
||||
@PointType, @SegmentIndex, @DistanceFromPrevious, @CreatedAt)";
|
||||
|
||||
await connection.ExecuteAsync(sql, points);
|
||||
}
|
||||
|
||||
public async Task<int> UpdateRouteAsync(RouteEntity route)
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString);
|
||||
const string sql = @"
|
||||
UPDATE routes
|
||||
SET name = @Name,
|
||||
description = @Description,
|
||||
region_size_meters = @RegionSizeMeters,
|
||||
zoom_level = @ZoomLevel,
|
||||
total_distance_meters = @TotalDistanceMeters,
|
||||
total_points = @TotalPoints,
|
||||
updated_at = @UpdatedAt
|
||||
WHERE id = @Id";
|
||||
|
||||
return await connection.ExecuteAsync(sql, route);
|
||||
}
|
||||
|
||||
public async Task<int> DeleteRouteAsync(Guid id)
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString);
|
||||
const string sql = "DELETE FROM routes WHERE id = @Id";
|
||||
return await connection.ExecuteAsync(sql, new { Id = id });
|
||||
}
|
||||
|
||||
public async Task LinkRouteToRegionAsync(Guid routeId, Guid regionId)
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString);
|
||||
const string sql = @"
|
||||
INSERT INTO route_regions (route_id, region_id, created_at)
|
||||
VALUES (@RouteId, @RegionId, @CreatedAt)
|
||||
ON CONFLICT (route_id, region_id) DO NOTHING";
|
||||
|
||||
await connection.ExecuteAsync(sql, new { RouteId = routeId, RegionId = regionId, CreatedAt = DateTime.UtcNow });
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Guid>> GetRegionIdsByRouteAsync(Guid routeId)
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString);
|
||||
const string sql = @"
|
||||
SELECT region_id
|
||||
FROM route_regions
|
||||
WHERE route_id = @RouteId";
|
||||
|
||||
return await connection.QueryAsync<Guid>(sql, new { RouteId = routeId });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user