Index.cshtml.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using SharedKernel.Helpers;
  2. using SharedKernel.Extensions;
  3. using Application.Abstractions.Messaging;
  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.Attachments.CommentFile;
  10. public class IndexModel(IMediator mediator) : PageModel
  11. {
  12. [BindProperty(SupportsGet = true)]
  13. public QueryParams Query { get; set; } = new();
  14. public sealed class QueryParams
  15. {
  16. public int? BoardID { get; set; }
  17. public int? PostID { get; set; }
  18. public int? CommentID { get; set; }
  19. public int? Search { get; set; }
  20. public string? Keyword { get; set; }
  21. public string? StartAt { get; set; }
  22. public string? EndAt { get; set; }
  23. public bool? IsDisabled { get; set; }
  24. [Range(1, int.MaxValue)]
  25. [DisplayName("페이지 번호")]
  26. public int PageNum { get; set; } = 1;
  27. [Range(1, 100)]
  28. [DisplayName("페이지 목록 수")]
  29. public ushort PerPage { get; set; } = 20;
  30. }
  31. public int Total { get; set; } = 0;
  32. public List<SelectListItem> BoardList { get; set; } = [];
  33. public List<(
  34. int Num,
  35. int ID,
  36. int BoardID,
  37. string BoardName,
  38. int PostID,
  39. string PostSubject,
  40. int CommentID,
  41. string FileName,
  42. string Url,
  43. string? Extension,
  44. long? Size,
  45. int Downloads,
  46. bool IsDisabled,
  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 boards = await mediator.Send(new SearchBoards.Query(null, null, 1, 500), ct);
  57. BoardList = [..boards.List.Select(c => new SelectListItem
  58. {
  59. Value = c.ID.ToString(),
  60. Text = $"[{c.BoardGroupName}] {c.Name}"
  61. })];
  62. var result = await mediator.Send(new SearchCommentFiles.Query(
  63. Query.BoardID, Query.PostID, Query.CommentID,
  64. Query.Search, Query.Keyword, Query.IsDisabled,
  65. Query.StartAt, Query.EndAt, Query.PageNum, Query.PerPage
  66. ), ct);
  67. Total = result.Total;
  68. List = [..result.List.Select(c => (
  69. c.Num,
  70. c.ID,
  71. c.BoardID,
  72. c.BoardName,
  73. c.PostID,
  74. c.PostSubject,
  75. c.CommentID,
  76. c.FileName,
  77. c.Url,
  78. c.Extension,
  79. c.Size,
  80. c.Downloads,
  81. c.IsDisabled,
  82. c.CreatedAt.GetDateAt()
  83. ))];
  84. Pagination = new Pagination(result.Total, Query.PageNum, Query.PerPage);
  85. }
  86. public async Task<IActionResult> OnPostDeleteAsync(int[] ids, CancellationToken ct)
  87. {
  88. try
  89. {
  90. await mediator.Send(new DeleteCommentFile.Command(ids), ct);
  91. TempData["SuccessMessage"] = $"{ids.Length}건이 삭제되었습니다.";
  92. }
  93. catch (Exception e)
  94. {
  95. TempData["ErrorMessages"] = e.Message;
  96. }
  97. return RedirectToPage("/Forum/Attachments/CommentFile/Index", Query);
  98. }
  99. public async Task<IActionResult> OnPostDisableAsync(int[] ids, CancellationToken ct)
  100. {
  101. try
  102. {
  103. await mediator.Send(new ToggleDisableCommentFile.Command(ids, true), ct);
  104. TempData["SuccessMessage"] = $"{ids.Length}건이 비활성화되었습니다.";
  105. }
  106. catch (Exception e)
  107. {
  108. TempData["ErrorMessages"] = e.Message;
  109. }
  110. return RedirectToPage("/Forum/Attachments/CommentFile/Index", Query);
  111. }
  112. public async Task<IActionResult> OnPostEnableAsync(int[] ids, CancellationToken ct)
  113. {
  114. try
  115. {
  116. await mediator.Send(new ToggleDisableCommentFile.Command(ids, false), ct);
  117. TempData["SuccessMessage"] = $"{ids.Length}건이 활성화되었습니다.";
  118. }
  119. catch (Exception e)
  120. {
  121. TempData["ErrorMessages"] = e.Message;
  122. }
  123. return RedirectToPage("/Forum/Attachments/CommentFile/Index", Query);
  124. }
  125. }