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