//Create public async Task <NotificationResponse> CreateNotification(CreateNotificationRequest model) { var oldNotification = await _context.Notifications.Where(n => n.NotificationType == model.NotificationType && n.ActionOwnerId == model.ActionOwnerId && n.ReiceiverId == model.ReiceiverId).ToListAsync(); _context.RemoveRange(oldNotification); var notification = _mapper.Map <Notification>(model); if (notification == null) { throw new AppException("Create Notification failed"); } _context.Notifications.Add(notification); await _context.SaveChangesAsync(); var notificationResponse = _mapper.Map <NotificationResponse>(notification); var actionOwner = await _accountService.GetById(notificationResponse.ActionOwnerId); bool IsActionOwnerNameNull = actionOwner.Name == null; notificationResponse.ActionOwnerName = IsActionOwnerNameNull ? "" : actionOwner.Name; notificationResponse.ActionOwnerAvatarPath = actionOwner.AvatarPath; var receiver = await _accountService.GetById(notificationResponse.ReiceiverId); bool IsReceiverNull = receiver.Name == null; notificationResponse.ReiceiverName = IsReceiverNull ? "" : receiver.Name; return(notificationResponse); }
//Delete public async Task DeletePost(int id) { var post = await GetPost(id); _context.RemoveRange(GetComments(id)); _context.RemoveRange(GetReactions(id)); var reports = await _context.Reports.Where(report => report.TargetType == ReportTarget.Post && report.TargetId == id).ToListAsync(); _context.RemoveRange(reports); var notifications = await _context.Notifications.Where(n => n.PostId == id).ToListAsync(); _context.RemoveRange(notifications); _context.Remove(post); await _context.SaveChangesAsync(); }
//Delete public async Task DeleteComment(int id) { var comment = await GetComment(id); var childComments = await _context.Comments.Where(c => c.ParrentId == id).ToListAsync(); foreach (var childComment in childComments) { var childNotifications = await _context.Notifications.Where(noti => noti.NotificationType == NotificationType.Commented && noti.PostId == childComment.PostId).ToListAsync(); _context.RemoveRange(childNotifications); var childReactions = await _context.Reactions.Where(reaction => reaction.Target == ReactionTarget.Comment && reaction.TargetId == childComment.Id).ToListAsync(); _context.RemoveRange(childReactions); var childReports = await _context.Reports.Where(report => report.TargetType == ReportTarget.Comment && report.TargetId == childComment.Id).ToListAsync(); _context.RemoveRange(childReports); } _context.RemoveRange(childComments); //notification var notification = await _context.Notifications.Where(n => n.NotificationType == NotificationType.Commented && n.PostId == comment.PostId).ToListAsync(); _context.RemoveRange(notification); //reaction var reactions = await _context.Reactions.Where(n => n.Target == ReactionTarget.Comment && n.TargetId == id).ToListAsync(); _context.RemoveRange(reactions); //report var reports = await _context.Reports.Where(report => report.TargetType == ReportTarget.Comment && report.TargetId == id).ToListAsync(); _context.RemoveRange(reports); _context.Remove(comment); await _context.SaveChangesAsync(); }
public async Task Delete(int id) { var account = await getAccount(id); var posts = await _context.Posts.Where(p => p.OwnerId == id).ToListAsync(); //post foreach (var post in posts) { var postComments = await _context.Comments.Where(c => c.PostId == post.Id).ToListAsync(); _context.RemoveRange(postComments); var postReactions = await _context.Reactions.Where(r => r.Target == ReactionTarget.Post && r.TargetId == post.Id).ToListAsync(); _context.RemoveRange(postReactions); var postReports = await _context.Reports.Where(report => report.TargetType == ReportTarget.Post && report.TargetId == post.Id).ToListAsync(); _context.RemoveRange(postReports); var postNotifications = await _context.Notifications.Where(n => n.PostId == post.Id).ToListAsync(); _context.RemoveRange(postNotifications); } _context.RemoveRange(posts); //comment var comments = await _context.Comments.Where(c => c.OwnerId == id).ToListAsync(); foreach (var comment in comments) { var childComments = await _context.Comments.Where(c => c.ParrentId == comment.Id).ToListAsync(); foreach (var childComment in childComments) { var childNotifications = await _context.Notifications.Where(noti => noti.NotificationType == NotificationType.Commented && noti.PostId == childComment.PostId).ToListAsync(); _context.RemoveRange(childNotifications); var childReactions = await _context.Reactions.Where(reaction => reaction.Target == ReactionTarget.Comment && reaction.TargetId == childComment.Id).ToListAsync(); _context.RemoveRange(childReactions); var childReports = await _context.Reports.Where(report => report.TargetType == ReportTarget.Comment && report.TargetId == childComment.Id).ToListAsync(); _context.RemoveRange(childReports); } } _context.RemoveRange(comments); //reaction var reactions = await _context.Reactions.Where(c => c.OwnerId == id).ToListAsync(); _context.RemoveRange(reactions); //folow var follows = await _context.Follows.Where(f => f.SubjectId == id || f.FollowerId == id).ToListAsync(); _context.RemoveRange(follows); //notification var notifications = await _context.Notifications.Where(n => n.ActionOwnerId == id).ToListAsync(); _context.RemoveRange(notifications); //Message var messages = await _context.Messages.Where(m => m.SenderId == id || m.RecipientId == id).ToListAsync(); _context.RemoveRange(messages); //report var reports = await _context.Reports.Where(r => r.TargetType == ReportTarget.User && r.TargetId == id).ToListAsync(); _context.RemoveRange(reports); _context.Users.Remove(account); await _context.SaveChangesAsync(); //await _userManager.DeleteAsync(account); }