[AZ-370] Refactor C17: status / point-type enums + AC RT2 update

Replaces bare strings with two enums in Common/Enums/:
  RegionStatus { Queued, Processing, Completed, Failed }
  RoutePointType { Start, End, Action, Intermediate }

Adds a Dapper EnumStringTypeHandler<T> (DataAccess/TypeHandlers/)
that round-trips enums to/from lowercase strings, registered once
at startup via DapperEnumTypeHandlers.RegisterAll(). DataAccess now
references Common (project ref) so entities can carry the enum types.

Sites converted: RegionService (5), RouteProcessingService (3),
RoutePointGraphBuilder (4), entity Status/PointType columns. Log
message and summary file format preserved via .ToLowerInvariant().

API JSON contract preserved by adding JsonStringEnumConverter with
JsonNamingPolicy.CamelCase to the http JSON options — single-word
enum members serialize to the same lowercase strings as before.

DTO renamed: Common.DTO.RegionStatus -> RegionStatusResponse to
free the RegionStatus name for the new enum (forced by the task's
explicit enum name); the renamed DTO has no public-API impact at
the JSON wire level. Stale doc references updated.

AC RT2 in _docs/00_problem/acceptance_criteria.md now lists all 4
point types (start/end/action/intermediate).

Tests: 171 / 171 unit + 5 / 5 smoke green (was 141 + 5; +30 new tests
covering type handler round-trip, set/parse, unknown-value rejection,
idempotent registration, and the AC RT2 doc check).

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Oleksandr Bezdieniezhnykh
2026-05-11 03:55:22 +03:00
parent 6d98c8f8d1
commit 23ab05766d
29 changed files with 357 additions and 84 deletions
@@ -1,3 +1,5 @@
using SatelliteProvider.Common.Enums;
namespace SatelliteProvider.DataAccess.Models;
public class RegionEntity
@@ -7,7 +9,7 @@ public class RegionEntity
public double Longitude { get; set; }
public double SizeMeters { get; set; }
public int ZoomLevel { get; set; }
public string Status { get; set; } = string.Empty;
public RegionStatus Status { get; set; }
public string? CsvFilePath { get; set; }
public string? SummaryFilePath { get; set; }
public int TilesDownloaded { get; set; }
@@ -1,3 +1,5 @@
using SatelliteProvider.Common.Enums;
namespace SatelliteProvider.DataAccess.Models;
public class RoutePointEntity
@@ -7,7 +9,7 @@ public class RoutePointEntity
public int SequenceNumber { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
public string PointType { get; set; } = string.Empty;
public RoutePointType PointType { get; set; }
public int SegmentIndex { get; set; }
public double? DistanceFromPrevious { get; set; }
public DateTime CreatedAt { get; set; }
@@ -1,3 +1,4 @@
using SatelliteProvider.Common.Enums;
using SatelliteProvider.DataAccess.Models;
namespace SatelliteProvider.DataAccess.Repositories;
@@ -5,7 +6,7 @@ namespace SatelliteProvider.DataAccess.Repositories;
public interface IRegionRepository
{
Task<RegionEntity?> GetByIdAsync(Guid id);
Task<IEnumerable<RegionEntity>> GetByStatusAsync(string status);
Task<IEnumerable<RegionEntity>> GetByStatusAsync(RegionStatus status);
Task<Guid> InsertAsync(RegionEntity region);
Task<int> UpdateAsync(RegionEntity region);
Task<int> DeleteAsync(Guid id);
@@ -1,6 +1,7 @@
using Dapper;
using Microsoft.Extensions.Logging;
using Npgsql;
using SatelliteProvider.Common.Enums;
using SatelliteProvider.DataAccess.Models;
namespace SatelliteProvider.DataAccess.Repositories;
@@ -33,7 +34,7 @@ public class RegionRepository : IRegionRepository
return region;
}
public async Task<IEnumerable<RegionEntity>> GetByStatusAsync(string status)
public async Task<IEnumerable<RegionEntity>> GetByStatusAsync(RegionStatus status)
{
using var connection = new NpgsqlConnection(_connectionString);
const string sql = @"
@@ -14,6 +14,10 @@
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.10" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SatelliteProvider.Common\SatelliteProvider.Common.csproj" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Migrations\*.sql" />
</ItemGroup>
@@ -0,0 +1,51 @@
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>());
}
}