public bool DeleteCommentById(int id) { var comment = context.comments.SingleOrDefault(c => c.id == id); if (comment != null) { context.Remove(comment); return(context.SaveChanges() > 0); } return(false); }
public async Task <ActionResult <Models.Comment> > DeleteComment(long id) { var comment = await _context.GetComment(id); if (comment == null) { return(NotFound()); } await _context.Remove(id); await _context.SaveChangesAsync(); return(comment); }
public async Task <ActionResult> Delete(string collectionId) { Collection collection = _collectionContext.Collections.Where(o => o.Id == collectionId).SingleOrDefault(); string userName = collection.UserName; User user = await _userManager.FindByNameAsync(userName); _collectionContext.Collections.Remove(collection); await _collectionContext.SaveChangesAsync(); var items = _itemContext.Items.ToList(); var comments = _commentContext.Comments.ToList(); var likes = _likeContext.Likes.ToList(); foreach (var item in items) { foreach (var comment in comments) { if (comment.ItemId == item.Id) { user.nComments--; _commentContext.Remove(comment); } } foreach (var like in likes) { if (like.ItemId == item.Id) { user.nLikes--; _likeContext.Remove(like); } } if (item.CollectionId == collectionId) { _itemContext.Remove(item); user.nItems--; } } user.nCollections--; await _userManager.UpdateAsync(user); await _likeContext.SaveChangesAsync(); await _commentContext.SaveChangesAsync(); await _itemContext.SaveChangesAsync(); return(RedirectToAction("Index", "Profile", new { userName = userName })); }