Roles.cshtml.cs 1.8 KB

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