Index.cshtml.cs 2.6 KB

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