Edit.cshtml.cs 2.1 KB

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