Skip to content

Commit

Permalink
Fix Taxonomy Term Removal
Browse files Browse the repository at this point in the history
Fix #16537
  • Loading branch information
MikeAlhayek committed Aug 7, 2024
1 parent 166daf6 commit 3ea3a76
Showing 1 changed file with 38 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -311,18 +311,18 @@ public async Task<IActionResult> Delete(string taxonomyContentItemId, string tax
}

// Look for the target taxonomy item in the hierarchy.
var taxonomyItem = FindTaxonomyItem((JsonObject)taxonomy.As<TaxonomyPart>().Content, taxonomyItemId);
var content = (JsonObject)taxonomy.As<TaxonomyPart>().Content;

// Couldn't find targeted taxonomy item.
if (taxonomyItem == null)
RemoveTaxonomyItem(content, taxonomyItemId);

var updatedPart = content.ToObject<TaxonomyPart>();

if (updatedPart == null)
{
return NotFound();
}

taxonomy.Alter<TaxonomyPart>(part =>
{
part.Terms = part.Terms.Where(x => x.ContentItemId != taxonomyItemId).ToList();
});
taxonomy.Apply(updatedPart);

await _session.SaveAsync(taxonomy);

Expand Down Expand Up @@ -357,5 +357,36 @@ private static JsonObject FindTaxonomyItem(JsonObject contentItem, string taxono

return null;
}

private static bool RemoveTaxonomyItem(JsonObject contentItem, string taxonomyItemToRemove)
{
if (contentItem == null)
{
return false;
}

if (contentItem["ContentItemId"]?.Value<string>() == taxonomyItemToRemove)
{
return true;
}

if (!contentItem.TryGetPropertyValue("Terms", out var terms) || terms is not JsonArray taxonomyItems)
{
return false;
}

for (var i = taxonomyItems.Count - 1; i >= 0; i--)
{
var taxonomyItem = taxonomyItems[i] as JsonObject;
if (RemoveTaxonomyItem(taxonomyItem, taxonomyItemToRemove))
{
taxonomyItems.RemoveAt(i);

return false;
}
}

return false;
}
}
}

0 comments on commit 3ea3a76

Please sign in to comment.