Advertisement
Tark_Wight

DocCont

Jan 7th, 2024 (edited)
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.43 KB | None | 0 0
  1. using System.Security.Claims;
  2. using Microsoft.AspNetCore.Authentication;
  3. using Microsoft.AspNetCore.Authorization;
  4. using Microsoft.AspNetCore.Mvc;
  5. using WebNET.Source.DTO;
  6. using WebNET.Source.Service;
  7. using WebNET.Source.Service.Interface;
  8.  
  9. [ApiController]
  10. [Route("[controller]")]
  11. public class DoctorController : ControllerBase
  12. {
  13. private readonly IDoctorService _doctorService;
  14.  
  15. public DoctorController(IDoctorService authService)
  16. {
  17. _doctorService = authService;
  18. }
  19.  
  20. [HttpPost("register")]
  21. public async Task<IActionResult> Register([FromBody] DoctorDTO doctorDTO)
  22. {
  23. try
  24. {
  25. TokenDTO token = await _doctorService.Registration(doctorDTO);
  26. return Ok(token);
  27. }
  28. catch (Exception ex)
  29. {
  30. return BadRequest(ex.Message);
  31. }
  32. }
  33.  
  34. [HttpPost("login")]
  35. public async Task<IActionResult> Login([FromBody] LoginDTO loginDTO)
  36. {
  37. try
  38. {
  39. TokenDTO token = await _doctorService.Login(loginDTO);
  40. return Ok(token);
  41. }
  42. catch (Exception ex)
  43. {
  44. return ex.Data.Contains(StatusCodes.Status400BadRequest.ToString())
  45. ? BadRequest(ex.Data[StatusCodes.Status400BadRequest.ToString()])
  46. : (IActionResult)BadRequest(ex.Message);
  47. }
  48. }
  49.  
  50. [Authorize]
  51. [HttpPost("logout")]
  52. public async Task<IActionResult> Logout()
  53. {
  54. // Получение токена из контекста HTTP
  55. var token = await HttpContext.GetTokenAsync("access_token");
  56. DoctorService.ServiceResult result = await _doctorService.LogoutAsync(token);
  57.  
  58. if (result.Success)
  59. {
  60. return Ok(result.Message);
  61. }
  62. else
  63. {
  64. return StatusCode(result.StatusCode, result.Message);
  65. }
  66. }
  67.  
  68. [Authorize]
  69. [HttpGet("profile")]
  70. public async Task<IActionResult> GetProfile()
  71. {
  72. try
  73. {
  74.  
  75. if (User.Identity is not ClaimsIdentity identity)
  76. {
  77. return Unauthorized("User is not authenticated.");
  78. }
  79.  
  80. var userIdClaim = identity.FindFirst(ClaimTypes.NameIdentifier)?.Value;
  81. if (string.IsNullOrEmpty(userIdClaim))
  82. {
  83. return Unauthorized("User ID is missing in the token.");
  84. }
  85.  
  86. var userId = Guid.Parse(userIdClaim);
  87.  
  88.  
  89. var token = Request.Headers["Authorization"].FirstOrDefault()?.Split(" ").Last();
  90. if (string.IsNullOrEmpty(token))
  91. {
  92. return Unauthorized("Token is required.");
  93. }
  94.  
  95. var profile = await _doctorService.GetProfileAsync(userId, token);
  96. return Ok(profile);
  97. }
  98. catch (KeyNotFoundException)
  99. {
  100. return NotFound("User not found.");
  101. }
  102. catch (InvalidOperationException ex)
  103. {
  104. return Unauthorized(ex.Message);
  105. }
  106. catch (Exception ex)
  107. {
  108. return StatusCode(500, "Internal server error: " + ex.Message);
  109. }
  110. }
  111.  
  112. [Authorize]
  113. [HttpPut("Update")]
  114. public async Task<IActionResult> UpdateProfile([FromBody] UpdateProfileDTO updateProfileDTO)
  115. {
  116. try
  117. {
  118. var token = Request.Headers["Authorization"].FirstOrDefault()?.Split(" ").Last();
  119. if (string.IsNullOrEmpty(token))
  120. {
  121. return Unauthorized("Token is required.");
  122. }
  123.  
  124. var claimsIdentity = User.Identity as ClaimsIdentity;
  125. var userIdClaim = claimsIdentity?.FindFirst(ClaimTypes.NameIdentifier)?.Value;
  126. if (string.IsNullOrEmpty(userIdClaim))
  127. {
  128. return Unauthorized("User ID is missing in the token.");
  129. }
  130.  
  131. var userId = Guid.Parse(userIdClaim);
  132. await _doctorService.UpdateProfileAsync(userId, updateProfileDTO, token);
  133. return Ok("Profile updated successfully.");
  134. }
  135. catch (InvalidOperationException)
  136. {
  137. return Unauthorized("User is logged out.");
  138. }
  139. catch (KeyNotFoundException)
  140. {
  141. return NotFound("User not found.");
  142. }
  143. catch (Exception ex)
  144. {
  145. return StatusCode(500, "Internal server error: " + ex.Message);
  146. }
  147. }
  148. }
  149.  
  150.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement