Handler.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using Application.Abstractions.Messaging;
  2. using Application.Abstractions.Data;
  3. using Domain.Entities.Forum.ValueObject;
  4. using Microsoft.EntityFrameworkCore;
  5. using SharedKernel.Results;
  6. namespace Application.Features.Api.Forum.CommentReaction.Toggle;
  7. public sealed class Handler(IAppDbContext db) : ICommandHandler<Command, Result>
  8. {
  9. public async Task<Result> Handle(Command request, CancellationToken ct)
  10. {
  11. var comment = await db.Comment.FirstOrDefaultAsync(x => x.ID == request.CommentID, ct);
  12. if (comment is null)
  13. {
  14. return Result.Failure(Error.NotFound("CommentReaction.CommentNotFound", "댓글을 찾을 수 없습니다."));
  15. }
  16. var existing = await db.CommentReaction.FirstOrDefaultAsync(x => x.CommentID == request.CommentID && x.MemberID == request.MemberID, ct);
  17. // MemberStats 조회 (Like만 카운트)
  18. var giverStats = await db.MemberStats.FirstOrDefaultAsync(x => x.MemberID == request.MemberID, ct);
  19. Domain.Entities.Members.MemberStats? receiverStats = null;
  20. if (comment.MemberID != request.MemberID)
  21. {
  22. receiverStats = await db.MemberStats.FirstOrDefaultAsync(x => x.MemberID == comment.MemberID, ct);
  23. }
  24. if (existing is not null)
  25. {
  26. if (existing.Reaction == request.Reaction)
  27. {
  28. if (existing.Reaction == Reaction.Like)
  29. {
  30. comment.Likes--;
  31. if (giverStats is not null) { giverStats.LikeGivenCount--; }
  32. if (receiverStats is not null) { receiverStats.LikeReceivedCount--; }
  33. }
  34. else
  35. {
  36. comment.Dislikes--;
  37. }
  38. db.CommentReaction.Remove(existing);
  39. }
  40. else
  41. {
  42. if (existing.Reaction == Reaction.Like)
  43. {
  44. comment.Likes--;
  45. comment.Dislikes++;
  46. // Like → Dislike: Like 카운트 감소
  47. if (giverStats is not null) { giverStats.LikeGivenCount--; }
  48. if (receiverStats is not null) { receiverStats.LikeReceivedCount--; }
  49. }
  50. else
  51. {
  52. comment.Dislikes--;
  53. comment.Likes++;
  54. // Dislike → Like: Like 카운트 증가
  55. if (giverStats is not null) { giverStats.LikeGivenCount++; }
  56. if (receiverStats is not null) { receiverStats.LikeReceivedCount++; }
  57. }
  58. existing.Reaction = request.Reaction;
  59. existing.UpdatedAt = DateTime.UtcNow;
  60. }
  61. }
  62. else
  63. {
  64. var reaction = new Domain.Entities.Forum.Comments.CommentReaction
  65. {
  66. BoardID = comment.BoardID,
  67. PostID = comment.PostID,
  68. CommentID = request.CommentID,
  69. MemberID = request.MemberID,
  70. Reaction = request.Reaction
  71. };
  72. await db.CommentReaction.AddAsync(reaction, ct);
  73. if (request.Reaction == Reaction.Like)
  74. {
  75. comment.Likes++;
  76. if (giverStats is not null) { giverStats.LikeGivenCount++; }
  77. if (receiverStats is not null) { receiverStats.LikeReceivedCount++; }
  78. }
  79. else
  80. {
  81. comment.Dislikes++;
  82. }
  83. }
  84. await db.SaveChangesAsync(ct);
  85. return Result.Success();
  86. }
  87. }