| | 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 ApiKeysController( |
| 0 | 21 | | IApiKeyService apiKeyService, |
| 0 | 22 | | IOptionsSnapshot<LgdxRobotCloudConfiguration> lgdxRobotCloudConfiguration |
| 0 | 23 | | ) : ControllerBase |
| | 24 | | { |
| 0 | 25 | | private readonly IApiKeyService _apiKeyService = apiKeyService ?? throw new ArgumentNullException(nameof(apiKeyService |
| 0 | 26 | | private readonly LgdxRobotCloudConfiguration _lgdxRobotCloudConfiguration = lgdxRobotCloudConfiguration.Value ?? throw |
| | 27 | |
|
| | 28 | |
|
| | 29 | |
|
| | 30 | | [HttpGet("")] |
| | 31 | | [ProducesResponseType(typeof(IEnumerable<ApiKeyDto>), StatusCodes.Status200OK)] |
| | 32 | | public async Task<ActionResult<IEnumerable<ApiKeyDto>>> GetApiKeys(string? name, bool isThirdParty = false, int pageNu |
| 0 | 33 | | { |
| 0 | 34 | | pageSize = (pageSize > _lgdxRobotCloudConfiguration.ApiMaxPageSize) ? _lgdxRobotCloudConfiguration.ApiMaxPageSize : |
| 0 | 35 | | var (apiKeys, PaginationHelper) = await _apiKeyService.GetApiKeysAsync(name, isThirdParty, pageNumber, pageSize); |
| 0 | 36 | | Response.Headers.Append("X-Pagination", JsonSerializer.Serialize(PaginationHelper)); |
| 0 | 37 | | return Ok(apiKeys.ToDto()); |
| 0 | 38 | | } |
| | 39 | |
|
| | 40 | | [HttpGet("Search")] |
| | 41 | | [ProducesResponseType(typeof(IEnumerable<ApiKeySearchDto>), StatusCodes.Status200OK)] |
| | 42 | | public async Task<ActionResult<IEnumerable<ApiKeySearchDto>>> SearchApiKeys(string? name) |
| 0 | 43 | | { |
| 0 | 44 | | var apikeys = await _apiKeyService.SearchApiKeysAsync(name); |
| 0 | 45 | | return Ok(apikeys.ToDto()); |
| 0 | 46 | | } |
| | 47 | |
|
| | 48 | | [HttpGet("{id}", Name = "GetApiKey")] |
| | 49 | | [ProducesResponseType(typeof(ApiKeyDto), StatusCodes.Status200OK)] |
| | 50 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | 51 | | public async Task<ActionResult<ApiKeyDto>> GetApiKey(int id) |
| 0 | 52 | | { |
| 0 | 53 | | var apiKey = await _apiKeyService.GetApiKeyAsync(id); |
| 0 | 54 | | return Ok(apiKey.ToDto()); |
| 0 | 55 | | } |
| | 56 | |
|
| | 57 | | [HttpPost("")] |
| | 58 | | [ProducesResponseType(StatusCodes.Status201Created)] |
| | 59 | | public async Task<ActionResult> CreateApiKey(ApiKeyCreateDto apiKeyCreateDto) |
| 0 | 60 | | { |
| 0 | 61 | | var apiKey = await _apiKeyService.AddApiKeyAsync(apiKeyCreateDto.ToBusinessModel()); |
| 0 | 62 | | return CreatedAtRoute(nameof(GetApiKey), new { id = apiKey.Id }, apiKey.ToDto()); |
| 0 | 63 | | } |
| | 64 | |
|
| | 65 | | [HttpPut("{id}")] |
| | 66 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | 67 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | 68 | | public async Task<ActionResult> UpdateApiKey(int id, ApiKeyUpdateDto apiKeyUpdateDto) |
| 0 | 69 | | { |
| 0 | 70 | | if (!await _apiKeyService.UpdateApiKeyAsync(id, apiKeyUpdateDto.ToBusinessModel())) |
| 0 | 71 | | { |
| 0 | 72 | | return NotFound(); |
| | 73 | | } |
| 0 | 74 | | return NoContent(); |
| 0 | 75 | | } |
| | 76 | |
|
| | 77 | | [HttpDelete("{id}")] |
| | 78 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | 79 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | 80 | | public async Task<ActionResult> DeleteApiKey(int id) |
| 0 | 81 | | { |
| 0 | 82 | | if (!await _apiKeyService.DeleteApiKeyAsync(id)) |
| 0 | 83 | | { |
| 0 | 84 | | return NotFound(); |
| | 85 | | } |
| 0 | 86 | | return NoContent(); |
| 0 | 87 | | } |
| | 88 | |
|
| | 89 | | [HttpGet("{id}/Secret")] |
| | 90 | | [ProducesResponseType(typeof(ApiKeySecretDto), StatusCodes.Status200OK)] |
| | 91 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | 92 | | public async Task<ActionResult<ApiKeySecretDto>> GetApiKeySecret(int id) |
| 0 | 93 | | { |
| 0 | 94 | | var apiKey = await _apiKeyService.GetApiKeySecretAsync(id); |
| 0 | 95 | | return Ok(apiKey.ToDto()); |
| 0 | 96 | | } |
| | 97 | |
|
| | 98 | | [HttpPut("{id}/Secret")] |
| | 99 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | 100 | | [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] |
| | 101 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | 102 | | public async Task<ActionResult> UpdateApiKeySecret(int id, ApiKeySecretUpdateDto apiKeySecretUpdateDto) |
| 0 | 103 | | { |
| 0 | 104 | | await _apiKeyService.UpdateApiKeySecretAsync(id, apiKeySecretUpdateDto.ToBusinessModel()); |
| 0 | 105 | | return NoContent(); |
| 0 | 106 | | } |
| | 107 | | } |