ApiAipController.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using AipGateway.AIP;
  2. using AipGateway.API.Application.Modules;
  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/aip")]
  13. [Produces("application/json")]
  14. public class ApiAipController : BaseModule
  15. {
  16. private readonly ILogger<ApiAipController> _log;
  17. private readonly IApiAipService _apiAipService;
  18. public ApiAipController(
  19. ILogger<ApiAipController> logger,
  20. IUnitOfWork unitOfWork,
  21. IWebHostEnvironment webHostEnvironment,
  22. IApiAipService apiAipService)
  23. : base(unitOfWork, webHostEnvironment)
  24. {
  25. _log = logger;
  26. _apiAipService = apiAipService;
  27. _log.LogError("ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo: ApiAipController");
  28. }
  29. [HttpPost("download")]
  30. [SwaggerResponse(200, type: typeof(SuccessResponseModel<ApiResponseDto>))]
  31. public async Task<IResult> DownloadAipInfo(string apiKey)
  32. {
  33. GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_AIP_DOWNLOAD, 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 _apiAipService.DownloadAipInfo();
  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("labels")]
  54. [SwaggerResponse(200, type: typeof(SuccessResponseModel<List<AipLabel>>))]
  55. public async Task<IResult> GetAipFileLabels(string apiKey)
  56. {
  57. GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_AIP_LABELS, 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 _apiAipService.GetLabels();
  66. var result = Results.Ok(new SuccessResponseModel<List<AipLabel>>()
  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("policies")]
  78. [SwaggerResponse(200, type: typeof(SuccessResponseModel<List<AipLabel>>))]
  79. public async Task<IResult> GetAipFilePolicies(string apiKey)
  80. {
  81. GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_AIP_POLICIES, 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 _apiAipService.GetPolicies();
  90. var result = Results.Ok(new SuccessResponseModel<List<AipLabel>>()
  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("protections")]
  102. [SwaggerResponse(200, type: typeof(SuccessResponseModel<List<AipTemplate>>))]
  103. public async Task<IResult> GetAipFileProtections(string apiKey)
  104. {
  105. GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_AIP_PROTECTIONS, 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 _apiAipService.GetProtections();
  114. var result = Results.Ok(new SuccessResponseModel<List<AipTemplate>>()
  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. }
  126. }