Group.cshtml.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. namespace Admin.Pages.Forum.Board;
  7. public class GroupModel(IMediator mediator) : PageModel
  8. {
  9. public int Total { get; private set; } = 0;
  10. public List<(
  11. int Num,
  12. int ID,
  13. int Index,
  14. string Code,
  15. string Name,
  16. short Order,
  17. short Boards,
  18. int Posts,
  19. int Comments,
  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(255)]
  34. public required string Name { get; set; }
  35. [Range(-999, 999)]
  36. public short Order { get; set; }
  37. }
  38. public async Task OnGetAsync(CancellationToken ct)
  39. {
  40. if (!ModelState.IsValid)
  41. {
  42. return;
  43. }
  44. var result = await mediator.Send(new GetBoardGroups.Query(), ct);
  45. Total = result.Total;
  46. List = [..result.List.Select(c => (
  47. c.Num,
  48. c.ID,
  49. c.Index,
  50. c.Code,
  51. c.Name,
  52. c.Order,
  53. c.Boards,
  54. c.Posts,
  55. c.Comments,
  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. })];
  66. }
  67. public async Task<IActionResult> OnPostAsync(CancellationToken ct)
  68. {
  69. if (!ModelState.IsValid)
  70. {
  71. TempData["ErrorMessages"] = ModelState.GetErrorMessages();
  72. return RedirectToPage("/Forum/Board/Group");
  73. }
  74. var cmd = new SaveBoardGroups.Command(
  75. [..Input.Select(x => new SaveBoardGroups.Command.Row(
  76. x.ID,
  77. x.Code,
  78. x.Name,
  79. x.Order
  80. ))]
  81. );
  82. var result = await mediator.Send(cmd, ct);
  83. if (result.IsFailure)
  84. {
  85. TempData["ErrorMessages"] = result.Error.Description;
  86. }
  87. else
  88. {
  89. var response = result.Value;
  90. TempData["SuccessMessage"] = $"저장 완료 (추가: {response.Inserted}, 수정: {response.Updated}, 삭제: {response.Deleted})";
  91. }
  92. return RedirectToPage("/Forum/Board/Group");
  93. }
  94. }