Index.cshtml.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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.LoginLog;
  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; } = 10;
  21. [DisplayName("검색 조건")]
  22. [Range(1, 2, ErrorMessage = "{0}이(가) 올바르지 않습니다.")]
  23. public int? Search { get; set; }
  24. [DisplayName("검색어")]
  25. [MaxLength(255, ErrorMessage = "{0}은(는) {1}자 이하로 입력하세요.")]
  26. public string? Keyword { get; set; }
  27. [DisplayName("성공 여부")]
  28. public bool? Success { 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. int ID,
  38. string Account,
  39. bool Success,
  40. string? Reason,
  41. string? IpAddress,
  42. string? Browser,
  43. string? OS,
  44. string? Device,
  45. string CreatedAt
  46. )> List { get; set; } = [];
  47. public Pagination? Pagination { get; set; }
  48. public async Task OnGetAsync(CancellationToken ct)
  49. {
  50. if (!ModelState.IsValid)
  51. {
  52. return;
  53. }
  54. var result = await mediator.Send(new SearchAdminLoginLogs.Query(
  55. Query.PageNum,
  56. Query.PerPage,
  57. Query.Search,
  58. Query.Keyword,
  59. Query.Success,
  60. Query.StartAt,
  61. Query.EndAt
  62. ), ct);
  63. Total = result.Total;
  64. List = [..result.List.Select(c => (
  65. c.Num,
  66. c.ID,
  67. c.Account,
  68. c.Success,
  69. c.Reason ?? "-",
  70. c.IpAddress,
  71. c.Browser ?? "-",
  72. c.OS ?? "-",
  73. c.Device ?? "-",
  74. c.CreatedAt.GetDateAt()
  75. ))];
  76. Pagination = new Pagination(result.Total, Query.PageNum, Query.PerPage);
  77. }
  78. public async Task<IActionResult> OnPostDeleteAsync(int[] ids, CancellationToken ct)
  79. {
  80. try
  81. {
  82. if (ids.Length == 0)
  83. {
  84. throw new Exception("삭제할 항목을 선택해주세요.");
  85. }
  86. await mediator.Send(new DeleteAdminLoginLog.Command(ids), ct);
  87. TempData["SuccessMessage"] = $"{ids.Length}건이 삭제되었습니다.";
  88. }
  89. catch (Exception e)
  90. {
  91. TempData["ErrorMessages"] = e.Message;
  92. }
  93. return RedirectToPage(Query);
  94. }
  95. }