Handler.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System.Net;
  2. using System.Text.RegularExpressions;
  3. using Application.Abstractions.Data;
  4. using Application.Abstractions.Messaging;
  5. using Microsoft.EntityFrameworkCore;
  6. namespace Application.Features.Admin.Forum.Comment.Search;
  7. public sealed partial class Handler(IAppDbContext db) : IQueryHandler<Query, Response>
  8. {
  9. [GeneratedRegex("<[^>]*>")]
  10. private static partial Regex HtmlTagRegex();
  11. public async Task<Response> Handle(Query request, CancellationToken ct)
  12. {
  13. var query = db.Comment.AsNoTracking().Include(c => c.Board).Include(c => c.Post).AsQueryable();
  14. if (request.BoardID.HasValue)
  15. {
  16. query = query.Where(c => c.BoardID == request.BoardID.Value);
  17. }
  18. if (request.PostID.HasValue)
  19. {
  20. query = query.Where(c => c.PostID == request.PostID.Value);
  21. }
  22. if (!string.IsNullOrWhiteSpace(request.Keyword))
  23. {
  24. var kw = request.Keyword.Trim();
  25. query = request.Search switch
  26. {
  27. 0 => query.Where(c => c.Content.Contains(kw)),
  28. 1 => query.Where(c => (c.Name != null && c.Name.Contains(kw)) || (c.SID != null && c.SID.Contains(kw))),
  29. _ => query.Where(c => c.Content.Contains(kw))
  30. };
  31. }
  32. if (!string.IsNullOrWhiteSpace(request.StartAt) && DateTime.TryParse(request.StartAt, out var startDate))
  33. {
  34. query = query.Where(c => c.CreatedAt >= startDate);
  35. }
  36. if (!string.IsNullOrWhiteSpace(request.EndAt) && DateTime.TryParse(request.EndAt, out var endDate))
  37. {
  38. query = query.Where(c => c.CreatedAt <= endDate.AddDays(1));
  39. }
  40. if (request.IsDeleted == true)
  41. {
  42. query = query.Where(c => c.IsDeleted);
  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.Content,
  57. c.Name,
  58. c.SID,
  59. c.IsReply,
  60. c.IsSecret,
  61. c.IsDeleted,
  62. c.Likes,
  63. c.Dislikes,
  64. c.Reports,
  65. c.Replies,
  66. c.UpdatedAt,
  67. c.CreatedAt
  68. })
  69. .ToListAsync(ct);
  70. var startNum = total - ((request.PageNum - 1) * request.PerPage);
  71. return new Response(
  72. total,
  73. [..list.Select((c, i) => new Response.Row(
  74. Num: startNum - i,
  75. c.ID,
  76. c.BoardID,
  77. c.BoardName,
  78. c.PostID,
  79. c.PostSubject,
  80. StripHtml(c.Content),
  81. c.Name,
  82. c.SID,
  83. c.IsReply,
  84. c.IsSecret,
  85. c.IsDeleted,
  86. c.Likes,
  87. c.Dislikes,
  88. c.Reports,
  89. c.Replies,
  90. c.UpdatedAt,
  91. c.CreatedAt
  92. ))]
  93. );
  94. }
  95. private static string StripHtml(string html)
  96. {
  97. if (string.IsNullOrEmpty(html))
  98. {
  99. return html;
  100. }
  101. var decoded = WebUtility.HtmlDecode(html);
  102. return HtmlTagRegex().Replace(decoded, "");
  103. }
  104. }