| | 1 | | using LGDXRobotCloud.API.Authorisation; |
| | 2 | | using LGDXRobotCloud.API.Configurations; |
| | 3 | | using LGDXRobotCloud.API.Services.Administration; |
| | 4 | | using LGDXRobotCloud.Data.Models.Business.Administration; |
| | 5 | | using LGDXRobotCloud.Data.Models.DTOs.V1.Commands; |
| | 6 | | using LGDXRobotCloud.Data.Models.DTOs.V1.Responses; |
| | 7 | | using LGDXRobotCloud.Utilities.Constants; |
| | 8 | | using Microsoft.AspNetCore.Authentication.JwtBearer; |
| | 9 | | using Microsoft.AspNetCore.Authorization; |
| | 10 | | using Microsoft.AspNetCore.Mvc; |
| | 11 | | using Microsoft.Extensions.Options; |
| | 12 | | using System.Security.Claims; |
| | 13 | | using System.Text.Json; |
| | 14 | |
|
| | 15 | | namespace LGDXRobotCloud.API.Areas.Administration.Controllers; |
| | 16 | |
|
| | 17 | | [ApiController] |
| | 18 | | [Area("Administration")] |
| | 19 | | [Route("[area]/[controller]")] |
| | 20 | | [Authorize(AuthenticationSchemes = LgdxRobotCloudAuthenticationSchemes.ApiKeyOrCertificationScheme)] |
| | 21 | | [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] |
| | 22 | | [ValidateLgdxUserAccess] |
| 0 | 23 | | public class UsersController( |
| 0 | 24 | | IOptionsSnapshot<LgdxRobotCloudConfiguration> lgdxRobotCloudConfiguration, |
| 0 | 25 | | IUserService userService |
| 0 | 26 | | ) : ControllerBase |
| | 27 | | { |
| 0 | 28 | | private readonly LgdxRobotCloudConfiguration _lgdxRobotCloudConfiguration = lgdxRobotCloudConfiguration.Value ?? throw |
| 0 | 29 | | private readonly IUserService _userService = userService ?? throw new ArgumentNullException(nameof(userService)); |
| | 30 | |
|
| | 31 | | [HttpGet("")] |
| | 32 | | [ProducesResponseType(typeof(IEnumerable<LgdxUserListDto>), StatusCodes.Status200OK)] |
| | 33 | | public async Task<ActionResult<IEnumerable<LgdxUserListDto>>> GetUsers(string? name, int pageNumber = 1, int pageSize |
| 0 | 34 | | { |
| 0 | 35 | | pageSize = (pageSize > _lgdxRobotCloudConfiguration.ApiMaxPageSize) ? _lgdxRobotCloudConfiguration.ApiMaxPageSize : |
| 0 | 36 | | var (users, PaginationHelper) = await _userService.GetUsersAsync(name, pageNumber, pageSize); |
| 0 | 37 | | Response.Headers.Append("X-Pagination", JsonSerializer.Serialize(PaginationHelper)); |
| 0 | 38 | | return Ok(users.ToDto()); |
| 0 | 39 | | } |
| | 40 | |
|
| | 41 | | [HttpGet("{id}", Name = "GetUser")] |
| | 42 | | [ProducesResponseType(typeof(LgdxUserDto), StatusCodes.Status200OK)] |
| | 43 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | 44 | | public async Task<ActionResult<LgdxUserDto>> GetUser(Guid id) |
| 0 | 45 | | { |
| 0 | 46 | | var user = await _userService.GetUserAsync(id); |
| 0 | 47 | | return Ok(user.ToDto()); |
| 0 | 48 | | } |
| | 49 | |
|
| | 50 | | [HttpPost("")] |
| | 51 | | [ProducesResponseType(typeof(LgdxUserDto), StatusCodes.Status201Created)] |
| | 52 | | [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] |
| | 53 | | public async Task<ActionResult> CreateUser(LgdxUserCreateAdminDto lgdxUserCreateAdminDto) |
| 0 | 54 | | { |
| 0 | 55 | | var user = await _userService.CreateUserAsync(lgdxUserCreateAdminDto.ToBusinessModel()); |
| 0 | 56 | | return CreatedAtAction(nameof(GetUser), new { id = user.Id }, user.ToDto()); |
| 0 | 57 | | } |
| | 58 | |
|
| | 59 | | [HttpPut("{id}")] |
| | 60 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | 61 | | [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] |
| | 62 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | 63 | | public async Task<ActionResult> UpdateUser(Guid id, LgdxUserUpdateAdminDto lgdxUserUpdateAdminDto) |
| 0 | 64 | | { |
| 0 | 65 | | await _userService.UpdateUserAsync(id, lgdxUserUpdateAdminDto.ToBusinessModel()); |
| 0 | 66 | | return NoContent(); |
| 0 | 67 | | } |
| | 68 | |
|
| | 69 | | [HttpPatch("{id}/Unlock")] |
| | 70 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | 71 | | [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] |
| | 72 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | 73 | | public async Task<ActionResult> UnlockUser(Guid id) |
| 0 | 74 | | { |
| 0 | 75 | | await _userService.UnlockUserAsync(id); |
| 0 | 76 | | return NoContent(); |
| 0 | 77 | | } |
| | 78 | |
|
| | 79 | | [HttpDelete("{id}")] |
| | 80 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | 81 | | [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] |
| | 82 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | 83 | | public async Task<ActionResult> DeleteUser(Guid id) |
| 0 | 84 | | { |
| 0 | 85 | | var operatorId = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value; |
| 0 | 86 | | await _userService.DeleteUserAsync(id, operatorId!); |
| 0 | 87 | | return NoContent(); |
| 0 | 88 | | } |
| | 89 | | } |