Roles.cshtml.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using Application.Abstractions.Identity.Models;
  2. using MediatR;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.AspNetCore.Mvc.RazorPages;
  5. namespace Admin.Pages.Director.User
  6. {
  7. public class RolesModel(IMediator mediator) : PageModel
  8. {
  9. [BindProperty]
  10. public required GetUserRoles.Response Input { get; set; }
  11. public async Task<IActionResult> OnGetAsync(string id, CancellationToken ct)
  12. {
  13. if (string.IsNullOrEmpty(id))
  14. {
  15. TempData["ErrorMessages"] = "유효하지 않은 접근입니다.";
  16. return RedirectToPage("/Director/User/Index");
  17. }
  18. Input = await mediator.Send(new GetUserRoles.Query(id), ct);
  19. return Page();
  20. }
  21. public async Task<IActionResult> OnPostAsync(CancellationToken ct)
  22. {
  23. try
  24. {
  25. if (!ModelState.IsValid)
  26. {
  27. throw new Exception("유효성 검사에 실패하였습니다.");
  28. }
  29. var command = new UpdateUserRoles.Command(
  30. Input!.User.ID,
  31. new UserRolesDto
  32. {
  33. Roles = [.. (Input.Roles ?? []).Select(r => new UserRolesDto.Checkbox { DisplayValue = r.DisplayValue, IsSelected = r.IsSelected })]
  34. }
  35. );
  36. await mediator.Send(command, ct);
  37. TempData["SuccessMessage"] = "권한이 변경되었습니다.";
  38. return RedirectToPage(new { id = Input.User.ID });
  39. }
  40. catch (Exception e)
  41. {
  42. TempData["ErrorMessages"] = e.Message;
  43. return Page();
  44. }
  45. }
  46. }
  47. }