Index.cshtml.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using SharedKernel.Helpers;
  2. using SharedKernel.Extensions;
  3. using MediatR;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Microsoft.AspNetCore.Mvc.RazorPages;
  6. using Microsoft.AspNetCore.Mvc.Rendering;
  7. using System.ComponentModel;
  8. using System.ComponentModel.DataAnnotations;
  9. namespace Admin.Pages.Forum.Trash.Comment
  10. {
  11. public class IndexModel(IMediator mediator) : PageModel
  12. {
  13. [BindProperty(SupportsGet = true)]
  14. public QueryParams Query { get; set; } = new();
  15. public sealed class QueryParams
  16. {
  17. public int? BoardID { get; set; }
  18. public int? Search { get; set; }
  19. public string? Keyword { get; set; }
  20. public string? StartAt { get; set; }
  21. public string? EndAt { get; set; }
  22. [Range(1, int.MaxValue)]
  23. [DisplayName("페이지 번호")]
  24. public int PageNum { get; set; } = 1;
  25. [Range(1, 100)]
  26. [DisplayName("페이지 목록 수")]
  27. public ushort PerPage { get; set; } = 20;
  28. }
  29. public int Total { get; set; } = 0;
  30. public List<SelectListItem> BoardList { get; set; } = [];
  31. public List<(
  32. int Num,
  33. int ID,
  34. int BoardID,
  35. string BoardName,
  36. int PostID,
  37. string PostSubject,
  38. string Content,
  39. bool IsReply,
  40. string? Name,
  41. string? SID,
  42. string? DeletedAt,
  43. string CreatedAt
  44. )> List { get; set; } = [];
  45. public Pagination? Pagination { get; set; }
  46. public async Task OnGetAsync(CancellationToken ct)
  47. {
  48. if (!ModelState.IsValid)
  49. {
  50. return;
  51. }
  52. var boards = await mediator.Send(new SearchBoards.Query(null, null, 1, 500), ct);
  53. BoardList = [..boards.List.Select(c => new SelectListItem
  54. {
  55. Value = c.ID.ToString(),
  56. Text = $"[{c.BoardGroupName}] {c.Name}"
  57. })];
  58. var result = await mediator.Send(new SearchTrashComments.Query(
  59. Query.BoardID,
  60. Query.Search,
  61. Query.Keyword,
  62. Query.StartAt,
  63. Query.EndAt,
  64. Query.PageNum,
  65. Query.PerPage
  66. ), ct);
  67. Total = result.Total;
  68. List = [..result.List.Select(c => (
  69. c.Num, c.ID,
  70. c.BoardID,
  71. c.BoardName,
  72. c.PostID,
  73. c.PostSubject,
  74. c.Content,
  75. c.IsReply,
  76. c.Name,
  77. c.SID,
  78. c.DeletedAt.GetDateAt() ?? "-",
  79. c.CreatedAt.GetDateAt()
  80. ))];
  81. Pagination = new Pagination(result.Total, Query.PageNum, Query.PerPage);
  82. }
  83. public async Task<IActionResult> OnPostRestoreAsync(int[] ids, CancellationToken ct)
  84. {
  85. try
  86. {
  87. await mediator.Send(new RestoreTrashComment.Command(ids), ct);
  88. TempData["SuccessMessage"] = $"{ids.Length}건이 복원되었습니다.";
  89. }
  90. catch (Exception e)
  91. {
  92. TempData["ErrorMessages"] = e.Message;
  93. }
  94. return RedirectToPage("/Forum/Trash/Comment/Index", Query);
  95. }
  96. public async Task<IActionResult> OnPostDeleteAsync(int[] ids, CancellationToken ct)
  97. {
  98. try
  99. {
  100. await mediator.Send(new PermanentDeleteTrashComment.Command(ids), ct);
  101. TempData["SuccessMessage"] = $"{ids.Length}건이 영구 삭제되었습니다.";
  102. }
  103. catch (Exception e)
  104. {
  105. TempData["ErrorMessages"] = e.Message;
  106. }
  107. return RedirectToPage("/Forum/Trash/Comment/Index", Query);
  108. }
  109. }
  110. }