Index.cshtml.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using SharedKernel.Helpers;
  2. using SharedKernel.Extensions;
  3. using Application.Abstractions.Messaging;
  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.Attachments.PostFile;
  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? PostID { get; set; }
  18. public int? Search { get; set; }
  19. public string? Keyword { get; set; }
  20. public bool? IsDisabled { get; set; }
  21. public string? StartAt { get; set; }
  22. public string? EndAt { get; set; }
  23. [Range(1, int.MaxValue)]
  24. [DisplayName("페이지 번호")]
  25. public int PageNum { get; set; } = 1;
  26. [Range(1, 100)]
  27. [DisplayName("페이지 목록 수")]
  28. public ushort PerPage { get; set; } = 20;
  29. }
  30. public int Total { get; set; } = 0;
  31. public List<SelectListItem> BoardList { get; set; } = [];
  32. public List<(
  33. int Num,
  34. int ID,
  35. int BoardID,
  36. string BoardName,
  37. int PostID,
  38. string PostSubject,
  39. string FileName,
  40. string Url,
  41. string? Extension,
  42. long? Size,
  43. int Downloads,
  44. bool IsDisabled,
  45. string CreatedAt
  46. )> List { get; set; } = [];
  47. public Pagination? Pagination { get; set; }
  48. public async Task OnGetAsync(CancellationToken ct)
  49. {
  50. if (!ModelState.IsValid)
  51. {
  52. return;
  53. }
  54. var boards = await mediator.Send(new SearchBoards.Query(null, null, 1, 500), ct);
  55. BoardList = [..boards.List.Select(c => new SelectListItem
  56. {
  57. Value = c.ID.ToString(),
  58. Text = $"[{c.BoardGroupName}] {c.Name}"
  59. })];
  60. var result = await mediator.Send(new SearchPostFiles.Query(
  61. Query.BoardID, Query.PostID, Query.Search, Query.Keyword,
  62. Query.IsDisabled, Query.StartAt, Query.EndAt,
  63. Query.PageNum, Query.PerPage
  64. ), ct);
  65. Total = result.Total;
  66. List = [..result.List.Select(c => (
  67. c.Num, c.ID, c.BoardID, c.BoardName, c.PostID, c.PostSubject,
  68. c.FileName, c.Url, c.Extension, c.Size,
  69. c.Downloads, c.IsDisabled, c.CreatedAt.GetDateAt()
  70. ))];
  71. Pagination = new Pagination(result.Total, Query.PageNum, Query.PerPage);
  72. }
  73. public async Task<IActionResult> OnPostDeleteAsync(int[] ids, CancellationToken ct)
  74. {
  75. try
  76. {
  77. await mediator.Send(new DeletePostFile.Command(ids), ct);
  78. TempData["SuccessMessage"] = $"{ids.Length}건이 삭제되었습니다.";
  79. }
  80. catch (Exception e)
  81. {
  82. TempData["ErrorMessages"] = e.Message;
  83. }
  84. return RedirectToPage("/Forum/Attachments/PostFile/Index", Query);
  85. }
  86. public async Task<IActionResult> OnPostDisableAsync(int[] ids, CancellationToken ct)
  87. {
  88. try
  89. {
  90. await mediator.Send(new ToggleDisablePostFile.Command(ids, true), ct);
  91. TempData["SuccessMessage"] = $"{ids.Length}건이 비활성화되었습니다.";
  92. }
  93. catch (Exception e)
  94. {
  95. TempData["ErrorMessages"] = e.Message;
  96. }
  97. return RedirectToPage("/Forum/Attachments/PostFile/Index", Query);
  98. }
  99. public async Task<IActionResult> OnPostEnableAsync(int[] ids, CancellationToken ct)
  100. {
  101. try
  102. {
  103. await mediator.Send(new ToggleDisablePostFile.Command(ids, false), ct);
  104. TempData["SuccessMessage"] = $"{ids.Length}건이 활성화되었습니다.";
  105. }
  106. catch (Exception e)
  107. {
  108. TempData["ErrorMessages"] = e.Message;
  109. }
  110. return RedirectToPage("/Forum/Attachments/PostFile/Index", Query);
  111. }
  112. }