From 118614b20e014f9d241a49d0f33bd4245d4948cf Mon Sep 17 00:00:00 2001 From: Frank Tovar Date: Thu, 5 Feb 2026 11:50:53 +0100 Subject: [PATCH] Unlink child suggestions and clear votes on delete --- Endpoints/SuggestEndpoints.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Endpoints/SuggestEndpoints.cs b/Endpoints/SuggestEndpoints.cs index fd45a24..db14d6e 100644 --- a/Endpoints/SuggestEndpoints.cs +++ b/Endpoints/SuggestEndpoints.cs @@ -111,6 +111,14 @@ public static class SuggestEndpoints if (suggestion == null) return Results.NotFound(new { error = "Suggestion not found." }); + // Break any links that pointed at this suggestion + await db.Suggestions + .Where(s => s.ParentSuggestionId == suggestion.Id) + .ExecuteUpdateAsync(s => s.SetProperty(x => x.ParentSuggestionId, (int?)null)); + + // Remove votes for this suggestion to avoid orphaned vote rows or FK errors + await db.Votes.Where(v => v.SuggestionId == suggestion.Id).ExecuteDeleteAsync(); + db.Suggestions.Remove(suggestion); await db.SaveChangesAsync(); return Results.NoContent();