using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Data.SqlClient; using PlotLine.Data; using PlotLine.Services; namespace PlotLine.Controllers; [Authorize] public sealed class GeographyController(GeographyRepository repository,IProjectAccessService access,ICurrentUserService currentUser,GeographicRules rules,ILocationRepository locations) : Controller { [HttpGet] public async Task State(int projectId,int? locationId) { if(!await access.CanAccessProjectAsync(projectId)) return NotFound(); Response.Headers.CacheControl="private, no-store"; var states=await repository.GetAsync(projectId,currentUser.UserId!.Value,locationId); if(states.Any(s=>s.Status=="Pending")) { var projectLocations=(await locations.ListByProjectAsync(projectId)).ToDictionary(l=>l.LocationID); foreach(var state in states.Where(s=>s.Status=="Pending")) { var chain=new List(); var visited=new HashSet();var id=state.LocationID; while(projectLocations.TryGetValue(id,out var place)&&visited.Add(id)&&chain.Count<=100) { chain.Add(place); if(place.ParentLocationID is not int parent)break; id=parent; } if(rules.Query(chain) is null) { state.Status="Not eligible"; state.Message="Use geographical types beneath a recognised country or qualified region."; } } } return Json(states); } [HttpPost,ValidateAntiForgeryToken] public async Task Confirm(int projectId,int locationId,long generation,int candidateIndex) { if(!await access.CanAccessProjectAsync(projectId)) return NotFound(); if(!ModelState.IsValid || candidateIndex is <0 or >9) return BadRequest(); try { await repository.ChooseAsync(projectId,currentUser.UserId!.Value,locationId,generation,candidateIndex); return Json(new {ok=true}); } catch(SqlException ex) when(ex.Number==51000) { return Conflict(new {message=ex.Message}); } } [HttpPost,ValidateAntiForgeryToken] public async Task Retry(int projectId,int locationId,long generation,bool replaceConfirmed=false) { if(!await access.CanAccessProjectAsync(projectId)) return NotFound(); if(!ModelState.IsValid) return BadRequest(); try { await repository.RetryAsync(projectId,currentUser.UserId!.Value,locationId,generation,replaceConfirmed); return Json(new {ok=true}); } catch(SqlException ex) when(ex.Number==51000) { return Conflict(new {message=ex.Message}); } } [HttpPost,ValidateAntiForgeryToken] public async Task Exclude(int projectId,int locationId,long generation,bool isExcluded) { if(!await access.CanAccessProjectAsync(projectId)) return NotFound(); if(!ModelState.IsValid) return BadRequest(); try { await repository.ExcludeAsync(projectId,currentUser.UserId!.Value,locationId,generation,isExcluded); return Json(new {ok=true}); } catch(SqlException ex) when(ex.Number==51000) { return Conflict(new {message=ex.Message}); } } [HttpPost,ValidateAntiForgeryToken] public async Task Hierarchy(int projectId,int locationId,[FromServices] GeographicHierarchyService hierarchy) { if(!await access.CanAccessProjectAsync(projectId)) return NotFound(); if(!ModelState.IsValid) return BadRequest(); return Json(new {message=await hierarchy.SubmitAsync(projectId,locationId)}); } }