From 1c52367388baf78d8a8edf52059688b50036f4b6 Mon Sep 17 00:00:00 2001 From: Dominic Burger Date: Wed, 4 Sep 2024 15:04:57 +0200 Subject: [PATCH] Require spatial extent for mandates --- .../Controllers/MandateController.cs | 7 +++-- src/Geopilot.Api/Models/Mandate.cs | 24 +++++++------- .../Controllers/MandateControllerTest.cs | 31 +++++++++++++++++++ 3 files changed, 48 insertions(+), 14 deletions(-) diff --git a/src/Geopilot.Api/Controllers/MandateController.cs b/src/Geopilot.Api/Controllers/MandateController.cs index c34f27ba..601fe36a 100644 --- a/src/Geopilot.Api/Controllers/MandateController.cs +++ b/src/Geopilot.Api/Controllers/MandateController.cs @@ -94,11 +94,13 @@ public async Task Create(Mandate mandate) if (mandate == null) return BadRequest(); + if (!mandate.SetPolygonFromCoordinates()) + return BadRequest("Invalid coordinates for spatial extent."); + var organisationIds = mandate.Organisations.Select(o => o.Id).ToList(); mandate.Organisations = await context.Organisations .Where(o => organisationIds.Contains(o.Id)) .ToListAsync(); - mandate.SetPolygonFromCoordinates(); var entityEntry = await context.AddAsync(mandate).ConfigureAwait(false); await context.SaveChangesAsync().ConfigureAwait(false); @@ -146,7 +148,8 @@ public async Task Edit(Mandate mandate) if (existingMandate == null) return NotFound(); - mandate.SetPolygonFromCoordinates(); + if (!mandate.SetPolygonFromCoordinates()) + return BadRequest("Invalid coordinates for spatial extent."); context.Entry(existingMandate).CurrentValues.SetValues(mandate); diff --git a/src/Geopilot.Api/Models/Mandate.cs b/src/Geopilot.Api/Models/Mandate.cs index 5704f559..733b7463 100644 --- a/src/Geopilot.Api/Models/Mandate.cs +++ b/src/Geopilot.Api/Models/Mandate.cs @@ -53,23 +53,23 @@ public class Mandate /// /// Transforms the list to a polygon. /// - public void SetPolygonFromCoordinates() + /// true if this mandate has 2 coordinates to create the spatial extent; otherwise, false. + public bool SetPolygonFromCoordinates() { if (Coordinates.Count != 2) { - SpatialExtent = Geometry.DefaultFactory.CreatePolygon(); + return false; } - else + + SpatialExtent = Geometry.DefaultFactory.CreatePolygon(new NetTopologySuite.Geometries.Coordinate[] { - SpatialExtent = Geometry.DefaultFactory.CreatePolygon(new NetTopologySuite.Geometries.Coordinate[] - { - new (Coordinates[0].X, Coordinates[0].Y), - new (Coordinates[0].X, Coordinates[1].Y), - new (Coordinates[1].X, Coordinates[1].Y), - new (Coordinates[1].X, Coordinates[0].Y), - new (Coordinates[0].X, Coordinates[0].Y), - }); - } + new (Coordinates[0].X, Coordinates[0].Y), + new (Coordinates[0].X, Coordinates[1].Y), + new (Coordinates[1].X, Coordinates[1].Y), + new (Coordinates[1].X, Coordinates[0].Y), + new (Coordinates[0].X, Coordinates[0].Y), + }); + return true; } /// diff --git a/tests/Geopilot.Api.Test/Controllers/MandateControllerTest.cs b/tests/Geopilot.Api.Test/Controllers/MandateControllerTest.cs index 93a4a86b..27fa660a 100644 --- a/tests/Geopilot.Api.Test/Controllers/MandateControllerTest.cs +++ b/tests/Geopilot.Api.Test/Controllers/MandateControllerTest.cs @@ -180,6 +180,21 @@ public async Task CreateMandate() CollectionAssert.AreEqual(mandate.Coordinates, resultValue.Coordinates); } + [TestMethod] + public async Task CreateMandateRequiresSpatialExtent() + { + mandateController.SetupTestUser(adminUser); + var mandate = new Mandate() + { + FileTypes = new string[] { ".*" }, + Name = "ACCORDIANWALK", + Organisations = new List() { new () { Id = 1 } }, + Coordinates = new List(), + }; + var result = await mandateController.Create(mandate); + ActionResultAssert.IsBadRequest(result); + } + [TestMethod] public async Task EditMandate() { @@ -240,6 +255,22 @@ public async Task EditMandate() } } + [TestMethod] + public async Task EditMandateRequiresSpatialExtent() + { + mandateController.SetupTestUser(adminUser); + var mandate = new Mandate() + { + Id = xtfMandate.Id, + FileTypes = new string[] { ".*", ".zip" }, + Name = "PEARLFOLLOWER", + Organisations = new List() { new () { Id = 1 } }, + Coordinates = new List(), + }; + var result = await mandateController.Edit(mandate); + ActionResultAssert.IsBadRequest(result); + } + [TestCleanup] public void Cleanup() {