From edc0a11adf6fe206c3e029eccb1a45b4c59be6db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20L=C3=BCthi?= Date: Tue, 7 Jan 2025 14:50:44 +0100 Subject: [PATCH 1/7] Revert nullable model types to match ContextModelSnapshot --- src/Geopilot.Api/Models/Asset.cs | 2 +- src/Geopilot.Api/Models/Delivery.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Geopilot.Api/Models/Asset.cs b/src/Geopilot.Api/Models/Asset.cs index cd157073..d5c1d9ae 100644 --- a/src/Geopilot.Api/Models/Asset.cs +++ b/src/Geopilot.Api/Models/Asset.cs @@ -38,7 +38,7 @@ public class Asset /// /// Backreference to the delivery the asset belongs to. /// - public Delivery? Delivery { get; set; } = new Delivery(); + public Delivery Delivery { get; set; } = new Delivery(); /// /// The deletion status of the asset. diff --git a/src/Geopilot.Api/Models/Delivery.cs b/src/Geopilot.Api/Models/Delivery.cs index f53fb189..742401d7 100644 --- a/src/Geopilot.Api/Models/Delivery.cs +++ b/src/Geopilot.Api/Models/Delivery.cs @@ -28,7 +28,7 @@ public class Delivery /// /// The mandate the delivery fulfills. /// - public Mandate? Mandate { get; set; } = new Mandate(); + public Mandate Mandate { get; set; } = new Mandate(); /// /// Assets delivered or created by the validation and delivery process. From 3ef9e040e9de1306c54af4d7bb17ea882c8e16a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20L=C3=BCthi?= Date: Tue, 7 Jan 2025 15:00:47 +0100 Subject: [PATCH 2/7] Reenable StrictMode on SeedMandates --- src/Geopilot.Api/ContextExtensions.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Geopilot.Api/ContextExtensions.cs b/src/Geopilot.Api/ContextExtensions.cs index 612645c7..5d435b6d 100644 --- a/src/Geopilot.Api/ContextExtensions.cs +++ b/src/Geopilot.Api/ContextExtensions.cs @@ -114,10 +114,12 @@ public static void SeedMandates(this Context context) var knownFileFormats = new string[] { ".xtf", ".gpkg", ".*", ".itf", ".xml", ".zip", ".csv" }; var mandateFaker = new Faker() .UseDateTimeReference(referenceDateTime) + .StrictMode(true) .RuleFor(o => o.Id, f => 0) .RuleFor(o => o.Name, f => f.Commerce.ProductName()) .RuleFor(o => o.FileTypes, f => f.PickRandom(knownFileFormats, 4).Distinct().ToArray()) .RuleFor(o => o.SpatialExtent, f => GetExtent()) + .Ignore(o => o.Coordinates) .RuleFor(o => o.Organisations, f => f.PickRandom(context.Organisations.ToList(), 1).ToList()) .RuleFor(o => o.Deliveries, _ => new List()); From 5d49a6dac5a96dfafd87cc02a38b887f697ac6ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20L=C3=BCthi?= Date: Wed, 8 Jan 2025 11:09:20 +0100 Subject: [PATCH 3/7] Initialize reference attributes with null! Conform to https://learn.microsoft.com/en-us/ef/core/modeling/relationships/one-to-many --- src/Geopilot.Api/Models/Asset.cs | 2 +- src/Geopilot.Api/Models/Delivery.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Geopilot.Api/Models/Asset.cs b/src/Geopilot.Api/Models/Asset.cs index d5c1d9ae..e5130f42 100644 --- a/src/Geopilot.Api/Models/Asset.cs +++ b/src/Geopilot.Api/Models/Asset.cs @@ -38,7 +38,7 @@ public class Asset /// /// Backreference to the delivery the asset belongs to. /// - public Delivery Delivery { get; set; } = new Delivery(); + public Delivery Delivery { get; set; } = null!; /// /// The deletion status of the asset. diff --git a/src/Geopilot.Api/Models/Delivery.cs b/src/Geopilot.Api/Models/Delivery.cs index 742401d7..1ef33617 100644 --- a/src/Geopilot.Api/Models/Delivery.cs +++ b/src/Geopilot.Api/Models/Delivery.cs @@ -23,12 +23,12 @@ public class Delivery /// /// The user that declared the delivery. /// - public User DeclaringUser { get; set; } = new User(); + public User DeclaringUser { get; set; } = null!; /// /// The mandate the delivery fulfills. /// - public Mandate Mandate { get; set; } = new Mandate(); + public Mandate Mandate { get; set; } = null!; /// /// Assets delivered or created by the validation and delivery process. From 42deeb1ed1057e54f868f0e0fce3c0fcdfa0251f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20L=C3=BCthi?= Date: Wed, 8 Jan 2025 11:10:19 +0100 Subject: [PATCH 4/7] Disconnect deliveries on update --- src/Geopilot.Frontend/src/pages/admin/mandates.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Geopilot.Frontend/src/pages/admin/mandates.tsx b/src/Geopilot.Frontend/src/pages/admin/mandates.tsx index 0be48936..02ab8ce8 100644 --- a/src/Geopilot.Frontend/src/pages/admin/mandates.tsx +++ b/src/Geopilot.Frontend/src/pages/admin/mandates.tsx @@ -43,6 +43,7 @@ export const Mandates = () => { }, [fetchApi]); async function saveMandate(mandate: Mandate) { + mandate.deliveries = []; mandate.organisations = mandate.organisations?.map(value => typeof value === "number" ? ({ id: value } as Organisation) From b23fa2009e3f8188e452e1146189fbd7c228104e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20L=C3=BCthi?= Date: Wed, 8 Jan 2025 11:14:35 +0100 Subject: [PATCH 5/7] Define matching query filter for Asset Fixes Microsoft.EntityFrameworkCore.Model.Validation[10622] --- src/Geopilot.Api/Context.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Geopilot.Api/Context.cs b/src/Geopilot.Api/Context.cs index 1b266332..c9936e86 100644 --- a/src/Geopilot.Api/Context.cs +++ b/src/Geopilot.Api/Context.cs @@ -104,5 +104,8 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) ArgumentNullException.ThrowIfNull(modelBuilder); modelBuilder.Entity().HasQueryFilter(d => !d.Deleted); + modelBuilder.Entity() + .HasQueryFilter(a => !a.Delivery.Deleted) + .HasQueryFilter(a => !a.Deleted); } } From e75b151966e00aac4a8555ecc381a40173cdd290 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20L=C3=BCthi?= Date: Wed, 8 Jan 2025 13:36:18 +0100 Subject: [PATCH 6/7] Satisfy insert constraints for delivery (Tests) --- tests/Geopilot.Api.Test/Controllers/DeliveryControllerTest.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Geopilot.Api.Test/Controllers/DeliveryControllerTest.cs b/tests/Geopilot.Api.Test/Controllers/DeliveryControllerTest.cs index d1c38019..9727dc35 100644 --- a/tests/Geopilot.Api.Test/Controllers/DeliveryControllerTest.cs +++ b/tests/Geopilot.Api.Test/Controllers/DeliveryControllerTest.cs @@ -165,7 +165,7 @@ public async Task Create(bool setOptionals) public void Delete() { var guid = Guid.NewGuid(); - var delivery = new Delivery { JobId = guid }; + var delivery = new Delivery { JobId = guid, Mandate = context.Mandates.First(), DeclaringUser = context.Users.First() }; delivery.Assets.Add(new Asset()); context.Deliveries.Add(delivery); context.SaveChanges(); @@ -196,7 +196,7 @@ public async Task Download() { assetHandlerMock.Setup(p => p.DownloadAssetAsync(It.IsAny(), It.IsAny())).ReturnsAsync((Encoding.UTF8.GetBytes("Test"), "text/xml")); var guid = Guid.NewGuid(); - var delivery = new Delivery { JobId = guid }; + var delivery = new Delivery { JobId = guid, Mandate = context.Mandates.First(), DeclaringUser = context.Users.First() }; delivery.Assets.Add(new Asset() { OriginalFilename = "Test.xml", SanitizedFilename = "xyz.xml" }); context.Deliveries.Add(delivery); context.SaveChanges(); From 7d4e30fb84a2ae71a29fea1077f58f552582e5dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20L=C3=BCthi?= Date: Wed, 8 Jan 2025 13:38:44 +0100 Subject: [PATCH 7/7] Avoid DeclaringUser null reference for STAC item generation --- src/Geopilot.Api/Context.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Geopilot.Api/Context.cs b/src/Geopilot.Api/Context.cs index c9936e86..babed51f 100644 --- a/src/Geopilot.Api/Context.cs +++ b/src/Geopilot.Api/Context.cs @@ -89,6 +89,8 @@ public IQueryable MandatesWithIncludes .Include(m => m.Organisations) .ThenInclude(o => o.Users) .Include(m => m.Deliveries) + .ThenInclude(d => d.DeclaringUser) + .Include(m => m.Deliveries) .ThenInclude(d => d.Assets); } }