< Summary

Information
Class: LGDXRobotCloud.API.Areas.Administration.Controllers.ApiKeysController
Assembly: LGDXRobotCloud.API
File(s): /builds/yukaitung/lgdxrobot2-cloud/LGDXRobotCloud.API/Areas/Administration/Controllers/ApiKeysController.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 44
Coverable lines: 44
Total lines: 107
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 10
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)0%2040%
GetApiKeys()0%620%
SearchApiKeys()100%210%
GetApiKey()100%210%
CreateApiKey()100%210%
UpdateApiKey()0%620%
DeleteApiKey()0%620%
GetApiKeySecret()100%210%
UpdateApiKeySecret()100%210%

File(s)

/builds/yukaitung/lgdxrobot2-cloud/LGDXRobotCloud.API/Areas/Administration/Controllers/ApiKeysController.cs

#LineLine coverage
 1using LGDXRobotCloud.API.Authorisation;
 2using LGDXRobotCloud.API.Configurations;
 3using LGDXRobotCloud.API.Services.Administration;
 4using LGDXRobotCloud.Data.Models.Business.Administration;
 5using LGDXRobotCloud.Data.Models.DTOs.V1.Commands;
 6using LGDXRobotCloud.Data.Models.DTOs.V1.Responses;
 7using Microsoft.AspNetCore.Authentication.JwtBearer;
 8using Microsoft.AspNetCore.Authorization;
 9using Microsoft.AspNetCore.Mvc;
 10using Microsoft.Extensions.Options;
 11using System.Text.Json;
 12
 13namespace LGDXRobotCloud.API.Areas.Administration.Controllers;
 14
 15[ApiController]
 16[Area("Administration")]
 17[Route("[area]/[controller]")]
 18[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
 19[ValidateLgdxUserAccess]
 020public sealed class ApiKeysController(
 021    IApiKeyService apiKeyService,
 022    IOptionsSnapshot<LgdxRobotCloudConfiguration> lgdxRobotCloudConfiguration
 023  ) : ControllerBase
 24{
 025  private readonly IApiKeyService _apiKeyService = apiKeyService ?? throw new ArgumentNullException(nameof(apiKeyService
 026  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
 033  {
 034    pageSize = (pageSize > _lgdxRobotCloudConfiguration.ApiMaxPageSize) ? _lgdxRobotCloudConfiguration.ApiMaxPageSize : 
 035    var (apiKeys, PaginationHelper) = await _apiKeyService.GetApiKeysAsync(name, isThirdParty, pageNumber, pageSize);
 036    Response.Headers.Append("X-Pagination", JsonSerializer.Serialize(PaginationHelper));
 037    return Ok(apiKeys.ToDto());
 038  }
 39
 40  [HttpGet("Search")]
 41  [ProducesResponseType(typeof(IEnumerable<ApiKeySearchDto>), StatusCodes.Status200OK)]
 42  public async Task<ActionResult<IEnumerable<ApiKeySearchDto>>> SearchApiKeys(string? name)
 043  {
 044    var apikeys = await _apiKeyService.SearchApiKeysAsync(name);
 045    return Ok(apikeys.ToDto());
 046  }
 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)
 052  {
 053    var apiKey = await _apiKeyService.GetApiKeyAsync(id);
 054    return Ok(apiKey.ToDto());
 055  }
 56
 57  [HttpPost("")]
 58  [ProducesResponseType(StatusCodes.Status201Created)]
 59  public async Task<ActionResult> CreateApiKey(ApiKeyCreateDto apiKeyCreateDto)
 060  {
 061    var apiKey = await _apiKeyService.AddApiKeyAsync(apiKeyCreateDto.ToBusinessModel());
 062    return CreatedAtRoute(nameof(GetApiKey), new { id = apiKey.Id }, apiKey.ToDto());
 063  }
 64
 65  [HttpPut("{id}")]
 66  [ProducesResponseType(StatusCodes.Status204NoContent)]
 67  [ProducesResponseType(StatusCodes.Status404NotFound)]
 68  public async Task<ActionResult> UpdateApiKey(int id, ApiKeyUpdateDto apiKeyUpdateDto)
 069  {
 070    if (!await _apiKeyService.UpdateApiKeyAsync(id, apiKeyUpdateDto.ToBusinessModel()))
 071    {
 072      return NotFound();
 73    }
 074    return NoContent();
 075  }
 76
 77  [HttpDelete("{id}")]
 78  [ProducesResponseType(StatusCodes.Status204NoContent)]
 79  [ProducesResponseType(StatusCodes.Status404NotFound)]
 80  public async Task<ActionResult> DeleteApiKey(int id)
 081  {
 082    if (!await _apiKeyService.DeleteApiKeyAsync(id))
 083    {
 084      return NotFound();
 85    }
 086    return NoContent();
 087  }
 88
 89  [HttpGet("{id}/Secret")]
 90  [ProducesResponseType(typeof(ApiKeySecretDto), StatusCodes.Status200OK)]
 91  [ProducesResponseType(StatusCodes.Status404NotFound)]
 92  public async Task<ActionResult<ApiKeySecretDto>> GetApiKeySecret(int id)
 093  {
 094    var apiKey = await _apiKeyService.GetApiKeySecretAsync(id);
 095    return Ok(apiKey.ToDto());
 096  }
 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)
 0103  {
 0104    await _apiKeyService.UpdateApiKeySecretAsync(id, apiKeySecretUpdateDto.ToBusinessModel());
 0105    return NoContent();
 0106  }
 107}