Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Security.Claims;
- using Microsoft.AspNetCore.Authentication;
- using Microsoft.AspNetCore.Authorization;
- using Microsoft.AspNetCore.Mvc;
- using WebNET.Source.DTO;
- using WebNET.Source.Service;
- using WebNET.Source.Service.Interface;
- [ApiController]
- [Route("[controller]")]
- public class DoctorController : ControllerBase
- {
- private readonly IDoctorService _doctorService;
- public DoctorController(IDoctorService authService)
- {
- _doctorService = authService;
- }
- [HttpPost("register")]
- public async Task<IActionResult> Register([FromBody] DoctorDTO doctorDTO)
- {
- try
- {
- TokenDTO token = await _doctorService.Registration(doctorDTO);
- return Ok(token);
- }
- catch (Exception ex)
- {
- return BadRequest(ex.Message);
- }
- }
- [HttpPost("login")]
- public async Task<IActionResult> Login([FromBody] LoginDTO loginDTO)
- {
- try
- {
- TokenDTO token = await _doctorService.Login(loginDTO);
- return Ok(token);
- }
- catch (Exception ex)
- {
- return ex.Data.Contains(StatusCodes.Status400BadRequest.ToString())
- ? BadRequest(ex.Data[StatusCodes.Status400BadRequest.ToString()])
- : (IActionResult)BadRequest(ex.Message);
- }
- }
- [Authorize]
- [HttpPost("logout")]
- public async Task<IActionResult> Logout()
- {
- // Получение токена из контекста HTTP
- var token = await HttpContext.GetTokenAsync("access_token");
- DoctorService.ServiceResult result = await _doctorService.LogoutAsync(token);
- if (result.Success)
- {
- return Ok(result.Message);
- }
- else
- {
- return StatusCode(result.StatusCode, result.Message);
- }
- }
- [Authorize]
- [HttpGet("profile")]
- public async Task<IActionResult> GetProfile()
- {
- try
- {
- if (User.Identity is not ClaimsIdentity identity)
- {
- return Unauthorized("User is not authenticated.");
- }
- var userIdClaim = identity.FindFirst(ClaimTypes.NameIdentifier)?.Value;
- if (string.IsNullOrEmpty(userIdClaim))
- {
- return Unauthorized("User ID is missing in the token.");
- }
- var userId = Guid.Parse(userIdClaim);
- var token = Request.Headers["Authorization"].FirstOrDefault()?.Split(" ").Last();
- if (string.IsNullOrEmpty(token))
- {
- return Unauthorized("Token is required.");
- }
- var profile = await _doctorService.GetProfileAsync(userId, token);
- return Ok(profile);
- }
- catch (KeyNotFoundException)
- {
- return NotFound("User not found.");
- }
- catch (InvalidOperationException ex)
- {
- return Unauthorized(ex.Message);
- }
- catch (Exception ex)
- {
- return StatusCode(500, "Internal server error: " + ex.Message);
- }
- }
- [Authorize]
- [HttpPut("Update")]
- public async Task<IActionResult> UpdateProfile([FromBody] UpdateProfileDTO updateProfileDTO)
- {
- try
- {
- var token = Request.Headers["Authorization"].FirstOrDefault()?.Split(" ").Last();
- if (string.IsNullOrEmpty(token))
- {
- return Unauthorized("Token is required.");
- }
- var claimsIdentity = User.Identity as ClaimsIdentity;
- var userIdClaim = claimsIdentity?.FindFirst(ClaimTypes.NameIdentifier)?.Value;
- if (string.IsNullOrEmpty(userIdClaim))
- {
- return Unauthorized("User ID is missing in the token.");
- }
- var userId = Guid.Parse(userIdClaim);
- await _doctorService.UpdateProfileAsync(userId, updateProfileDTO, token);
- return Ok("Profile updated successfully.");
- }
- catch (InvalidOperationException)
- {
- return Unauthorized("User is logged out.");
- }
- catch (KeyNotFoundException)
- {
- return NotFound("User not found.");
- }
- catch (Exception ex)
- {
- return StatusCode(500, "Internal server error: " + ex.Message);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement