using Aip.Service.Models.Dto; using Aip.Service.Models.Response; using Aip.Service.Repositories; using Aip.Service.Services.Interfaces; using Microsoft.AspNetCore.Mvc; using Swashbuckle.AspNetCore.Annotations; using System.ComponentModel.DataAnnotations; namespace Aip.Service.Controllers; [ApiController] [Route("/api/v1/db")] [Produces("application/json")] public class ApiDbController : BaseController { private readonly ILogger _log; private readonly IApiAuthService _authService; private readonly IApiDbService _service; public ApiDbController(ILogger log, IApiAuthService authService, IApiDbService apiDbService) { _log = log; _authService = authService; _service = apiDbService; } [HttpPost("reload")] [SwaggerResponse(200, type: typeof(ApiResponseModel))] public async Task ReloadDatabase([Required] string apiKey) { return await CreateResponseAsync(async () => { GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_DB_RELOAD, apiKey, null); int authError = _authService.CheckApiKeyValidation(HttpContext, apiKey, GlobalConstants.API_DB_RELOAD); if (authError != 0) { throw ResponseApiKeyValidationError(HttpContext, authError); } var response = await _service.ReloadDatabase(); var result = Results.Ok(ResponseSuccess(HttpContext, response)); HttpContext.Items[GlobalConstants.API_RESULT] = result; return result; }); } [HttpGet("linked-systems")] [SwaggerResponse(200, type: typeof(ApiResponseModel>))] public async Task GetDbLinkedSystems([Required] string apiKey) { return await CreateResponseAsync(async () => { GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_DB_LINKED_SYSTEMS, apiKey, null); int authError = _authService.CheckApiKeyValidation(HttpContext, apiKey, GlobalConstants.API_DB_LINKED_SYSTEMS); if (authError != 0) { throw ResponseApiKeyValidationError(HttpContext, authError); } var response = await _service.GetLinkedSystems(); var result = Results.Ok(new ApiResponseModel>() { success = true, errorCode = 0, errorMessage = GlobalConstants.API_RESULT_SUCCESS, result = response, }); HttpContext.Items[GlobalConstants.API_RESULT_CODE] = GlobalConstants.API_RESULT_SUCCESS_CODE; HttpContext.Items[GlobalConstants.API_RESULT_MESSAGE] = GlobalConstants.API_RESULT_SUCCESS; HttpContext.Items[GlobalConstants.API_RESULT] = result; return result; }); } [HttpGet("linked-servers")] [SwaggerResponse(200, type: typeof(ApiResponseModel>))] public async Task GetDbLinkedServers([Required] string apiKey) { return await CreateResponseAsync(async () => { GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_DB_LINKED_SERVERS, apiKey, null); int authError = _authService.CheckApiKeyValidation(HttpContext, apiKey, GlobalConstants.API_DB_LINKED_SERVERS); if (authError != 0) { throw ResponseApiKeyValidationError(HttpContext, authError); } var response = await _service.GetLinkedServers(); var result = Results.Ok(new ApiResponseModel>() { success = true, errorCode = 0, errorMessage = GlobalConstants.API_RESULT_SUCCESS, result = response, }); HttpContext.Items[GlobalConstants.API_RESULT_CODE] = GlobalConstants.API_RESULT_SUCCESS_CODE; HttpContext.Items[GlobalConstants.API_RESULT_MESSAGE] = GlobalConstants.API_RESULT_SUCCESS; HttpContext.Items[GlobalConstants.API_RESULT] = result; return result; }); } [HttpGet("linked-api-keys")] [SwaggerResponse(200, type: typeof(ApiResponseModel>))] public async Task GetDbLinkedApiKeys([Required] string apiKey) { return await CreateResponseAsync(async () => { GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_DB_LINKED_API_KEYS, apiKey, null); int authError = _authService.CheckApiKeyValidation(HttpContext, apiKey, GlobalConstants.API_DB_LINKED_API_KEYS); if (authError != 0) { throw ResponseApiKeyValidationError(HttpContext, authError); } var response = await _service.GetLinkedApiKeys(); var result = Results.Ok(new ApiResponseModel>() { success = true, errorCode = 0, errorMessage = GlobalConstants.API_RESULT_SUCCESS, result = response, }); HttpContext.Items[GlobalConstants.API_RESULT_CODE] = GlobalConstants.API_RESULT_SUCCESS_CODE; HttpContext.Items[GlobalConstants.API_RESULT_MESSAGE] = GlobalConstants.API_RESULT_SUCCESS; HttpContext.Items[GlobalConstants.API_RESULT] = result; return result; }); } [HttpGet("linked-decrypt-keys")] [SwaggerResponse(200, type: typeof(ApiResponseModel>))] public async Task GetDbLinkedDecryptKeys([Required] string apiKey) { return await CreateResponseAsync(async () => { GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_DB_LINKED_DECRYPT_KEYS, apiKey, null); int authError = _authService.CheckApiKeyValidation(HttpContext, apiKey, GlobalConstants.API_DB_LINKED_DECRYPT_KEYS); if (authError != 0) { throw ResponseApiKeyValidationError(HttpContext, authError); } var response = await _service.GetLinkedDecryptKeys(); var result = Results.Ok(new ApiResponseModel>() { success = true, errorCode = 0, errorMessage = GlobalConstants.API_RESULT_SUCCESS, result = response, }); HttpContext.Items[GlobalConstants.API_RESULT_CODE] = GlobalConstants.API_RESULT_SUCCESS_CODE; HttpContext.Items[GlobalConstants.API_RESULT_MESSAGE] = GlobalConstants.API_RESULT_SUCCESS; HttpContext.Items[GlobalConstants.API_RESULT] = result; return result; }); } }