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
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user