Index.cshtml.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using SharedKernel.Helpers;
  2. using MediatR;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.AspNetCore.Mvc.RazorPages;
  5. using System.ComponentModel;
  6. using System.ComponentModel.DataAnnotations;
  7. namespace Admin.Pages.Popup;
  8. public class IndexModel(IMediator mediator) : PageModel
  9. {
  10. [BindProperty(SupportsGet = true)]
  11. public QueryParams Query { get; set; } = new();
  12. public sealed class QueryParams
  13. {
  14. [Range(1, int.MaxValue)]
  15. [DisplayName("페이지 번호")]
  16. public int PageNum { get; set; } = 1;
  17. [Range(1, 100)]
  18. [DisplayName("페이지 당 수")]
  19. public ushort PerPage { get; set; } = 10;
  20. }
  21. public int Total { get; set; } = 0;
  22. public List<(
  23. int Num,
  24. int ID,
  25. string PositionCode,
  26. string PositionSubject,
  27. string Subject,
  28. string? Link,
  29. string? StartAt,
  30. string? EndAt,
  31. short Order,
  32. bool IsActive,
  33. string? UpdatedAt,
  34. string CreatedAt,
  35. string EditURL,
  36. string DeleteURL
  37. )> List { get; set; } = [];
  38. public Pagination? Pagination { get; set; }
  39. public async Task OnGetAsync(CancellationToken ct)
  40. {
  41. if (!ModelState.IsValid)
  42. {
  43. return;
  44. }
  45. var result = await mediator.Send(new SearchPopups.Query(Query.PageNum, Query.PerPage), ct);
  46. Total = result.Total;
  47. List = [.. result.List.Select(c => (
  48. c.Num,
  49. c.ID,
  50. c.PositionCode,
  51. c.PositionSubject,
  52. c.Subject,
  53. c.Link ?? "-",
  54. c.StartAt ?? "-",
  55. c.EndAt ?? "-",
  56. c.Order,
  57. c.IsActive,
  58. c.UpdatedAt,
  59. c.CreatedAt,
  60. EditURL: $"/Popup/Edit/{c.ID}{Request.QueryString}",
  61. DeleteURL: $"/Popup/Delete/{c.ID}{Request.QueryString}"
  62. ))];
  63. Pagination = new Pagination(result.Total, Query.PageNum, Query.PerPage);
  64. }
  65. public async Task<IActionResult> OnPostDeleteAsync(int[] ids, CancellationToken ct)
  66. {
  67. try
  68. {
  69. await mediator.Send(new DeletePopup.Command(ids), ct);
  70. TempData["SuccessMessage"] = $"{ids.Length}개 팝업이 삭제되었습니다.";
  71. }
  72. catch (Exception e)
  73. {
  74. TempData["ErrorMessages"] = e.Message;
  75. }
  76. return RedirectToPage("/Popup/Index", Query);
  77. }
  78. }