Group.cshtml.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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.Forum.Board
  7. {
  8. public class GroupModel(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. short Boards,
  19. int Posts,
  20. int Comments,
  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(30)]
  32. public required string Code { get; set; }
  33. [Required]
  34. [StringLength(255)]
  35. public required string Name { get; set; }
  36. [Range(-999, 999)]
  37. public short Order { get; set; }
  38. }
  39. public async Task OnGetAsync(CancellationToken ct)
  40. {
  41. if (!ModelState.IsValid)
  42. {
  43. return;
  44. }
  45. var result = await mediator.Send(new GetBoardGroups.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.Boards,
  55. c.Posts,
  56. c.Comments,
  57. c.UpdatedAt.GetDateAt() ?? "-",
  58. c.CreatedAt.GetDateAt()
  59. ))];
  60. Data = [..result.List.Select(x => new InputModel
  61. {
  62. ID = x.ID,
  63. Code = x.Code,
  64. Name = x.Name,
  65. Order = x.Order
  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 SaveBoardGroups.Command(
  77. [..Input.Select(x => new SaveBoardGroups.Command.Row(
  78. x.ID,
  79. x.Code,
  80. x.Name,
  81. x.Order
  82. ))]
  83. );
  84. var response = await mediator.Send(cmd, ct);
  85. TempData["SuccessMessage"] = $"저장 완료 (추가: {response.Inserted}, 수정: {response.Updated}, 삭제: {response.Deleted})";
  86. }
  87. catch (Exception e)
  88. {
  89. TempData["ErrorMessages"] = e.Message;
  90. }
  91. return RedirectToPage("/Forum/Board/Group");
  92. }
  93. }
  94. }