Index.cshtml.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using Domain.Entities.Store.ValueObject;
  2. using SharedKernel.Extensions;
  3. using SharedKernel.Helpers;
  4. using Application.Abstractions.Messaging;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.AspNetCore.Mvc.RazorPages;
  7. using System.ComponentModel;
  8. using System.ComponentModel.DataAnnotations;
  9. namespace Admin.Pages.Store.Product;
  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 Name)> Games { get; set; } = [];
  15. public sealed class QueryParams
  16. {
  17. [Range(1, int.MaxValue)]
  18. public int PageNum { get; set; } = 1;
  19. [Range(1, 100)]
  20. public ushort PerPage { get; set; } = 20;
  21. [DisplayName("검색 조건")]
  22. [Range(1, 2, ErrorMessage = "{0}이(가) 올바르지 않습니다.")]
  23. public int? Search { get; set; }
  24. [DisplayName("검색어")]
  25. [MaxLength(100)]
  26. public string? Keyword { get; set; }
  27. [DisplayName("시작일")]
  28. public string? StartAt { get; set; }
  29. [DisplayName("종료일")]
  30. public string? EndAt { get; set; }
  31. [DisplayName("게임")]
  32. public int? GameID { get; set; }
  33. [DisplayName("상품 유형")]
  34. public ProductType? Type { get; set; }
  35. [DisplayName("활성화")]
  36. public int? IsActive { get; set; }
  37. }
  38. public int Total { get; set; }
  39. public List<(
  40. int Num,
  41. int ID,
  42. int GameID,
  43. string GameName,
  44. string Name,
  45. string? Thumbnail,
  46. string TypeText,
  47. int Price,
  48. DiscountType DiscountType,
  49. int DiscountValue,
  50. int SalePrice,
  51. int Stock,
  52. char IsActive,
  53. int Order,
  54. string? SaleStartAt,
  55. string? SaleEndAt,
  56. string? UpdatedAt,
  57. string CreatedAt,
  58. string EditURL
  59. )> List { get; set; } = [];
  60. public Pagination? Pagination { get; set; }
  61. public async Task OnGetAsync(CancellationToken ct)
  62. {
  63. if (!ModelState.IsValid)
  64. {
  65. return;
  66. }
  67. // 게임 드롭다운 (필터용)
  68. var gamesResult = await mediator.Send(new GetGames.Query(false), ct);
  69. Games = [.. gamesResult.List.Select(g => (ID: g.ID, Name: g.KorName))];
  70. bool? isActive = Query.IsActive == 1 ? true : null;
  71. var result = await mediator.Send(new SearchProducts.Query(
  72. Query.Search,
  73. Query.Keyword,
  74. Query.StartAt,
  75. Query.EndAt,
  76. Query.GameID,
  77. Query.Type,
  78. isActive,
  79. Query.PageNum,
  80. Query.PerPage
  81. ), ct);
  82. Total = result.Total;
  83. var qs = Request.QueryString.ToString();
  84. List = [.. result.List.Select(c => (
  85. c.Num,
  86. c.ID,
  87. c.GameID,
  88. c.GameName,
  89. c.Name,
  90. c.Thumbnail,
  91. TypeText: c.Type == ProductType.Physical ? "실물" : "쿠폰",
  92. c.Price,
  93. c.DiscountType,
  94. c.DiscountValue,
  95. c.SalePrice,
  96. c.Stock,
  97. c.IsActive ? 'Y' : 'N',
  98. c.Order,
  99. c.SaleStartAt?.ToString("yyyy-MM-dd"),
  100. c.SaleEndAt?.ToString("yyyy-MM-dd"),
  101. c.UpdatedAt.GetDateAt() ?? "-",
  102. c.CreatedAt.GetDateAt(),
  103. EditURL: $"/Store/Product/Edit/{c.ID}{qs}"
  104. ))];
  105. Pagination = new Pagination(result.Total, Query.PageNum, Query.PerPage);
  106. }
  107. public async Task<IActionResult> OnPostDeleteAsync(int[] ids, CancellationToken ct)
  108. {
  109. try
  110. {
  111. if (ids is null || ids.Length == 0)
  112. {
  113. throw new Exception("삭제할 항목을 선택해주세요.");
  114. }
  115. var result = await mediator.Send(new DeleteProduct.Command(ids), ct);
  116. if (result.IsFailure)
  117. {
  118. throw new Exception(result.Error.Description);
  119. }
  120. TempData["SuccessMessage"] = $"{ids.Length}건이 삭제되었습니다.";
  121. }
  122. catch (Exception e)
  123. {
  124. TempData["ErrorMessages"] = e.Message;
  125. }
  126. return Redirect($"{Request.Path}{Request.QueryString}");
  127. }
  128. }