Index.cshtml.cs 1.9 KB

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