refact program.cs vineyardController, service e repo
This commit is contained in:
1 parent
a373201ca4
commit
3a1a0ca7ef
17 files changed
+769
-306
No files matched your search
@@ -1,15 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using uWine.Core.Domain.Entities;
|
||||
using NetTopologySuite.Geometries;
|
||||
using NetTopologySuite.IO;
|
||||
|
||||
namespace uWine.Core.Infrastructure.Helpers
|
||||
{
|
||||
public class CoordinatesHelper
|
||||
{
|
||||
public static string ToWktPolygon(Dictionary<int, Coordinates> coordinatesDict)
|
||||
public static string ToWktPolygon(Dictionary<int, Domain.Entities.Coordinates> coordinatesDict)
|
||||
{
|
||||
if (coordinatesDict == null || !coordinatesDict.Any())
|
||||
{
|
||||
@@ -31,5 +27,42 @@ namespace uWine.Core.Infrastructure.Helpers
|
||||
|
||||
return $"POLYGON(({closedPoints}))";
|
||||
}
|
||||
|
||||
public static Dictionary<int, Domain.Entities.Coordinates> WktPolygonToDictionary(string wktPolygon)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(wktPolygon))
|
||||
{
|
||||
throw new ArgumentException("La stringa WKT del poligono non può essere nulla o vuota.");
|
||||
}
|
||||
|
||||
var reader = new WKTReader();
|
||||
NetTopologySuite.Geometries.Geometry geometry = reader.Read(wktPolygon);
|
||||
|
||||
if (!(geometry is Polygon polygon))
|
||||
{
|
||||
throw new ArgumentException("La stringa WKT non rappresenta un poligono valido.");
|
||||
}
|
||||
|
||||
var coordinatesDict = new Dictionary<int, Domain.Entities.Coordinates>();
|
||||
var points = polygon.ExteriorRing.Coordinates; // Ottieni i punti dell'anello esterno
|
||||
|
||||
// Nota: i poligoni WKT ripetono il primo punto alla fine per chiudere.
|
||||
// Dobbiamo escludere l'ultimo punto se è identico al primo per non duplicarlo nel dictionary,
|
||||
// a meno che tu non voglia mantenere anche il punto di chiusura.
|
||||
int count = points.Length;
|
||||
if (count > 0 && points[0].Equals2D(points[count - 1]))
|
||||
{
|
||||
count--; // Escludi l'ultimo punto se è una duplicazione del primo
|
||||
}
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
// Ricorda che NetTopologySuite usa (X, Y) che corrispondono a (Longitude, Latitude) per WGS84
|
||||
coordinatesDict.Add(i, new Domain.Entities.Coordinates(points[i].Y, points[i].X ));
|
||||
}
|
||||
|
||||
return coordinatesDict;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -70,6 +70,18 @@ namespace uWine.Core.Infrastructure.Repository
|
||||
}
|
||||
}
|
||||
|
||||
public string Area(int vineyardId)
|
||||
{
|
||||
using (var connection = Connection())
|
||||
{
|
||||
connection.Open();
|
||||
var command = new MySqlCommand("SELECT ST_AsText(area) FROM t_vineyard WHERE id = @id", connection);
|
||||
command.Parameters.AddWithValue("@id", vineyardId);
|
||||
var result = command.ExecuteScalar();
|
||||
return result?.ToString() ?? string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
public List<Vineyard> GetOwnerVineyards(int ownerId)
|
||||
{
|
||||
var vineyards = new List<Vineyard>();
|
||||
@@ -92,5 +104,29 @@ namespace uWine.Core.Infrastructure.Repository
|
||||
}
|
||||
return vineyards;
|
||||
}
|
||||
|
||||
public List<Vineyard> GetUserVineyards(int userId)
|
||||
{
|
||||
var vineyards = new List<Vineyard>();
|
||||
using (var connection = Connection())
|
||||
{
|
||||
connection.Open();
|
||||
var command = new MySqlCommand("SELECT v.id, v.name, v.location, v.hectares, v.latitude, v.longitude " +
|
||||
"FROM t_vineyard v " +
|
||||
"JOIN t_owners_vineyards ov ON v.id = ov.vineyard_id " +
|
||||
"JOUN t_user_owner uo on uo.owner_id = ov.owner_id" +
|
||||
"WHERE uo.user_id = @userId", connection);
|
||||
command.Parameters.AddWithValue("@userId", userId);
|
||||
|
||||
using (var reader = command.ExecuteReader())
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
vineyards.Add(Vineyard.FromRecord(reader));
|
||||
}
|
||||
}
|
||||
}
|
||||
return vineyards;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,15 +4,17 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using uWine.Core.Domain.Entities;
|
||||
using uWine.Core.Domain.Repositories;
|
||||
using uWine.Core.Infrastructure.Helpers;
|
||||
using uWine.Core.Infrastructure.Repository;
|
||||
|
||||
namespace uWine.Core.Infrastructure.Services
|
||||
{
|
||||
public class CoordinatesService
|
||||
{
|
||||
public MariaDbVineyardRepository _vineyardRepository;
|
||||
public IVineyardRepository _vineyardRepository;
|
||||
|
||||
public CoordinatesService(MariaDbVineyardRepository vineyardRepository)
|
||||
public CoordinatesService(IVineyardRepository vineyardRepository)
|
||||
{
|
||||
_vineyardRepository = vineyardRepository;
|
||||
}
|
||||
@@ -28,14 +30,21 @@ namespace uWine.Core.Infrastructure.Services
|
||||
// return new Coordinates(vineyard.Latitude, vineyard.Longitude);
|
||||
//}
|
||||
|
||||
public async Task<Dictionary<int,Coordinates>> VineyardCoordinates(int vineyardId)
|
||||
{
|
||||
|
||||
}
|
||||
//public Dictionary<int, Coordinates> VineyardCoordinates(int vineyardId)
|
||||
//{
|
||||
// return new Dictionary<int, Coordinates>();
|
||||
//}
|
||||
|
||||
public void SaveArea(int vineyardId, Dictionary<int, Coordinates> coordinates)
|
||||
{
|
||||
_vineyardRepository.InsertArea(coordinates, vineyardId);
|
||||
}
|
||||
|
||||
public Dictionary<int, Coordinates> GetArea(int vineyardId)
|
||||
{
|
||||
var dict = new Dictionary<int, Coordinates>();
|
||||
dict = CoordinatesHelper.WktPolygonToDictionary(_vineyardRepository.Area(vineyardId));
|
||||
return dict;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using uWine.Core.Domain.Entities;
|
||||
using uWine.Core.Domain.Repositories;
|
||||
|
||||
namespace uWine.Core.Infrastructure.Services
|
||||
{
|
||||
public class VineyardService
|
||||
{
|
||||
public IVineyardRepository _vineyardRepository;
|
||||
public CoordinatesService _coordinatesService;
|
||||
public VineyardService(IVineyardRepository vineyardRepository, CoordinatesService coordinatesService)
|
||||
{
|
||||
_vineyardRepository = vineyardRepository;
|
||||
_coordinatesService = coordinatesService;
|
||||
}
|
||||
|
||||
public void Insert(Vineyard vineyard, int ownerId)
|
||||
{
|
||||
if (vineyard == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(vineyard), "Vineyard cannot be null");
|
||||
}
|
||||
|
||||
_vineyardRepository.Insert(vineyard, ownerId);
|
||||
|
||||
if (vineyard.Area != null || vineyard.Area.Count >0)
|
||||
{
|
||||
_coordinatesService.SaveArea(vineyard.Id, vineyard.Area);
|
||||
}
|
||||
}
|
||||
|
||||
public List<Vineyard> User(int userId)
|
||||
{
|
||||
if (userId <= 0)
|
||||
{
|
||||
throw new ArgumentException("User ID must be greater than zero.", nameof(userId));
|
||||
}
|
||||
|
||||
var vineyards = _vineyardRepository.GetUserVineyards(userId);
|
||||
if (vineyards == null || !vineyards.Any())
|
||||
{
|
||||
return new List<Vineyard>();
|
||||
}
|
||||
|
||||
foreach (var vineyard in vineyards)
|
||||
{
|
||||
vineyard.Area = _coordinatesService.GetArea(vineyard.Id);
|
||||
}
|
||||
|
||||
return vineyards;
|
||||
}
|
||||
|
||||
public List<Vineyard> Owner(int ownerId)
|
||||
{
|
||||
if (ownerId <= 0)
|
||||
{
|
||||
throw new ArgumentException("Owner ID must be greater than zero.", nameof(ownerId));
|
||||
}
|
||||
|
||||
var vineyards = _vineyardRepository.GetOwnerVineyards(ownerId);
|
||||
if (vineyards == null || !vineyards.Any())
|
||||
{
|
||||
return new List<Vineyard>();
|
||||
}
|
||||
|
||||
foreach (var vineyard in vineyards)
|
||||
{
|
||||
vineyard.Area = _coordinatesService.GetArea(vineyard.Id);
|
||||
}
|
||||
|
||||
return vineyards;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.6" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="9.0.6" />
|
||||
<PackageReference Include="MySqlConnector" Version="2.4.0" />
|
||||
<PackageReference Include="NetTopologySuite" Version="2.6.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
Reference in new issue
Block a user