Index.cshtml.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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.Attachments.CommentImage
  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? PostID { get; set; }
  19. public int? CommentID { get; set; }
  20. public int? Search { get; set; }
  21. public string? Keyword { get; set; }
  22. public string? StartAt { get; set; }
  23. public string? EndAt { get; set; }
  24. public bool? IsDisabled { get; set; }
  25. [Range(1, int.MaxValue)]
  26. [DisplayName("페이지 번호")]
  27. public int PageNum { get; set; } = 1;
  28. [Range(1, 100)]
  29. [DisplayName("페이지 목록 수")]
  30. public ushort PerPage { get; set; } = 20;
  31. }
  32. public int Total { get; set; } = 0;
  33. public List<SelectListItem> BoardList { get; set; } = [];
  34. public List<(
  35. int Num,
  36. int ID,
  37. int BoardID,
  38. string BoardName,
  39. int PostID,
  40. string PostSubject,
  41. int CommentID,
  42. string FileName,
  43. string Url,
  44. string? Extension,
  45. long? Size,
  46. short? Width,
  47. short? Height,
  48. bool IsDisabled,
  49. string CreatedAt
  50. )> List { get; set; } = [];
  51. public Pagination? Pagination { get; set; }
  52. public async Task OnGetAsync(CancellationToken ct)
  53. {
  54. if (!ModelState.IsValid)
  55. {
  56. return;
  57. }
  58. var boards = await mediator.Send(new SearchBoards.Query(null, null, 1, 500), ct);
  59. BoardList = [..boards.List.Select(c => new SelectListItem
  60. {
  61. Value = c.ID.ToString(),
  62. Text = $"[{c.BoardGroupName}] {c.Name}"
  63. })];
  64. var result = await mediator.Send(new SearchCommentImages.Query(
  65. Query.BoardID, Query.PostID, Query.CommentID,
  66. Query.Search, Query.Keyword, Query.IsDisabled,
  67. Query.StartAt, Query.EndAt, Query.PageNum, Query.PerPage
  68. ), ct);
  69. Total = result.Total;
  70. List = [..result.List.Select(c => (
  71. c.Num,
  72. c.ID,
  73. c.BoardID,
  74. c.BoardName,
  75. c.PostID,
  76. c.PostSubject,
  77. c.CommentID,
  78. c.FileName,
  79. c.Url,
  80. c.Extension,
  81. c.Size,
  82. c.Width,
  83. c.Height,
  84. c.IsDisabled,
  85. c.CreatedAt.GetDateAt()
  86. ))];
  87. Pagination = new Pagination(result.Total, Query.PageNum, Query.PerPage);
  88. }
  89. public async Task<IActionResult> OnPostDeleteAsync(int[] ids, CancellationToken ct)
  90. {
  91. try
  92. {
  93. await mediator.Send(new DeleteCommentImage.Command(ids), ct);
  94. TempData["SuccessMessage"] = $"{ids.Length}건이 삭제되었습니다.";
  95. }
  96. catch (Exception e)
  97. {
  98. TempData["ErrorMessages"] = e.Message;
  99. }
  100. return RedirectToPage("/Forum/Attachments/CommentImage/Index", Query);
  101. }
  102. public async Task<IActionResult> OnPostDisableAsync(int[] ids, CancellationToken ct)
  103. {
  104. try
  105. {
  106. await mediator.Send(new ToggleDisableCommentImage.Command(ids, true), ct);
  107. TempData["SuccessMessage"] = $"{ids.Length}건이 비활성화되었습니다.";
  108. }
  109. catch (Exception e)
  110. {
  111. TempData["ErrorMessages"] = e.Message;
  112. }
  113. return RedirectToPage("/Forum/Attachments/CommentImage/Index", Query);
  114. }
  115. public async Task<IActionResult> OnPostEnableAsync(int[] ids, CancellationToken ct)
  116. {
  117. try
  118. {
  119. await mediator.Send(new ToggleDisableCommentImage.Command(ids, false), ct);
  120. TempData["SuccessMessage"] = $"{ids.Length}건이 활성화되었습니다.";
  121. }
  122. catch (Exception e)
  123. {
  124. TempData["ErrorMessages"] = e.Message;
  125. }
  126. return RedirectToPage("/Forum/Attachments/CommentImage/Index", Query);
  127. }
  128. }
  129. }