public async Task <IActionResult> DeleteConfirmed(int id) { var faq = await _context.Faqs .Include(f => f.FaqTags) .ThenInclude(ft => ft.Tag) .Include(f => f.Category) .ThenInclude(c => c.Faqs) .SingleOrDefaultAsync(f => f.Id == id); if (faq.IsNull()) { return(NotFound()); } _context.Remove(faq); // If the category has no more related entities then delete the category as well if (await CategoryHelpers.HasNoRelatedEntities(faq.Category, _context, ignore: faq)) { _context.Remove(faq.Category); } // If the tags has no more related entities then delete the category as well foreach (var tag in faq.FaqTags.Select(ft => ft.Tag)) { if (await TagHelpers.HasNoRelatedEntities(tag, _context, ignore: faq.FaqTags)) { _context.Remove(tag); } } await _context.SaveChangesAsync(); return(RedirectToAction("Index")); }
public IActionResult Delete(int id) { try { var username = HttpContext.User.GetUsername(); var isAdmin = HttpContext.User.IsAdmin(); var contextEntity = _intranetApiContext.News.Find(id); if (contextEntity == null) { return(NotFound()); } if (contextEntity.UserId?.Equals(username) != true && !isAdmin) { return(Forbid()); } _intranetApiContext.Remove(contextEntity); _intranetApiContext.SaveChanges(); return(Ok()); } catch (Exception) { return(StatusCode(StatusCodes.Status500InternalServerError)); } }
public async Task <IActionResult> DeleteConfirmed(int id) { var policy = await _context.Policies .Include(p => p.PolicyTags) .ThenInclude(pt => pt.Tag) .Include(p => p.Category) .ThenInclude(c => c.Policies) .SingleOrDefaultAsync(p => p.Id == id); if (policy.IsNull()) { return(NotFound()); } // TODO: Delete blob as well _context.Remove(policy); // If the category has no more related entities then delete the category as well if (await CategoryHelpers.HasNoRelatedEntities(policy.Category, _context, ignore: policy)) { _context.Remove(policy.Category); } // If the tags has no more related entities then delete the tag as well foreach (var tag in policy.PolicyTags.Select(ft => ft.Tag)) { if (await TagHelpers.HasNoRelatedEntities(tag, _context, ignore: policy.PolicyTags)) { _context.Remove(tag); } } await _context.SaveChangesAsync(); return(RedirectToAction("Index")); }
public async Task <IActionResult> DeleteConfirmed(int id, [FromQuery] string from = null) { var category = await _context.Categories .Include(m => m.Faqs) .SingleOrDefaultAsync(f => f.Id == id); if (category.IsNull()) { return(NotFound()); } if (category.Faqs.Any()) { ModelState.AddModelError("Error", "Must remove all FAQ's and policies first!"); return(View(category)); } _context.Remove(category); await _context.SaveChangesAsync(); return(RedirectToAction("Index", new { from })); }