ApiDbController.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using AipGateway.API.Application;
  2. using AipGateway.API.Application.Interfaces.Services;
  3. using AipGateway.API.Domain.Models;
  4. using AipGateway.API.Domain.Models.Dto;
  5. using AipGateway.API.Domain.Models.Response;
  6. using AipGateway.API.Services;
  7. using AipGateway.API.Services.Interfaces;
  8. using Microsoft.AspNetCore.Mvc;
  9. using Swashbuckle.AspNetCore.Annotations;
  10. using System.ComponentModel.DataAnnotations;
  11. namespace AipGateway.API.Controllers
  12. {
  13. [ApiController]
  14. [Route("/api/v1/db")]
  15. [Produces("application/json")]
  16. public class ApiDbController : BaseController
  17. {
  18. private readonly ILogger<ApiDbController> _log;
  19. private readonly IApiDbService _service;
  20. private readonly IApiAuthService _authService;
  21. public ApiDbController(ILogger<ApiDbController> log, IApiAuthService authService, IApiDbService apiDbService)
  22. {
  23. _log = log;
  24. _authService = authService;
  25. _service = apiDbService;
  26. }
  27. [HttpPost("reload")]
  28. [SwaggerResponse(200, type: typeof(ApiResponseModel<GeneralResponse>))]
  29. public async Task<IResult> ReloadDatabase([Required] string apiKey)
  30. {
  31. return await CreateResponseAsync(async () =>
  32. {
  33. GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_DB_RELOAD, apiKey, null);
  34. int authError = _authService.CheckApiKeyValidation(HttpContext, apiKey, GlobalConstants.API_DB_RELOAD);
  35. if (authError != 0)
  36. {
  37. throw ResponseApiKeyValidationError(HttpContext, authError);
  38. }
  39. var response = await _service.ReloadDatabase();
  40. var result = Results.Ok(ResponseSuccess(HttpContext, response));
  41. HttpContext.Items[GlobalConstants.API_RESULT] = result;
  42. return result;
  43. });
  44. }
  45. [HttpGet("linked-systems")]
  46. [SwaggerResponse(200, type: typeof(ApiResponseModel<List<LinkedSystemDto>>))]
  47. public async Task<IResult> GetDbLinkedSystems([Required] string apiKey)
  48. {
  49. return await CreateResponseAsync(async () =>
  50. {
  51. GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_DB_LINKED_SYSTEMS, apiKey, null);
  52. int authError = _authService.CheckApiKeyValidation(HttpContext, apiKey, GlobalConstants.API_DB_LINKED_SYSTEMS);
  53. if (authError != 0)
  54. {
  55. throw ResponseApiKeyValidationError(HttpContext, authError);
  56. }
  57. var response = await _service.GetLinkedSystems();
  58. var result = Results.Ok(new ApiResponseModel<List<LinkedSystemDto>>()
  59. {
  60. success = true,
  61. errorCode = 0,
  62. errorMessage = GlobalConstants.API_RESULT_SUCCESS,
  63. result = response,
  64. });
  65. HttpContext.Items[GlobalConstants.API_RESULT_CODE] = 0;
  66. HttpContext.Items[GlobalConstants.API_RESULT_MESSAGE] = GlobalConstants.API_RESULT_SUCCESS;
  67. HttpContext.Items[GlobalConstants.API_RESULT] = result;
  68. return result;
  69. });
  70. }
  71. [HttpGet("linked-servers")]
  72. [SwaggerResponse(200, type: typeof(ApiResponseModel<List<LinkedServerDto>>))]
  73. public async Task<IResult> GetDbLinkedServers([Required] string apiKey)
  74. {
  75. return await CreateResponseAsync(async () =>
  76. {
  77. GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_DB_LINKED_SERVERS, apiKey, null);
  78. int authError = _authService.CheckApiKeyValidation(HttpContext, apiKey, GlobalConstants.API_DB_LINKED_SERVERS);
  79. if (authError != 0)
  80. {
  81. throw ResponseApiKeyValidationError(HttpContext, authError);
  82. }
  83. var response = await _service.GetLinkedServers();
  84. var result = Results.Ok(new ApiResponseModel<List<LinkedServerDto>>()
  85. {
  86. success = true,
  87. errorCode = 0,
  88. errorMessage = GlobalConstants.API_RESULT_SUCCESS,
  89. result = response,
  90. });
  91. HttpContext.Items[GlobalConstants.API_RESULT_CODE] = 0;
  92. HttpContext.Items[GlobalConstants.API_RESULT_MESSAGE] = GlobalConstants.API_RESULT_SUCCESS;
  93. HttpContext.Items[GlobalConstants.API_RESULT] = result;
  94. return result;
  95. });
  96. }
  97. [HttpGet("linked-api-keys")]
  98. [SwaggerResponse(200, type: typeof(ApiResponseModel<List<LinkedApiKeyDto>>))]
  99. public async Task<IResult> GetDbLinkedApiKeys([Required] string apiKey)
  100. {
  101. return await CreateResponseAsync(async () =>
  102. {
  103. GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_DB_LINKED_API_KEYS, apiKey, null);
  104. int authError = _authService.CheckApiKeyValidation(HttpContext, apiKey, GlobalConstants.API_DB_LINKED_API_KEYS);
  105. if (authError != 0)
  106. {
  107. throw ResponseApiKeyValidationError(HttpContext, authError);
  108. }
  109. var response = await _service.GetLinkedApiKeys();
  110. var result = Results.Ok(new ApiResponseModel<List<LinkedApiKeyDto>>()
  111. {
  112. success = true,
  113. errorCode = 0,
  114. errorMessage = GlobalConstants.API_RESULT_SUCCESS,
  115. result = response,
  116. });
  117. HttpContext.Items[GlobalConstants.API_RESULT_CODE] = 0;
  118. HttpContext.Items[GlobalConstants.API_RESULT_MESSAGE] = GlobalConstants.API_RESULT_SUCCESS;
  119. HttpContext.Items[GlobalConstants.API_RESULT] = result;
  120. return result;
  121. });
  122. }
  123. [HttpGet("linked-decrypt-keys")]
  124. [SwaggerResponse(200, type: typeof(ApiResponseModel<List<LinkedDecryptKeyDto>>))]
  125. public async Task<IResult> GetDbLinkedDecryptKeys([Required] string apiKey)
  126. {
  127. return await CreateResponseAsync(async () =>
  128. {
  129. GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_DB_LINKED_DECRYPT_KEYS, apiKey, null);
  130. int authError = _authService.CheckApiKeyValidation(HttpContext, apiKey, GlobalConstants.API_DB_LINKED_DECRYPT_KEYS);
  131. if (authError != 0)
  132. {
  133. throw ResponseApiKeyValidationError(HttpContext, authError);
  134. }
  135. var response = await _service.GetLinkedDecryptKeys();
  136. var result = Results.Ok(new ApiResponseModel<List<LinkedDecryptKeyDto>>()
  137. {
  138. success = true,
  139. errorCode = 0,
  140. errorMessage = GlobalConstants.API_RESULT_SUCCESS,
  141. result = response,
  142. });
  143. HttpContext.Items[GlobalConstants.API_RESULT_CODE] = 0;
  144. HttpContext.Items[GlobalConstants.API_RESULT_MESSAGE] = GlobalConstants.API_RESULT_SUCCESS;
  145. HttpContext.Items[GlobalConstants.API_RESULT] = result;
  146. return result;
  147. });
  148. }
  149. }
  150. }