Index.cshtml.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using SharedKernel.Helpers;
  2. using MediatR;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.AspNetCore.Mvc.RazorPages;
  5. using System.ComponentModel;
  6. using System.ComponentModel.DataAnnotations;
  7. namespace Admin.Pages.Donation.Alert;
  8. public class IndexModel(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, 6, 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, 2)]
  32. public int Tab { get; set; } = 0;
  33. }
  34. public int Total { get; set; } = 0;
  35. public int Received { get; set; } = 0;
  36. public int NotReceived { get; set; } = 0;
  37. public List<SearchDonationAlerts.Response.Row> List { get; set; } = [];
  38. public Pagination? Pagination { get; set; }
  39. public async Task OnGetAsync(CancellationToken ct)
  40. {
  41. if (!ModelState.IsValid)
  42. {
  43. return;
  44. }
  45. var result = await mediator.Send(new SearchDonationAlerts.Query(
  46. Query.PageNum,
  47. Query.PerPage,
  48. Query.Search,
  49. Query.Keyword,
  50. Query.StartAt,
  51. Query.EndAt,
  52. Query.Tab
  53. ), ct);
  54. Total = result.Total;
  55. Received = result.Received;
  56. NotReceived = result.NotReceived;
  57. List = [.. result.List];
  58. Pagination = new Pagination(result.Total, Query.PageNum, Query.PerPage, Request.QueryString.ToString());
  59. }
  60. public async Task<IActionResult> OnPostDeleteAsync(int[] ids, CancellationToken ct)
  61. {
  62. try
  63. {
  64. if (ids is null || ids.Length == 0)
  65. {
  66. throw new Exception("삭제할 항목을 선택해주세요.");
  67. }
  68. await mediator.Send(new DeleteDonationAlert.Command(ids), ct);
  69. TempData["SuccessMessage"] = $"{ids.Length}건이 삭제되었습니다.";
  70. }
  71. catch (Exception e)
  72. {
  73. TempData["ErrorMessages"] = e.Message;
  74. }
  75. return Redirect($"{Request.Path}{Request.QueryString}");
  76. }
  77. public async Task<IActionResult> OnPostResendAsync(int id, CancellationToken ct)
  78. {
  79. try
  80. {
  81. await mediator.Send(new ResendDonationAlert.Command(id), ct);
  82. TempData["SuccessMessage"] = "후원 알림을 재전송 대기열에 추가했습니다.";
  83. }
  84. catch (Exception e)
  85. {
  86. TempData["ErrorMessages"] = e.Message;
  87. }
  88. return Redirect($"{Request.Path}{Request.QueryString}");
  89. }
  90. }