Handler.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using Application.Abstractions.Data;
  2. using Application.Abstractions.Messaging;
  3. using Domain.Entities.Forum.ValueObject;
  4. using Microsoft.EntityFrameworkCore;
  5. namespace Application.Features.Forum.CommentReport.Search;
  6. public sealed class Handler(IAppDbContext db) : IQueryHandler<Query, Response>
  7. {
  8. public async Task<Response> Handle(Query request, CancellationToken ct)
  9. {
  10. var query = db.CommentReport.AsNoTracking()
  11. .Include(c => c.Board)
  12. .Include(c => c.Post)
  13. .Include(c => c.Comment)
  14. .Include(c => c.Member)
  15. .AsQueryable();
  16. if (request.BoardID.HasValue)
  17. {
  18. query = query.Where(c => c.BoardID == request.BoardID.Value);
  19. }
  20. if (request.PostID.HasValue)
  21. {
  22. query = query.Where(c => c.PostID == request.PostID.Value);
  23. }
  24. if (request.CommentID.HasValue)
  25. {
  26. query = query.Where(c => c.CommentID == request.CommentID.Value);
  27. }
  28. if (request.Status.HasValue)
  29. {
  30. query = query.Where(c => c.Status == (ReportStatus)request.Status.Value);
  31. }
  32. if (request.Type.HasValue)
  33. {
  34. query = query.Where(c => c.Type == (ReportType)request.Type.Value);
  35. }
  36. if (!string.IsNullOrWhiteSpace(request.StartAt) && DateTime.TryParse(request.StartAt, out var startDate))
  37. {
  38. query = query.Where(c => c.CreatedAt >= startDate);
  39. }
  40. if (!string.IsNullOrWhiteSpace(request.EndAt) && DateTime.TryParse(request.EndAt, out var endDate))
  41. {
  42. query = query.Where(c => c.CreatedAt <= endDate.AddDays(1));
  43. }
  44. query = query.OrderByDescending(c => c.ID);
  45. var total = await query.CountAsync(ct);
  46. var list = await query
  47. .Skip((request.PageNum - 1) * request.PerPage)
  48. .Take(request.PerPage)
  49. .Select(c => new
  50. {
  51. c.ID,
  52. c.BoardID,
  53. BoardName = c.Board.Name,
  54. c.PostID,
  55. PostSubject = c.Post.Subject,
  56. c.CommentID,
  57. Comment = c.Comment.Content,
  58. c.MemberID,
  59. MemberName = c.Member.Name,
  60. c.Type,
  61. c.Reason,
  62. c.Status,
  63. c.Memo,
  64. c.UpdatedAt,
  65. c.CreatedAt
  66. })
  67. .ToListAsync(ct);
  68. var startNum = total - ((request.PageNum - 1) * request.PerPage);
  69. return new Response(
  70. total,
  71. [..list.Select((c, i) => new Response.Row(
  72. Num: startNum - i,
  73. c.ID,
  74. c.BoardID,
  75. c.BoardName,
  76. c.PostID,
  77. c.PostSubject,
  78. c.CommentID,
  79. c.Comment,
  80. c.MemberID,
  81. c.MemberName,
  82. c.Type,
  83. c.Reason,
  84. c.Status,
  85. c.Memo,
  86. c.UpdatedAt,
  87. c.CreatedAt
  88. ))]
  89. );
  90. }
  91. }