ApiDbController.cs 6.9 KB

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