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