Category.cshtml.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using SharedKernel.Extensions;
  2. using MediatR;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.AspNetCore.Mvc.RazorPages;
  5. using System.ComponentModel.DataAnnotations;
  6. namespace Admin.Pages.Crypto
  7. {
  8. public class CategoryModel(IMediator mediator) : PageModel
  9. {
  10. public int Total { get; private set; } = 0;
  11. public List<(
  12. int Num,
  13. int ID,
  14. int Index,
  15. string Code,
  16. string Name,
  17. short Order,
  18. char IsActive,
  19. int CoinCount,
  20. string? UpdatedAt,
  21. string CreatedAt
  22. )> List { get; set; } = [];
  23. [BindProperty(Name = "request")]
  24. public List<InputModel> Input { get; private set; } = [];
  25. public List<InputModel> Data { get; private set; } = [];
  26. public sealed class InputModel
  27. {
  28. public int? ID { get; set; }
  29. [Required]
  30. [StringLength(30)]
  31. public required string Code { get; set; }
  32. [Required]
  33. [StringLength(100)]
  34. public required string Name { get; set; }
  35. [Range(-9999, 9999)]
  36. public short Order { get; set; }
  37. public bool IsActive { get; set; } = false;
  38. }
  39. public async Task OnGetAsync(CancellationToken ct)
  40. {
  41. if (!ModelState.IsValid)
  42. {
  43. return;
  44. }
  45. var result = await mediator.Send(new GetCryptoCategories.Query(), ct);
  46. Total = result.Total;
  47. List = [..result.List.Select(c => (
  48. c.Num,
  49. c.ID,
  50. c.Index,
  51. c.Code,
  52. c.Name,
  53. c.Order,
  54. c.IsActive ? 'Y' : 'N',
  55. (int)c.CoinCount,
  56. c.UpdatedAt.GetDateAt() ?? "-",
  57. c.CreatedAt.GetDateAt()
  58. ))];
  59. Data = [..result.List.Select(x => new InputModel
  60. {
  61. ID = x.ID,
  62. Code = x.Code,
  63. Name = x.Name,
  64. Order = x.Order,
  65. IsActive = x.IsActive
  66. })];
  67. }
  68. public async Task<IActionResult> OnPostAsync(CancellationToken ct)
  69. {
  70. try
  71. {
  72. if (!ModelState.IsValid)
  73. {
  74. throw new Exception();
  75. }
  76. var cmd = new SaveCryptoCategories.Command(
  77. [..Input.Select(x => new SaveCryptoCategories.Command.Row(
  78. x.ID,
  79. x.Code,
  80. x.Name,
  81. x.Order,
  82. x.IsActive
  83. ))]
  84. );
  85. var response = await mediator.Send(cmd, ct);
  86. TempData["SuccessMessage"] = $"저장 완료 (추가: {response.Inserted}, 수정: {response.Updated}, 삭제: {response.Deleted})";
  87. }
  88. catch (Exception e)
  89. {
  90. TempData["ErrorMessages"] = e.Message;
  91. }
  92. return RedirectToPage("/Crypto/Category");
  93. }
  94. }
  95. }