Files
satellite-provider/SatelliteProvider.DataAccess/TypeHandlers/EnumStringTypeHandler.cs
T
Oleksandr Bezdieniezhnykh 687d6bdd5b [AZ-484] Multi-source tile storage: source + captured_at
Add per-source tile rows to support multi-provider imagery (Google
Maps + future UAV). Migration 013 (transactional) introduces
source/captured_at columns, backfills existing rows to
(source='google_maps', captured_at=created_at), and replaces the
4-column unique index with a 5-column index that includes source.

TileRepository:
- ColumnList includes source + captured_at
- GetByTileCoordinatesAsync returns most-recent row across sources
  (ORDER BY captured_at DESC, updated_at DESC, id DESC)
- GetTilesByRegionAsync uses DISTINCT ON to pick the most-recent
  tile per cell, restoring caller-facing row order
- Insert/Update upsert on the new 5-column conflict key

TileSource enum lives in Common.Enums. Snake_case wire format
(google_maps, uav) is enforced by a focused TileSourceTypeHandler
because the generic ToLowerInvariant pattern would emit
"googlemaps", violating contract v1.0.0.

TileService stamps Source=GoogleMaps + CapturedAt=UtcNow on every
new tile. Tile-storage contract is now frozen at v1.0.0.

AC coverage 7/7. New unit + integration tests cover all ACs;
existing 200 unit + 5 smoke tests preserved.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-11 06:21:59 +03:00

53 lines
1.5 KiB
C#

using System.Data;
using Dapper;
using SatelliteProvider.Common.Enums;
namespace SatelliteProvider.DataAccess.TypeHandlers;
public class EnumStringTypeHandler<T> : SqlMapper.TypeHandler<T> where T : struct, Enum
{
public override T Parse(object value)
{
if (value is null || value is DBNull)
{
throw new DataException($"Cannot parse null DB value into enum {typeof(T).Name}");
}
var s = value as string ?? value.ToString();
if (string.IsNullOrEmpty(s))
{
throw new DataException($"Cannot parse empty DB value into enum {typeof(T).Name}");
}
if (!Enum.TryParse<T>(s, ignoreCase: true, out var parsed) || !Enum.IsDefined(parsed))
{
throw new DataException($"DB value '{s}' is not a defined member of enum {typeof(T).Name}");
}
return parsed;
}
public override void SetValue(IDbDataParameter parameter, T value)
{
parameter.Value = value.ToString().ToLowerInvariant();
parameter.DbType = DbType.String;
}
}
public static class DapperEnumTypeHandlers
{
private static int _registered;
public static void RegisterAll()
{
if (Interlocked.Exchange(ref _registered, 1) == 1)
{
return;
}
SqlMapper.AddTypeHandler(new EnumStringTypeHandler<RegionStatus>());
SqlMapper.AddTypeHandler(new EnumStringTypeHandler<RoutePointType>());
SqlMapper.AddTypeHandler(new TileSourceTypeHandler());
}
}