| | 1 | | using System.Security.Claims; |
| | 2 | | using LGDXRobotCloud.API.Services.Identity; |
| | 3 | | using LGDXRobotCloud.Data.Models.Business.Administration; |
| | 4 | | using LGDXRobotCloud.Data.Models.DTOs.V1.Commands; |
| | 5 | | using LGDXRobotCloud.Data.Models.DTOs.V1.Requests; |
| | 6 | | using LGDXRobotCloud.Data.Models.DTOs.V1.Responses; |
| | 7 | | using Microsoft.AspNetCore.Authentication.JwtBearer; |
| | 8 | | using Microsoft.AspNetCore.Authorization; |
| | 9 | | using Microsoft.AspNetCore.Mvc; |
| | 10 | |
|
| | 11 | | namespace LGDXRobotCloud.API.Areas.Identity.Controllers; |
| | 12 | |
|
| | 13 | | [ApiController] |
| | 14 | | [Area("Identity")] |
| | 15 | | [Route("[area]/[controller]")] |
| | 16 | | [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] |
| 0 | 17 | | public sealed class UserController( |
| 0 | 18 | | IAuthService authService, |
| 0 | 19 | | ICurrentUserService currentUserService |
| 0 | 20 | | ) : ControllerBase |
| | 21 | | { |
| 0 | 22 | | private readonly IAuthService _authService = authService ?? throw new ArgumentNullException(nameof(authService)); |
| 0 | 23 | | private readonly ICurrentUserService _currentUserService = currentUserService ?? throw new ArgumentNullException(nameo |
| | 24 | |
|
| | 25 | | [HttpGet("")] |
| | 26 | | [ProducesResponseType(typeof(LgdxUserDto), StatusCodes.Status200OK)] |
| | 27 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | 28 | | public async Task<ActionResult<LgdxUserDto>> GetUser() |
| 0 | 29 | | { |
| 0 | 30 | | var userId = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value; |
| 0 | 31 | | var user = await _currentUserService.GetUserAsync(userId!); |
| 0 | 32 | | return Ok(user.ToDto()); |
| 0 | 33 | | } |
| | 34 | |
|
| | 35 | | [HttpPut("")] |
| | 36 | | [ProducesResponseType(typeof(LgdxUserDto), StatusCodes.Status204NoContent)] |
| | 37 | | [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] |
| | 38 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | 39 | | public async Task<ActionResult> UpdateUser(LgdxUserUpdateDto lgdxUserUpdateDto) |
| 0 | 40 | | { |
| 0 | 41 | | var userId = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value; |
| 0 | 42 | | await _currentUserService.UpdateUserAsync(userId!, lgdxUserUpdateDto.ToBusinessModel()); |
| 0 | 43 | | return NoContent(); |
| 0 | 44 | | } |
| | 45 | |
|
| | 46 | | [HttpPost("Password")] |
| | 47 | | [ProducesResponseType(typeof(LgdxUserDto), StatusCodes.Status204NoContent)] |
| | 48 | | [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] |
| | 49 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | 50 | | public async Task<ActionResult> UpdatePassword(UpdatePasswordRequestDto updatePasswordRequestDto) |
| 0 | 51 | | { |
| 0 | 52 | | var userId = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value; |
| 0 | 53 | | await _authService.UpdatePasswordAsync(userId!, updatePasswordRequestDto.ToBusinessModel()); |
| 0 | 54 | | return NoContent(); |
| 0 | 55 | | } |
| | 56 | | } |