PlotDirector/PlotLine/Services/GeographicHierarchyService.cs

19 lines
1.2 KiB
C#

using Microsoft.Data.SqlClient;
using PlotLine.Data;
namespace PlotLine.Services;
public sealed class GeographicHierarchyService(GeographyRepository repository,GeographicRules rules,IProjectAccessService access,ICurrentUserService currentUser)
{
public async Task<string> SubmitAsync(int projectId,int locationId)
{
if(!await access.CanAccessProjectAsync(projectId))return "Project access is required.";
var chain=await repository.ChainAsync(locationId);
if(chain.Count==0 || chain[0].ProjectID!=projectId)return "Location is unavailable.";
if(rules.Query(chain) is null)return "This hierarchy is not eligible for geographical lookup. Use geographical types beneath a recognised root.";
var state=(await repository.GetAsync(projectId,currentUser.UserId!.Value,locationId)).SingleOrDefault();
if(state is null)return "Geographical state is unavailable.";
try {await repository.SubmitAsync(projectId,currentUser.UserId!.Value,locationId,state.Generation);return "Geographical hierarchy lookup requested (at most 20 places). External processing requires the configured provider to be enabled.";}
catch(SqlException ex) when(ex.Number==51000){return ex.Message;}
}
}