| | 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.Text.Json; |
| | 12 | |
|
| | 13 | | namespace LGDXRobotCloud.API.Areas.Administration.Controllers; |
| | 14 | |
|
| | 15 | | [ApiController] |
| | 16 | | [Area("Administration")] |
| | 17 | | [Route("[area]/[controller]")] |
| | 18 | | [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] |
| | 19 | | [ValidateLgdxUserAccess] |
| 0 | 20 | | public sealed class RolesController( |
| 0 | 21 | | IOptionsSnapshot<LgdxRobotCloudConfiguration> lgdxRobotCloudConfiguration, |
| 0 | 22 | | IRoleService roleService |
| 0 | 23 | | ) : ControllerBase |
| | 24 | | { |
| 0 | 25 | | private readonly LgdxRobotCloudConfiguration _lgdxRobotCloudConfiguration = lgdxRobotCloudConfiguration.Value ?? throw |
| 0 | 26 | | private readonly IRoleService _roleService = roleService ?? throw new ArgumentNullException(nameof(roleService)); |
| | 27 | |
|
| | 28 | | [HttpGet("")] |
| | 29 | | [ProducesResponseType(typeof(IEnumerable<LgdxRoleListDto>), StatusCodes.Status200OK)] |
| | 30 | | public async Task<ActionResult<IEnumerable<LgdxRoleListDto>>> GetRoles(string? name, int pageNumber = 1, int pageSize |
| 0 | 31 | | { |
| 0 | 32 | | pageSize = (pageSize > _lgdxRobotCloudConfiguration.ApiMaxPageSize) ? _lgdxRobotCloudConfiguration.ApiMaxPageSize : |
| 0 | 33 | | var (roles, PaginationHelper) = await _roleService.GetRolesAsync(name, pageNumber, pageSize); |
| 0 | 34 | | Response.Headers.Append("X-Pagination", JsonSerializer.Serialize(PaginationHelper)); |
| 0 | 35 | | return Ok(roles.ToDto()); |
| 0 | 36 | | } |
| | 37 | |
|
| | 38 | | [HttpGet("Search")] |
| | 39 | | [ProducesResponseType(typeof(IEnumerable<LgdxRoleSearchDto>), StatusCodes.Status200OK)] |
| | 40 | | public async Task<ActionResult<IEnumerable<LgdxRoleSearchDto>>> SearchRoles(string name) |
| 0 | 41 | | { |
| 0 | 42 | | var roles = await _roleService.SearchRoleAsync(name); |
| 0 | 43 | | return Ok(roles.ToDto()); |
| 0 | 44 | | } |
| | 45 | |
|
| | 46 | | [HttpGet("{id}", Name = "GetRole")] |
| | 47 | | [ProducesResponseType(typeof(LgdxRoleDto), StatusCodes.Status200OK)] |
| | 48 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | 49 | | public async Task<ActionResult<LgdxRoleDto>> GetRole(Guid id) |
| 0 | 50 | | { |
| 0 | 51 | | var role = await _roleService.GetRoleAsync(id); |
| 0 | 52 | | return Ok(role.ToDto()); |
| 0 | 53 | | } |
| | 54 | |
|
| | 55 | | [HttpPost("")] |
| | 56 | | [ProducesResponseType(StatusCodes.Status201Created)] |
| | 57 | | [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] |
| | 58 | | public async Task<ActionResult> CreateRole(LgdxRoleCreateDto lgdxRoleCreateDto) |
| 0 | 59 | | { |
| 0 | 60 | | var role = await _roleService.CreateRoleAsync(lgdxRoleCreateDto.ToBusinessModel()); |
| 0 | 61 | | return CreatedAtAction(nameof(GetRole), new { id = role.Id }, role.ToDto()); |
| 0 | 62 | | } |
| | 63 | |
|
| | 64 | | [HttpPut("{id}")] |
| | 65 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | 66 | | [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] |
| | 67 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | 68 | | public async Task<ActionResult> UpdateRole(Guid id, LgdxRoleUpdateDto lgdxRoleUpdateDto) |
| 0 | 69 | | { |
| 0 | 70 | | await _roleService.UpdateRoleAsync(id, lgdxRoleUpdateDto.ToBusinessModel()); |
| 0 | 71 | | return NoContent(); |
| 0 | 72 | | } |
| | 73 | |
|
| | 74 | | [HttpDelete("{id}")] |
| | 75 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | 76 | | [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] |
| | 77 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | 78 | | public async Task<IActionResult> DeleteRole(Guid id) |
| 0 | 79 | | { |
| 0 | 80 | | await _roleService.DeleteRoleAsync(id); |
| 0 | 81 | | return NoContent(); |
| 0 | 82 | | } |
| | 83 | | } |