Roles.cshtml.cs 1.6 KB

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