Application: Add StreetService
This commit is contained in:
parent
db51476d4d
commit
2a68fa8257
81
src/Application/StreetService.cs
Normal file
81
src/Application/StreetService.cs
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
using System.Data;
|
||||||
|
using NetTopologySuite.Geometries;
|
||||||
|
|
||||||
|
public class StreetService
|
||||||
|
{
|
||||||
|
private readonly StreetRepository repository;
|
||||||
|
|
||||||
|
public StreetService(StreetRepository repository)
|
||||||
|
{
|
||||||
|
this.repository = repository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Point ToGeometryPoint(CoordinateDTO coordinate)
|
||||||
|
{
|
||||||
|
return new Point(coordinate.X, coordinate.Y);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static LineString ToGeometry(List<CoordinateDTO> coordinates)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (coordinates == null || coordinates.Count() < 2)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Street must contain at least two points.");
|
||||||
|
}
|
||||||
|
|
||||||
|
var geometryFactory = new GeometryFactory();
|
||||||
|
|
||||||
|
return geometryFactory.CreateLineString(coordinates.Select(g => new Coordinate(g.X, g.Y)).ToArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Street> CreateStreetAsync(Street street)
|
||||||
|
{
|
||||||
|
if (await repository.ExistsAsync(street.Name))
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException($"Street '{street.Name}' already exists.");
|
||||||
|
}
|
||||||
|
|
||||||
|
await repository.AddAsync(street);
|
||||||
|
return street;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<bool> DeleteStreetAsync(string name)
|
||||||
|
{
|
||||||
|
if (!await repository.ExistsAsync(name))
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException($"Street '{name}' does not exist.");
|
||||||
|
}
|
||||||
|
return await repository.RemoveAsync(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<GetStreetDTO> GetStreetAsync(string name)
|
||||||
|
{
|
||||||
|
var street = await repository.GetByNameAsync(name);
|
||||||
|
if (street == null)
|
||||||
|
{
|
||||||
|
throw new KeyNotFoundException($"Street '{name}' not found.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return new GetStreetDTO { Name = street.Name, Capacity = street.Capacity, Geometry = street.Geometry.Coordinates.Select(c => new CoordinateDTO { X = (int)c.X, Y = (int)c.Y }).ToList() };
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task AddPointAsync(string name, Point point, bool usePostGIS)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (usePostGIS)
|
||||||
|
{
|
||||||
|
await repository.UpdateGeometryWithPostGIS(name, point);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
await repository.UpdateGeometryInBackend(name, point);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user