| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- using SharedKernel.Extensions;
- using Application.Abstractions.Messaging;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.AspNetCore.Mvc.RazorPages;
- using System.ComponentModel;
- using System.ComponentModel.DataAnnotations;
- namespace Admin.Pages.NavMenu;
- public class EditModel(IMediator mediator) : PageModel
- {
- [BindProperty]
- public InputModel Input { get; set; } = new();
- public sealed class InputModel
- {
- [DisplayName("ID")]
- [Required(ErrorMessage = "{0}은(는) 필수입니다.")]
- public int ID { get; set; }
- [DisplayName("메뉴 명")]
- [DataType(DataType.Text)]
- [Required(ErrorMessage = "{0}은(는) 필수입니다.")]
- [StringLength(50, ErrorMessage = "{0}은(는) {1}자 이하로 입력하세요.")]
- public string Label { get; set; } = null!;
- [DisplayName("링크 주소")]
- [DataType(DataType.Text)]
- [Required(ErrorMessage = "{0}은(는) 필수입니다.")]
- [StringLength(200, ErrorMessage = "{0}은(는) {1}자 이하로 입력하세요.")]
- public string Href { get; set; } = null!;
- [DisplayName("순서")]
- [Required(ErrorMessage = "{0}은(는) 필수입니다.")]
- [Range(0, 9999, ErrorMessage = "{0} 값의 범위는 {1} ~ {2} 입니다.")]
- public int SortOrder { get; set; } = 0;
- [DisplayName("노출 여부")]
- public bool IsVisible { get; set; } = true;
- }
- public async Task<IActionResult> OnGetAsync(int id, CancellationToken ct)
- {
- var result = await mediator.Send(new Application.Features.Admin.NavMenu.Get.Query(id), ct);
- if (result.IsFailure)
- {
- TempData["ErrorMessages"] = result.Error.Description;
- return RedirectToPage("/NavMenu/Index");
- }
- var data = result.Value;
- Input = new InputModel
- {
- ID = data.ID,
- Label = data.Label,
- Href = data.Href,
- SortOrder = data.SortOrder,
- IsVisible = data.IsVisible
- };
- return Page();
- }
- public async Task<IActionResult> OnPostAsync(CancellationToken ct)
- {
- if (!ModelState.IsValid)
- {
- TempData["ErrorMessages"] = ModelState.GetErrorMessages();
- return Redirect($"/NavMenu/Edit/{Input.ID}");
- }
- var result = await mediator.Send(new Application.Features.Admin.NavMenu.Update.Command(
- Input.ID,
- Input.Label,
- Input.Href,
- Input.SortOrder,
- Input.IsVisible
- ), ct);
- if (result.IsFailure)
- {
- TempData["ErrorMessages"] = result.Error.Description;
- }
- else
- {
- TempData["SuccessMessage"] = $"{Input.Label} 메뉴가 수정되었습니다.";
- }
- return Redirect($"/NavMenu/Edit/{Input.ID}");
- }
- }
|