Index.cshtml.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using Application.Features.Director.CreateRole;
  2. using Application.Features.Director.DeleteRole;
  3. using Application.Features.Director.GetRoles;
  4. using MediatR;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.AspNetCore.Mvc.RazorPages;
  7. using System.ComponentModel;
  8. using System.ComponentModel.DataAnnotations;
  9. namespace Admin.Pages.Director.Role
  10. {
  11. public class IndexModel(IMediator mediator) : PageModel
  12. {
  13. public int Total { get; set; } = 0;
  14. public List<Response> List { get; set; } = [];
  15. [Required]
  16. [DisplayName("역할(Role)")]
  17. [MaxLength(100)]
  18. [BindProperty]
  19. public string? RoleName { get; set; }
  20. public async Task OnGetAsync(CancellationToken ct)
  21. {
  22. List = await mediator.Send(new GetAllRolesQuery(), ct);
  23. Total = List.Count;
  24. }
  25. public async Task<IActionResult> OnPostAsync(CancellationToken ct)
  26. {
  27. try
  28. {
  29. if (!ModelState.IsValid)
  30. {
  31. throw new Exception("유효성 검사에 실패하였습니다.");
  32. }
  33. var command = new CreateRoleCommand(
  34. RoleName: RoleName
  35. );
  36. await mediator.Send(command, ct);
  37. TempData["SuccessMessage"] = $"{RoleName} 역할이 추가되었습니다.";
  38. return RedirectToPage();
  39. }
  40. catch (Exception e)
  41. {
  42. TempData["ErrorMessages"] = e.Message;
  43. return Page();
  44. }
  45. }
  46. public async Task<IActionResult> OnPostDeleteAsync(string id, CancellationToken ct)
  47. {
  48. try
  49. {
  50. await mediator.Send(new DeleteRoleCommand(id), ct);
  51. TempData["SuccessMessage"] = "정상적으로 삭제되었습니다.";
  52. return RedirectToPage();
  53. }
  54. catch (Exception e)
  55. {
  56. TempData["ErrorMessages"] = e.Message;
  57. return RedirectToPage();
  58. }
  59. }
  60. }
  61. }