GameGenre.cshtml.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using SharedKernel.Extensions;
  2. using Application.Abstractions.Messaging;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.AspNetCore.Mvc.RazorPages;
  5. using System.ComponentModel.DataAnnotations;
  6. using GetGameGenres = Application.Features.Admin.Store.GameGenre.GetAll;
  7. using SaveGameGenres = Application.Features.Admin.Store.GameGenre.Save;
  8. namespace Admin.Pages.Store;
  9. public class GameGenreModel(IMediator mediator) : PageModel
  10. {
  11. public int Total { get; private set; }
  12. public List<(
  13. int Num,
  14. int ID,
  15. int Index,
  16. string KorName,
  17. string? EngName,
  18. int Order,
  19. char IsActive,
  20. int GameCount,
  21. string? UpdatedAt,
  22. string CreatedAt
  23. )> List { get; set; } = [];
  24. [BindProperty(Name = "request")]
  25. public List<InputModel> Input { get; private set; } = [];
  26. public List<InputModel> Data { get; private set; } = [];
  27. public sealed class InputModel
  28. {
  29. public int? ID { get; set; }
  30. [Required]
  31. [StringLength(100)]
  32. public required string KorName { get; set; }
  33. [StringLength(100)]
  34. public string? EngName { get; set; }
  35. [Range(-9999, 9999)]
  36. public int 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 GetGameGenres.Query(), ct);
  46. Total = result.Total;
  47. List = [.. result.List.Select(c => (
  48. c.Num,
  49. c.ID,
  50. c.Index,
  51. c.KorName,
  52. c.EngName,
  53. c.Order,
  54. c.IsActive ? 'Y' : 'N',
  55. c.GameCount,
  56. c.UpdatedAt.GetDateAt() ?? "-",
  57. c.CreatedAt.GetDateAt()
  58. ))];
  59. Data = [.. result.List.Select(x => new InputModel
  60. {
  61. ID = x.ID,
  62. KorName = x.KorName,
  63. EngName = x.EngName,
  64. Order = x.Order,
  65. IsActive = x.IsActive
  66. })];
  67. }
  68. public async Task<IActionResult> OnPostAsync(CancellationToken ct)
  69. {
  70. if (!ModelState.IsValid)
  71. {
  72. TempData["ErrorMessages"] = ModelState.GetErrorMessages();
  73. return RedirectToPage("/Store/GameGenre");
  74. }
  75. var cmd = new SaveGameGenres.Command(
  76. [.. Input.Select(x => new SaveGameGenres.Command.Row(
  77. x.ID,
  78. x.KorName,
  79. x.EngName,
  80. x.Order,
  81. x.IsActive
  82. ))]
  83. );
  84. var result = await mediator.Send(cmd, ct);
  85. if (result.IsFailure)
  86. {
  87. TempData["ErrorMessages"] = result.Error.Description;
  88. }
  89. else
  90. {
  91. var response = result.Value;
  92. TempData["SuccessMessage"] = $"저장 완료 (추가: {response.Inserted}, 수정: {response.Updated}, 삭제: {response.Deleted})";
  93. }
  94. return RedirectToPage("/Store/GameGenre");
  95. }
  96. }