Edit.cshtml.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using MediatR;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.AspNetCore.Mvc.RazorPages;
  4. namespace Admin.Pages.Director.User
  5. {
  6. public class EditModel(IMediator mediator) : PageModel
  7. {
  8. [BindProperty]
  9. public GetUser.Response? Input { get; set; }
  10. public async Task OnGetAsync(string id, CancellationToken ct)
  11. {
  12. var user = await mediator.Send(new GetUser.Query(id), ct);
  13. if (user != null)
  14. {
  15. Input = new GetUser.Response
  16. {
  17. ID = user.ID,
  18. Name = user.FullName,
  19. Email = user.Email,
  20. Phone = user.PhoneNumber,
  21. IsDeleted = user.IsDeleted,
  22. EmailConfirmed = user.EmailConfirmed,
  23. LockoutEnd = user.LockoutEnd.HasValue,
  24. Roles = user.Roles
  25. };
  26. }
  27. }
  28. public async Task<IActionResult> OnPostAsync(CancellationToken ct)
  29. {
  30. try
  31. {
  32. if (!ModelState.IsValid)
  33. {
  34. throw new Exception("유효성 검사에 실패하였습니다.");
  35. }
  36. var command = new UpdateUser.Command(
  37. ID: Input!.ID,
  38. FullName: Input.Name,
  39. Email: Input.Email,
  40. NewPassword: Input.NewPassword,
  41. ConfirmPassword: Input.ConfirmPassword,
  42. PhoneNumber: Input.Phone,
  43. IsDeleted: Input.IsDeleted,
  44. EmailConfirmed: Input.EmailConfirmed,
  45. LockoutEnd: Input.LockoutEnd
  46. );
  47. await mediator.Send(command, ct);
  48. TempData["SuccessMessage"] = "사용자 정보가 정상적으로 수정되었습니다.";
  49. return RedirectToPage(command);
  50. }
  51. catch (Exception e)
  52. {
  53. TempData["ErrorMessages"] = e.Message;
  54. return Page();
  55. }
  56. }
  57. }
  58. }