Index.cshtml.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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.Comments.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? 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? IsDeleted { get; set; }
  24. [Range(1, int.MaxValue)]
  25. [DisplayName("페이지 번호")]
  26. public int PageNum { get; set; } = 1;
  27. [Range(1, 100)]
  28. [DisplayName("페이지 목록 수")]
  29. public ushort PerPage { get; set; } = 20;
  30. }
  31. public int Total { get; set; } = 0;
  32. public List<SelectListItem> BoardList { get; set; } = [];
  33. public List<(
  34. int Num,
  35. int ID,
  36. int BoardID,
  37. string BoardName,
  38. int PostID,
  39. string PostSubject,
  40. string Content,
  41. string? Name,
  42. string? SID,
  43. bool IsReply,
  44. bool IsSecret,
  45. bool IsDeleted,
  46. int Likes,
  47. int Dislikes,
  48. int Reports,
  49. int Replies,
  50. string? UpdatedAt,
  51. string CreatedAt
  52. )> List { get; set; } = [];
  53. public Pagination? Pagination { get; set; }
  54. public async Task OnGetAsync(CancellationToken ct)
  55. {
  56. if (!ModelState.IsValid)
  57. {
  58. return;
  59. }
  60. var boards = await mediator.Send(new SearchBoards.Query(null, null, 1, 500), ct);
  61. BoardList = [..boards.List.Select(c => new SelectListItem
  62. {
  63. Value = c.ID.ToString(),
  64. Text = $"[{c.BoardGroupName}] {c.Name}"
  65. })];
  66. var result = await mediator.Send(new SearchComments.Query(
  67. Query.BoardID,
  68. null,
  69. Query.Search,
  70. Query.Keyword,
  71. Query.StartAt,
  72. Query.EndAt,
  73. Query.IsDeleted,
  74. Query.PageNum,
  75. Query.PerPage
  76. ), ct);
  77. Total = result.Total;
  78. List = [..result.List.Select(c => (
  79. c.Num,
  80. c.ID,
  81. c.BoardID,
  82. c.BoardName,
  83. c.PostID,
  84. c.PostSubject,
  85. c.Content,
  86. c.Name,
  87. c.SID,
  88. c.IsReply,
  89. c.IsSecret,
  90. c.IsDeleted,
  91. c.Likes,
  92. c.Dislikes,
  93. c.Reports,
  94. c.Replies,
  95. c.UpdatedAt.GetDateAt() ?? "-",
  96. c.CreatedAt.GetDateAt()
  97. ))];
  98. Pagination = new Pagination(result.Total, Query.PageNum, Query.PerPage);
  99. }
  100. public async Task<IActionResult> OnPostDeleteAsync(int[] ids, CancellationToken ct)
  101. {
  102. try
  103. {
  104. await mediator.Send(new DeleteComment.Command(ids), ct);
  105. TempData["SuccessMessage"] = $"{ids.Length}건이 삭제되었습니다.";
  106. }
  107. catch (Exception e)
  108. {
  109. TempData["ErrorMessages"] = e.Message;
  110. }
  111. return RedirectToPage("/Forum/Comments/List/Index", Query);
  112. }
  113. }
  114. }