Index.cshtml.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using SharedKernel.Helpers;
  2. using SharedKernel.Extensions;
  3. using MediatR;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Microsoft.AspNetCore.Mvc.RazorPages;
  6. using System.ComponentModel;
  7. using System.ComponentModel.DataAnnotations;
  8. namespace Admin.Pages.Director.AccessLog;
  9. public class IndexModel(IMediator mediator) : PageModel
  10. {
  11. [BindProperty(SupportsGet = true)]
  12. public QueryParams Query { get; set; } = new();
  13. public sealed class QueryParams
  14. {
  15. [Range(1, int.MaxValue)]
  16. [DisplayName("페이지 번호")]
  17. public int PageNum { get; set; } = 1;
  18. [Range(1, 100)]
  19. [DisplayName("페이지 목록 수")]
  20. public ushort PerPage { get; set; } = 20;
  21. [DisplayName("검색 조건")]
  22. [Range(1, 4, ErrorMessage = "{0}이(가) 올바르지 않습니다.")]
  23. public int? Search { get; set; }
  24. [DisplayName("검색어")]
  25. [MaxLength(255, ErrorMessage = "{0}은(는) {1}자 이하로 입력하세요.")]
  26. public string? Keyword { get; set; }
  27. [DisplayName("Method")]
  28. public string? Method { get; set; }
  29. [DisplayName("시작일")]
  30. public string? StartAt { get; set; }
  31. [DisplayName("종료일")]
  32. public string? EndAt { get; set; }
  33. }
  34. public int Total { get; set; } = 0;
  35. public List<(
  36. int Num,
  37. long ID,
  38. string UserID,
  39. string? UserName,
  40. string Method,
  41. string Path,
  42. string? QueryString,
  43. int StatusCode,
  44. long ElapsedMs,
  45. string? MenuName,
  46. string? IpAddress,
  47. string CreatedAt
  48. )> List { get; set; } = [];
  49. public Pagination? Pagination { get; set; }
  50. public async Task OnGetAsync(CancellationToken ct)
  51. {
  52. if (!ModelState.IsValid)
  53. {
  54. return;
  55. }
  56. var result = await mediator.Send(new SearchAdminAccessLogs.Query(
  57. Query.PageNum,
  58. Query.PerPage,
  59. Query.Search,
  60. Query.Keyword,
  61. Query.Method,
  62. Query.StartAt,
  63. Query.EndAt
  64. ), ct);
  65. Total = result.Total;
  66. List = [..result.List.Select(c => (
  67. c.Num,
  68. c.ID,
  69. c.UserID,
  70. c.UserName,
  71. c.Method,
  72. c.Path,
  73. c.QueryString,
  74. c.StatusCode,
  75. c.ElapsedMs,
  76. c.MenuName,
  77. c.IpAddress,
  78. c.CreatedAt.GetDateAt()
  79. ))];
  80. Pagination = new Pagination(result.Total, Query.PageNum, Query.PerPage);
  81. }
  82. public async Task<IActionResult> OnPostDeleteAsync(long[] ids, CancellationToken ct)
  83. {
  84. try
  85. {
  86. if (ids.Length == 0)
  87. {
  88. throw new Exception("삭제할 항목을 선택해주세요.");
  89. }
  90. await mediator.Send(new DeleteAdminAccessLog.Command(ids), ct);
  91. TempData["SuccessMessage"] = $"{ids.Length}건이 삭제되었습니다.";
  92. }
  93. catch (Exception e)
  94. {
  95. TempData["ErrorMessages"] = e.Message;
  96. }
  97. return RedirectToPage(Query);
  98. }
  99. }