Index.cshtml.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using SharedKernel;
  2. using SharedKernel.Helpers;
  3. using SharedKernel.Extensions;
  4. using Application.Abstractions.Messaging;
  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. public class IndexModel(IMediator mediator, IOptions<AppSettings> options) : PageModel
  13. {
  14. public readonly string ApiURL = options.Value.App.ApiURL;
  15. [BindProperty(SupportsGet = true)]
  16. public QueryParams Query { get; set; } = new();
  17. public sealed class QueryParams
  18. {
  19. public int? BoardID { get; set; }
  20. public int? PostID { get; set; }
  21. public int? Search { get; set; }
  22. public string? Keyword { get; set; }
  23. public string? StartAt { get; set; }
  24. public string? EndAt { get; set; }
  25. public bool? IsDisabled { get; set; }
  26. [Range(1, int.MaxValue)]
  27. [DisplayName("페이지 번호")]
  28. public int PageNum { get; set; } = 1;
  29. [Range(1, 100)]
  30. [DisplayName("페이지 목록 수")]
  31. public ushort PerPage { get; set; } = 20;
  32. }
  33. public int Total { get; set; } = 0;
  34. public List<SelectListItem> BoardList { get; set; } = [];
  35. public List<(
  36. int Num,
  37. int ID,
  38. int BoardID,
  39. string BoardName,
  40. int PostID,
  41. string PostSubject,
  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 SearchPostImages.Query(
  65. Query.BoardID, Query.PostID, Query.Search, Query.Keyword,
  66. Query.IsDisabled, Query.StartAt, Query.EndAt,
  67. 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.FileName,
  78. c.Url,
  79. c.Extension,
  80. c.Size,
  81. c.Width,
  82. c.Height,
  83. c.IsDisabled,
  84. c.CreatedAt.GetDateAt()
  85. ))];
  86. Pagination = new Pagination(result.Total, Query.PageNum, Query.PerPage);
  87. }
  88. public async Task<IActionResult> OnPostDeleteAsync(int[] ids, CancellationToken ct)
  89. {
  90. try
  91. {
  92. await mediator.Send(new DeletePostImage.Command(ids), ct);
  93. TempData["SuccessMessage"] = $"{ids.Length}건이 삭제되었습니다.";
  94. }
  95. catch (Exception e)
  96. {
  97. TempData["ErrorMessages"] = e.Message;
  98. }
  99. return RedirectToPage("/Forum/Attachments/PostImage/Index", Query);
  100. }
  101. public async Task<IActionResult> OnPostDisableAsync(int[] ids, CancellationToken ct)
  102. {
  103. try
  104. {
  105. await mediator.Send(new ToggleDisablePostImage.Command(ids, true), ct);
  106. TempData["SuccessMessage"] = $"{ids.Length}건이 비활성화되었습니다.";
  107. }
  108. catch (Exception e)
  109. {
  110. TempData["ErrorMessages"] = e.Message;
  111. }
  112. return RedirectToPage("/Forum/Attachments/PostImage/Index", Query);
  113. }
  114. public async Task<IActionResult> OnPostEnableAsync(int[] ids, CancellationToken ct)
  115. {
  116. try
  117. {
  118. await mediator.Send(new ToggleDisablePostImage.Command(ids, false), ct);
  119. TempData["SuccessMessage"] = $"{ids.Length}건이 활성화되었습니다.";
  120. }
  121. catch (Exception e)
  122. {
  123. TempData["ErrorMessages"] = e.Message;
  124. }
  125. return RedirectToPage("/Forum/Attachments/PostImage/Index", Query);
  126. }
  127. }