Index.cshtml.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using SharedKernel.Extensions;
  2. using SharedKernel.Helpers;
  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.Posts.List;
  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? BoardPrefixID { 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. public int? Sort { get; set; }
  23. public bool? IsNotice { get; set; }
  24. public bool? IsSecret { get; set; }
  25. public bool? IsReply { get; set; }
  26. public bool? IsDeleted { get; set; }
  27. public bool? IsQnA { get; set; }
  28. [Range(1, int.MaxValue)]
  29. [DisplayName("페이지 번호")]
  30. public int PageNum { get; set; } = 1;
  31. [Range(1, 100)]
  32. [DisplayName("페이지 목록 수")]
  33. public ushort PerPage { get; set; } = 20;
  34. }
  35. public int Total { get; set; }
  36. public List<SelectListItem> BoardList { get; set; } = [];
  37. public List<(
  38. int No,
  39. int ID,
  40. int BoardID,
  41. string BoardName,
  42. string? BoardPrefixName,
  43. int? BoardPrefixID,
  44. string Subject,
  45. string? Thumbnail,
  46. string? Name,
  47. string? SID,
  48. bool IsNotice,
  49. bool IsSecret,
  50. bool IsQnA,
  51. bool IsReply,
  52. bool IsSpeaker,
  53. bool IsAnonymous,
  54. bool IsActive,
  55. bool IsDeleted,
  56. int Views,
  57. int Likes,
  58. int Dislikes,
  59. int Comments,
  60. byte Images,
  61. byte Medias,
  62. byte Files,
  63. byte Tags,
  64. string? UpdatedAt,
  65. string CreatedAt,
  66. string ViewURL
  67. )> Data { get; set; } = [];
  68. public Pagination? Pagination { get; set; }
  69. public async Task OnGetAsync(CancellationToken ct)
  70. {
  71. if (!ModelState.IsValid)
  72. {
  73. return;
  74. }
  75. // 게시판 목록 조회
  76. var boards = await mediator.Send(new SearchBoards.Query(null, null, 1, 500), ct);
  77. BoardList = [..boards.List.Select(c => new SelectListItem
  78. {
  79. Value = c.ID.ToString(),
  80. Text = $"[{c.BoardGroupName}] {c.Name}"
  81. })];
  82. var result = await mediator.Send(new SearchPosts.Query(
  83. Query.BoardID,
  84. Query.BoardPrefixID,
  85. Query.Search,
  86. Query.Keyword,
  87. Query.StartAt,
  88. Query.EndAt,
  89. Query.Sort,
  90. Query.IsNotice,
  91. Query.IsSecret,
  92. Query.IsReply,
  93. Query.IsDeleted,
  94. Query.IsQnA,
  95. Query.PageNum,
  96. Query.PerPage
  97. ), ct);
  98. Total = result.Total;
  99. Data = [..result.List.Select(c => (
  100. c.Num,
  101. c.ID,
  102. c.BoardID,
  103. c.BoardName,
  104. c.BoardPrefixName,
  105. c.BoardPrefixID,
  106. c.Subject,
  107. c.Thumbnail,
  108. c.Name,
  109. c.SID,
  110. c.IsNotice,
  111. c.IsSecret,
  112. c.IsQnA,
  113. c.IsReply,
  114. c.IsSpeaker,
  115. c.IsAnonymous,
  116. IsActive: !c.IsDeleted,
  117. c.IsDeleted,
  118. c.Views,
  119. c.Likes,
  120. c.Dislikes,
  121. c.Comments,
  122. c.Images,
  123. c.Medias,
  124. c.Files,
  125. c.Tags,
  126. c.UpdatedAt.GetDateAt() ?? "-",
  127. c.CreatedAt.GetDateAt(),
  128. ViewURL: $"/Forum/Posts/List/View/{c.ID}{Request.QueryString}"
  129. ))];
  130. Pagination = new Pagination(result.Total, Query.PageNum, Query.PerPage);
  131. }
  132. public async Task<IActionResult> OnPostDeleteAsync(int[] ids, CancellationToken ct)
  133. {
  134. try
  135. {
  136. await mediator.Send(new DeletePost.Command(ids), ct);
  137. TempData["SuccessMessage"] = $"{ids.Length}건이 삭제되었습니다.";
  138. }
  139. catch (Exception e)
  140. {
  141. TempData["ErrorMessages"] = e.Message;
  142. }
  143. return RedirectToPage("/Forum/Posts/List/Index", Query);
  144. }
  145. }