Unlink child suggestions and clear votes on delete

This commit is contained in:
2026-02-05 11:50:53 +01:00
parent 56a3f185e0
commit 118614b20e

View File

@@ -111,6 +111,14 @@ public static class SuggestEndpoints
if (suggestion == null) if (suggestion == null)
return Results.NotFound(new { error = "Suggestion not found." }); 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); db.Suggestions.Remove(suggestion);
await db.SaveChangesAsync(); await db.SaveChangesAsync();
return Results.NoContent(); return Results.NoContent();