List.cshtml.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using SharedKernel.Helpers;
  2. using Application.Abstractions.Messaging;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.AspNetCore.Mvc.RazorPages;
  5. using System.ComponentModel;
  6. using System.ComponentModel.DataAnnotations;
  7. namespace Admin.Pages.Mail;
  8. public class ListModel(IMediator mediator) : PageModel
  9. {
  10. [BindProperty(SupportsGet = true)]
  11. public QueryParams Query { get; set; } = new();
  12. public sealed class QueryParams
  13. {
  14. [Range(1, int.MaxValue)]
  15. [DisplayName("페이지 번호")]
  16. public int PageNum { get; set; } = 1;
  17. [Range(1, 100)]
  18. [DisplayName("페이지 목록 수")]
  19. public ushort PerPage { get; set; } = 10;
  20. [DisplayName("검색 조건")]
  21. [Range(1, 4, ErrorMessage = "{0}이(가) 올바르지 않습니다.")]
  22. public int? Search { get; set; }
  23. [DisplayName("검색어")]
  24. [MaxLength(100, ErrorMessage = "{0}은(는) {1}자 이하로 입력하세요.")]
  25. public string? Keyword { get; set; }
  26. [DisplayName("시작일")]
  27. public string? StartAt { get; set; }
  28. [DisplayName("종료일")]
  29. public string? EndAt { get; set; }
  30. [DisplayName("탭")]
  31. [Range(0, 4)]
  32. public int Tab { get; set; } = 0;
  33. }
  34. public int Total { get; set; } = 0;
  35. public int PendingCount { get; set; } = 0;
  36. public int ProcessingCount { get; set; } = 0;
  37. public int SentCount { get; set; } = 0;
  38. public int FailedCount { get; set; } = 0;
  39. public List<SearchEmailLogs.Response.Row> List { get; set; } = [];
  40. public Pagination? Pagination { get; set; }
  41. public async Task OnGetAsync(CancellationToken ct)
  42. {
  43. if (!ModelState.IsValid)
  44. {
  45. return;
  46. }
  47. var result = await mediator.Send(new SearchEmailLogs.Query(
  48. Query.PageNum,
  49. Query.PerPage,
  50. Query.Search,
  51. Query.Keyword,
  52. Query.StartAt,
  53. Query.EndAt,
  54. Query.Tab
  55. ), ct);
  56. Total = result.Total;
  57. PendingCount = result.Pending;
  58. ProcessingCount = result.Processing;
  59. SentCount = result.Sent;
  60. FailedCount = result.Failed;
  61. List = [.. result.List];
  62. Pagination = new Pagination(result.Total, Query.PageNum, Query.PerPage, Request.QueryString.ToString());
  63. }
  64. public async Task<IActionResult> OnPostResendAsync(long[] ids, CancellationToken ct)
  65. {
  66. try
  67. {
  68. if (ids is null || ids.Length == 0)
  69. {
  70. throw new Exception("재전송할 항목을 선택해주세요.");
  71. }
  72. await mediator.Send(new ResendEmailLog.Command(ids), ct);
  73. TempData["SuccessMessage"] = $"{ids.Length}건이 재전송 대기열에 추가되었습니다.";
  74. }
  75. catch (Exception e)
  76. {
  77. TempData["ErrorMessages"] = e.Message;
  78. }
  79. return Redirect($"{Request.Path}{Request.QueryString}");
  80. }
  81. public async Task<IActionResult> OnPostDeleteAsync(long[] ids, CancellationToken ct)
  82. {
  83. try
  84. {
  85. if (ids is null || ids.Length == 0)
  86. {
  87. throw new Exception("삭제할 항목을 선택해주세요.");
  88. }
  89. await mediator.Send(new DeleteEmailLog.Command(ids), ct);
  90. TempData["SuccessMessage"] = $"{ids.Length}건이 삭제되었습니다.";
  91. }
  92. catch (Exception e)
  93. {
  94. TempData["ErrorMessages"] = e.Message;
  95. }
  96. return Redirect($"{Request.Path}{Request.QueryString}");
  97. }
  98. }