Index.cshtml.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. string? Name,
  40. string? SID,
  41. string? DeletedAt,
  42. string CreatedAt
  43. )> List { get; set; } = [];
  44. public Pagination? Pagination { get; set; }
  45. public async Task OnGetAsync(CancellationToken ct)
  46. {
  47. if (!ModelState.IsValid)
  48. {
  49. return;
  50. }
  51. var boards = await mediator.Send(new SearchBoards.Query(null, null, 1, 500), ct);
  52. BoardList = [..boards.List.Select(c => new SelectListItem
  53. {
  54. Value = c.ID.ToString(),
  55. Text = $"[{c.BoardGroupName}] {c.Name}"
  56. })];
  57. var result = await mediator.Send(new SearchTrashComments.Query(
  58. Query.BoardID,
  59. Query.Search,
  60. Query.Keyword,
  61. Query.StartAt,
  62. Query.EndAt,
  63. Query.PageNum,
  64. Query.PerPage
  65. ), ct);
  66. Total = result.Total;
  67. List = [..result.List.Select(c => (
  68. c.Num, c.ID,
  69. c.BoardID,
  70. c.BoardName,
  71. c.PostID,
  72. c.PostSubject,
  73. c.Content,
  74. c.Name,
  75. c.SID,
  76. c.DeletedAt.GetDateAt() ?? "-",
  77. c.CreatedAt.GetDateAt()
  78. ))];
  79. Pagination = new Pagination(result.Total, Query.PageNum, Query.PerPage);
  80. }
  81. public async Task<IActionResult> OnPostRestoreAsync(int[] ids, CancellationToken ct)
  82. {
  83. try
  84. {
  85. await mediator.Send(new RestoreTrashComment.Command(ids), ct);
  86. TempData["SuccessMessage"] = $"{ids.Length}건이 복원되었습니다.";
  87. }
  88. catch (Exception e)
  89. {
  90. TempData["ErrorMessages"] = e.Message;
  91. }
  92. return RedirectToPage("/Forum/Trash/Comment/Index", Query);
  93. }
  94. public async Task<IActionResult> OnPostDeleteAsync(int[] ids, CancellationToken ct)
  95. {
  96. try
  97. {
  98. await mediator.Send(new PermanentDeleteTrashComment.Command(ids), ct);
  99. TempData["SuccessMessage"] = $"{ids.Length}건이 영구 삭제되었습니다.";
  100. }
  101. catch (Exception e)
  102. {
  103. TempData["ErrorMessages"] = e.Message;
  104. }
  105. return RedirectToPage("/Forum/Trash/Comment/Index", Query);
  106. }
  107. }
  108. }