Edit.cshtml.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using SharedKernel.Extensions;
  2. using Application.Abstractions.Messaging;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.AspNetCore.Mvc.RazorPages;
  5. using System.ComponentModel;
  6. using System.ComponentModel.DataAnnotations;
  7. namespace Admin.Pages.NavMenu;
  8. public class EditModel(IMediator mediator) : PageModel
  9. {
  10. [BindProperty]
  11. public InputModel Input { get; set; } = new();
  12. public sealed class InputModel
  13. {
  14. [DisplayName("ID")]
  15. [Required(ErrorMessage = "{0}은(는) 필수입니다.")]
  16. public int ID { get; set; }
  17. [DisplayName("메뉴 명")]
  18. [DataType(DataType.Text)]
  19. [Required(ErrorMessage = "{0}은(는) 필수입니다.")]
  20. [StringLength(50, ErrorMessage = "{0}은(는) {1}자 이하로 입력하세요.")]
  21. public string Label { get; set; } = null!;
  22. [DisplayName("링크 주소")]
  23. [DataType(DataType.Text)]
  24. [Required(ErrorMessage = "{0}은(는) 필수입니다.")]
  25. [StringLength(200, ErrorMessage = "{0}은(는) {1}자 이하로 입력하세요.")]
  26. public string Href { get; set; } = null!;
  27. [DisplayName("순서")]
  28. [Required(ErrorMessage = "{0}은(는) 필수입니다.")]
  29. [Range(0, 9999, ErrorMessage = "{0} 값의 범위는 {1} ~ {2} 입니다.")]
  30. public int SortOrder { get; set; } = 0;
  31. [DisplayName("노출 여부")]
  32. public bool IsVisible { get; set; } = true;
  33. }
  34. public async Task<IActionResult> OnGetAsync(int id, CancellationToken ct)
  35. {
  36. var result = await mediator.Send(new Application.Features.Admin.NavMenu.Get.Query(id), ct);
  37. if (result.IsFailure)
  38. {
  39. TempData["ErrorMessages"] = result.Error.Description;
  40. return RedirectToPage("/NavMenu/Index");
  41. }
  42. var data = result.Value;
  43. Input = new InputModel
  44. {
  45. ID = data.ID,
  46. Label = data.Label,
  47. Href = data.Href,
  48. SortOrder = data.SortOrder,
  49. IsVisible = data.IsVisible
  50. };
  51. return Page();
  52. }
  53. public async Task<IActionResult> OnPostAsync(CancellationToken ct)
  54. {
  55. if (!ModelState.IsValid)
  56. {
  57. TempData["ErrorMessages"] = ModelState.GetErrorMessages();
  58. return Redirect($"/NavMenu/Edit/{Input.ID}");
  59. }
  60. var result = await mediator.Send(new Application.Features.Admin.NavMenu.Update.Command(
  61. Input.ID,
  62. Input.Label,
  63. Input.Href,
  64. Input.SortOrder,
  65. Input.IsVisible
  66. ), ct);
  67. if (result.IsFailure)
  68. {
  69. TempData["ErrorMessages"] = result.Error.Description;
  70. }
  71. else
  72. {
  73. TempData["SuccessMessage"] = $"{Input.Label} 메뉴가 수정되었습니다.";
  74. }
  75. return Redirect($"/NavMenu/Edit/{Input.ID}");
  76. }
  77. }