mirror of
https://github.com/azaion/satellite-provider.git
synced 2026-06-21 14:31:13 +00:00
f7ad7aa5ab
Extract RouteValidator (aggregating validator), RoutePointGraphBuilder (point interpolation + sequence numbering), GeofenceGridCalculator (NW/SE region centers), and RouteResponseMapper (entity -> DTO; also used by GetRouteAsync, eliminating duplicate DTO assembly). CreateRouteAsync shrinks 184 -> 52 LOC of orchestration. RouteService.cs shrinks 295 -> 138 LOC overall. Validation aggregates all failures into a single ArgumentException (AC-2); single-violation messages preserved verbatim so existing RouteServiceTests pass unchanged. 28 new unit tests for the four helpers (112/112 unit tests, smoke green). Co-authored-by: Cursor <cursoragent@cursor.com>
82 lines
2.9 KiB
C#
82 lines
2.9 KiB
C#
using SatelliteProvider.Common.DTO;
|
|
using SatelliteProvider.Common.Utils;
|
|
|
|
namespace SatelliteProvider.Services.RouteManagement;
|
|
|
|
public class RoutePointGraphBuilder
|
|
{
|
|
public const double MaxPointSpacingMeters = 200.0;
|
|
|
|
public RoutePointGraph Build(IReadOnlyList<RoutePoint> userPoints)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(userPoints);
|
|
if (userPoints.Count < 2)
|
|
{
|
|
throw new ArgumentException("Route must have at least 2 points", nameof(userPoints));
|
|
}
|
|
|
|
var allPoints = new List<RoutePointDto>();
|
|
var totalDistance = 0.0;
|
|
var sequenceNumber = 0;
|
|
|
|
for (int segmentIndex = 0; segmentIndex < userPoints.Count; segmentIndex++)
|
|
{
|
|
var current = userPoints[segmentIndex];
|
|
var isStart = segmentIndex == 0;
|
|
var isEnd = segmentIndex == userPoints.Count - 1;
|
|
|
|
var currentGeo = new GeoPoint(current.Latitude, current.Longitude);
|
|
|
|
double? distanceFromPrevious = null;
|
|
if (allPoints.Count > 0)
|
|
{
|
|
var prev = allPoints[^1];
|
|
var prevGeo = new GeoPoint(prev.Latitude, prev.Longitude);
|
|
distanceFromPrevious = GeoUtils.CalculateDistance(prevGeo, currentGeo);
|
|
totalDistance += distanceFromPrevious.Value;
|
|
}
|
|
|
|
allPoints.Add(new RoutePointDto
|
|
{
|
|
Latitude = current.Latitude,
|
|
Longitude = current.Longitude,
|
|
PointType = isStart ? "start" : (isEnd ? "end" : "action"),
|
|
SequenceNumber = sequenceNumber++,
|
|
SegmentIndex = segmentIndex,
|
|
DistanceFromPrevious = distanceFromPrevious,
|
|
});
|
|
|
|
if (isEnd)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var next = userPoints[segmentIndex + 1];
|
|
var nextGeo = new GeoPoint(next.Latitude, next.Longitude);
|
|
var intermediates = GeoUtils.CalculateIntermediatePoints(currentGeo, nextGeo, MaxPointSpacingMeters);
|
|
|
|
foreach (var intermediateGeo in intermediates)
|
|
{
|
|
var prev = allPoints[^1];
|
|
var prevGeo = new GeoPoint(prev.Latitude, prev.Longitude);
|
|
var distFromPrev = GeoUtils.CalculateDistance(prevGeo, intermediateGeo);
|
|
totalDistance += distFromPrev;
|
|
|
|
allPoints.Add(new RoutePointDto
|
|
{
|
|
Latitude = intermediateGeo.Lat,
|
|
Longitude = intermediateGeo.Lon,
|
|
PointType = "intermediate",
|
|
SequenceNumber = sequenceNumber++,
|
|
SegmentIndex = segmentIndex,
|
|
DistanceFromPrevious = distFromPrev,
|
|
});
|
|
}
|
|
}
|
|
|
|
return new RoutePointGraph(allPoints, totalDistance);
|
|
}
|
|
}
|
|
|
|
public record RoutePointGraph(IReadOnlyList<RoutePointDto> Points, double TotalDistanceMeters);
|