Index.cshtml.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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.List;
  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, 7, 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. }
  31. public int Total { get; set; } = 0;
  32. public List<SearchDonations.Response.Row> List { get; set; } = [];
  33. public Pagination? Pagination { get; set; }
  34. public async Task OnGetAsync(CancellationToken ct)
  35. {
  36. if (!ModelState.IsValid)
  37. {
  38. return;
  39. }
  40. var result = await mediator.Send(new SearchDonations.Query(
  41. Query.PageNum,
  42. Query.PerPage,
  43. Query.Search,
  44. Query.Keyword,
  45. Query.StartAt,
  46. Query.EndAt
  47. ), ct);
  48. Total = result.Total;
  49. List = [.. result.List];
  50. Pagination = new Pagination(result.Total, Query.PageNum, Query.PerPage, Request.QueryString.ToString());
  51. }
  52. public async Task<IActionResult> OnPostDeleteAsync(int[] ids, CancellationToken ct)
  53. {
  54. try
  55. {
  56. if (ids is null || ids.Length == 0)
  57. {
  58. throw new Exception("삭제할 항목을 선택해주세요.");
  59. }
  60. await mediator.Send(new DeleteDonations.Command(ids), ct);
  61. TempData["SuccessMessage"] = $"{ids.Length}건이 삭제되었습니다.";
  62. }
  63. catch (Exception e)
  64. {
  65. TempData["ErrorMessages"] = e.Message;
  66. }
  67. return Redirect($"{Request.Path}{Request.QueryString}");
  68. }
  69. public async Task<IActionResult> OnGetExportDataAsync(CancellationToken ct)
  70. {
  71. var result = await mediator.Send(new ExportDonations.Query(
  72. Query.Search,
  73. Query.Keyword,
  74. Query.StartAt,
  75. Query.EndAt
  76. ), ct);
  77. return new JsonResult(new { rows = result.Rows });
  78. }
  79. }