Handler.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using Application.Abstractions.Data;
  2. using Application.Abstractions.Messaging;
  3. using Microsoft.EntityFrameworkCore;
  4. namespace Application.Features.Admin.Forum.Trash.Comment.Restore;
  5. public sealed class Handler(IAppDbContext db) : ICommandHandler<Command>
  6. {
  7. public async Task Handle(Command request, CancellationToken ct)
  8. {
  9. if (request.IDs is null || request.IDs.Length == 0)
  10. {
  11. return;
  12. }
  13. var comments = await db.Comment.Where(c => request.IDs.Contains(c.ID) && c.IsDeleted).ToListAsync(ct);
  14. foreach (var comment in comments)
  15. {
  16. comment.IsDeleted = false;
  17. comment.DeletedAt = null;
  18. comment.UpdatedAt = DateTime.UtcNow;
  19. }
  20. // Post 댓글 카운트 복원
  21. var postCommentCounts = comments.GroupBy(c => c.PostID).ToDictionary(g => g.Key, g => g.Count());
  22. var posts = await db.Post.Where(p => postCommentCounts.Keys.Contains(p.ID)).ToListAsync(ct);
  23. foreach (var post in posts)
  24. {
  25. post.Comments += postCommentCounts[post.ID];
  26. post.UpdatedAt = DateTime.UtcNow;
  27. }
  28. // Board 댓글 카운트 복원
  29. var boardCommentCounts = comments.GroupBy(c => c.BoardID).ToDictionary(g => g.Key, g => g.Count());
  30. var boards = await db.Board.Where(b => boardCommentCounts.Keys.Contains(b.ID)).ToListAsync(ct);
  31. foreach (var board in boards)
  32. {
  33. board.Comments += boardCommentCounts[board.ID];
  34. board.UpdatedAt = DateTime.UtcNow;
  35. }
  36. // MemberStats 댓글 수 복원
  37. var memberCommentCounts = comments.GroupBy(c => c.MemberID).ToDictionary(g => g.Key, g => g.Count());
  38. var memberStats = await db.MemberStats.Where(x => memberCommentCounts.Keys.Contains(x.MemberID)).ToListAsync(ct);
  39. foreach (var stats in memberStats)
  40. {
  41. stats.CommentCount += memberCommentCounts[stats.MemberID];
  42. }
  43. await db.SaveChangesAsync(ct);
  44. }
  45. }