using Application.Features.Director.CreateRole; using Application.Features.Director.DeleteRole; using Application.Features.Director.GetRoles; using MediatR; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using System.ComponentModel; using System.ComponentModel.DataAnnotations; namespace Admin.Pages.Director.Role { public class IndexModel(IMediator mediator) : PageModel { public int Total { get; set; } = 0; public List List { get; set; } = []; [Required] [DisplayName("역할(Role)")] [MaxLength(100)] [BindProperty] public string? RoleName { get; set; } public async Task OnGetAsync(CancellationToken ct) { List = await mediator.Send(new GetAllRolesQuery(), ct); Total = List.Count; } public async Task OnPostAsync(CancellationToken ct) { try { if (!ModelState.IsValid) { throw new Exception("유효성 검사에 실패하였습니다."); } var command = new CreateRoleCommand( RoleName: RoleName ); await mediator.Send(command, ct); TempData["SuccessMessage"] = $"{RoleName} 역할이 추가되었습니다."; return RedirectToPage(); } catch (Exception e) { TempData["ErrorMessages"] = e.Message; return Page(); } } public async Task OnPostDeleteAsync(string id, CancellationToken ct) { try { await mediator.Send(new DeleteRoleCommand(id), ct); TempData["SuccessMessage"] = "정상적으로 삭제되었습니다."; return RedirectToPage(); } catch (Exception e) { TempData["ErrorMessages"] = e.Message; return RedirectToPage(); } } } }