Index.cshtml.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using SharedKernel;
  2. using SharedKernel.Helpers;
  3. using SharedKernel.Extensions;
  4. using MediatR;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.AspNetCore.Mvc.RazorPages;
  7. using Microsoft.AspNetCore.Mvc.Rendering;
  8. using System.ComponentModel;
  9. using System.ComponentModel.DataAnnotations;
  10. using Microsoft.Extensions.Options;
  11. namespace Admin.Pages.Forum.Attachments.PostImage
  12. {
  13. public class IndexModel(IMediator mediator, IOptions<AppSettings> options) : PageModel
  14. {
  15. public readonly string ApiURL = options.Value.App.ApiURL;
  16. [BindProperty(SupportsGet = true)]
  17. public QueryParams Query { get; set; } = new();
  18. public sealed class QueryParams
  19. {
  20. public int? BoardID { get; set; }
  21. public int? PostID { get; set; }
  22. public int? Search { get; set; }
  23. public string? Keyword { get; set; }
  24. public string? StartAt { get; set; }
  25. public string? EndAt { get; set; }
  26. public bool? IsDisabled { get; set; }
  27. [Range(1, int.MaxValue)]
  28. [DisplayName("페이지 번호")]
  29. public int PageNum { get; set; } = 1;
  30. [Range(1, 100)]
  31. [DisplayName("페이지 목록 수")]
  32. public ushort PerPage { get; set; } = 20;
  33. }
  34. public int Total { get; set; } = 0;
  35. public List<SelectListItem> BoardList { get; set; } = [];
  36. public List<(
  37. int Num,
  38. int ID,
  39. int BoardID,
  40. string BoardName,
  41. int PostID,
  42. string PostSubject,
  43. string FileName,
  44. string Url,
  45. string? Extension,
  46. long? Size,
  47. short? Width,
  48. short? Height,
  49. bool IsDisabled,
  50. string CreatedAt
  51. )> List { get; set; } = [];
  52. public Pagination? Pagination { get; set; }
  53. public async Task OnGetAsync(CancellationToken ct)
  54. {
  55. if (!ModelState.IsValid)
  56. {
  57. return;
  58. }
  59. var boards = await mediator.Send(new SearchBoards.Query(null, null, 1, 500), ct);
  60. BoardList = [..boards.List.Select(c => new SelectListItem
  61. {
  62. Value = c.ID.ToString(),
  63. Text = $"[{c.BoardGroupName}] {c.Name}"
  64. })];
  65. var result = await mediator.Send(new SearchPostImages.Query(
  66. Query.BoardID, Query.PostID, Query.Search, Query.Keyword,
  67. Query.IsDisabled, Query.StartAt, Query.EndAt,
  68. Query.PageNum, Query.PerPage
  69. ), ct);
  70. Total = result.Total;
  71. List = [..result.List.Select(c => (
  72. c.Num,
  73. c.ID,
  74. c.BoardID,
  75. c.BoardName,
  76. c.PostID,
  77. c.PostSubject,
  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 DeletePostImage.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/PostImage/Index", Query);
  101. }
  102. public async Task<IActionResult> OnPostDisableAsync(int[] ids, CancellationToken ct)
  103. {
  104. try
  105. {
  106. await mediator.Send(new ToggleDisablePostImage.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/PostImage/Index", Query);
  114. }
  115. public async Task<IActionResult> OnPostEnableAsync(int[] ids, CancellationToken ct)
  116. {
  117. try
  118. {
  119. await mediator.Send(new ToggleDisablePostImage.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/PostImage/Index", Query);
  127. }
  128. }
  129. }