Handler.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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.Api.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()
  14. .Include(c => c.Board)
  15. .Include(c => c.Post)
  16. .AsQueryable();
  17. if (request.BoardID.HasValue)
  18. query = query.Where(c => c.BoardID == request.BoardID.Value);
  19. if (request.PostID.HasValue)
  20. query = query.Where(c => c.PostID == request.PostID.Value);
  21. if (!string.IsNullOrWhiteSpace(request.Keyword))
  22. {
  23. var kw = request.Keyword.Trim();
  24. query = request.Search switch
  25. {
  26. 0 => query.Where(c => c.Content.Contains(kw)),
  27. 1 => query.Where(c => (c.Name != null && c.Name.Contains(kw)) || (c.SID != null && c.SID.Contains(kw))),
  28. _ => query.Where(c => c.Content.Contains(kw))
  29. };
  30. }
  31. if (!string.IsNullOrWhiteSpace(request.StartAt) && DateTime.TryParse(request.StartAt, out var startDate))
  32. query = query.Where(c => c.CreatedAt >= startDate);
  33. if (!string.IsNullOrWhiteSpace(request.EndAt) && DateTime.TryParse(request.EndAt, out var endDate))
  34. query = query.Where(c => c.CreatedAt <= endDate.AddDays(1));
  35. if (request.IsDeleted == true)
  36. query = query.Where(c => c.IsDeleted);
  37. query = query.OrderByDescending(c => c.ID);
  38. var total = await query.CountAsync(ct);
  39. var list = await query
  40. .Skip((request.Page - 1) * request.PerPage)
  41. .Take(request.PerPage)
  42. .Select(c => new
  43. {
  44. c.ID,
  45. c.BoardID,
  46. BoardName = c.Board.Name,
  47. c.PostID,
  48. PostSubject = c.Post.Subject,
  49. c.Content,
  50. c.Name,
  51. c.SID,
  52. c.IsReply,
  53. c.IsSecret,
  54. c.IsDeleted,
  55. c.Likes,
  56. c.Dislikes,
  57. c.Reports,
  58. c.Replies,
  59. c.UpdatedAt,
  60. c.CreatedAt
  61. })
  62. .ToListAsync(ct);
  63. var startNum = total - ((request.Page - 1) * request.PerPage);
  64. return new Response(
  65. total,
  66. [..list.Select((c, i) => new Response.Row(
  67. Num: startNum - i,
  68. c.ID,
  69. c.BoardID,
  70. c.BoardName,
  71. c.PostID,
  72. c.PostSubject,
  73. StripHtml(c.Content),
  74. c.Name,
  75. c.SID,
  76. c.IsReply,
  77. c.IsSecret,
  78. c.IsDeleted,
  79. c.Likes,
  80. c.Dislikes,
  81. c.Reports,
  82. c.Replies,
  83. c.UpdatedAt,
  84. c.CreatedAt
  85. ))]
  86. );
  87. }
  88. private static string StripHtml(string html)
  89. {
  90. if (string.IsNullOrEmpty(html))
  91. {
  92. return html;
  93. }
  94. var decoded = WebUtility.HtmlDecode(html);
  95. return HtmlTagRegex().Replace(decoded, "");
  96. }
  97. }