Index.cshtml.cs 4.9 KB

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