From b7caaa2d0c81909d360a7dbc51fad584f56dd817 Mon Sep 17 00:00:00 2001 From: hello Date: Thu, 25 Apr 2024 22:59:28 +0800 Subject: [PATCH] =?UTF-8?q?=E8=81=9A=E5=90=88=20API=20=E7=BD=91=E5=85=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/PermissionsController.cs | 18 ++++++++++ src/HelloShop.ApiService/Program.cs | 2 ++ .../Services/IPermissionService.cs | 8 +++++ .../Services/PermissionService.cs | 35 +++++++++++++++++++ .../Extensions/PermissionExtensions.cs | 2 +- .../PermissionGroupDefinitionResponse.cs | 2 +- 6 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 src/HelloShop.ApiService/Controllers/PermissionsController.cs create mode 100644 src/HelloShop.ApiService/Services/IPermissionService.cs create mode 100644 src/HelloShop.ApiService/Services/PermissionService.cs diff --git a/src/HelloShop.ApiService/Controllers/PermissionsController.cs b/src/HelloShop.ApiService/Controllers/PermissionsController.cs new file mode 100644 index 0000000..5c4d31c --- /dev/null +++ b/src/HelloShop.ApiService/Controllers/PermissionsController.cs @@ -0,0 +1,18 @@ +using HelloShop.ApiService.Services; +using HelloShop.ServiceDefaults.Permissions; +using Microsoft.AspNetCore.Mvc; + +namespace HelloShop.ApiService.Controllers; + +[Route("api/[controller]")] +[ApiController] +public class PermissionsController(IPermissionService permissionService) : ControllerBase +{ + [HttpGet("Definitions")] + public async Task>> GetAllPermissionDefinitions() + { + IReadOnlyList permissionGroupDefinitions = await permissionService.GetAllPermissionGorupDefinitionsAsync(HttpContext.RequestAborted); + + return permissionGroupDefinitions.Any() ? Ok(permissionGroupDefinitions) : NoContent(); + } +} diff --git a/src/HelloShop.ApiService/Program.cs b/src/HelloShop.ApiService/Program.cs index 53eb1d2..b93e105 100644 --- a/src/HelloShop.ApiService/Program.cs +++ b/src/HelloShop.ApiService/Program.cs @@ -4,6 +4,7 @@ using HelloShop.ServiceDefaults.Extensions; using Microsoft.Extensions.Options; using Swashbuckle.AspNetCore.SwaggerUI; using HelloShop.ApiService.Extensions; +using HelloShop.ApiService.Services; var builder = WebApplication.CreateBuilder(args); @@ -22,6 +23,7 @@ builder.Services.AddReverseProxy() builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddTransient, OpenApiConfigureOptions>(); +builder.Services.AddTransient(); builder.Services.AddOpenApi(); diff --git a/src/HelloShop.ApiService/Services/IPermissionService.cs b/src/HelloShop.ApiService/Services/IPermissionService.cs new file mode 100644 index 0000000..ab41573 --- /dev/null +++ b/src/HelloShop.ApiService/Services/IPermissionService.cs @@ -0,0 +1,8 @@ +using HelloShop.ServiceDefaults.Permissions; + +namespace HelloShop.ApiService.Services; + +public interface IPermissionService +{ + Task> GetAllPermissionGorupDefinitionsAsync(CancellationToken cancellationToken = default); +} \ No newline at end of file diff --git a/src/HelloShop.ApiService/Services/PermissionService.cs b/src/HelloShop.ApiService/Services/PermissionService.cs new file mode 100644 index 0000000..060385c --- /dev/null +++ b/src/HelloShop.ApiService/Services/PermissionService.cs @@ -0,0 +1,35 @@ +using HelloShop.ApiService.Infrastructure; +using HelloShop.ServiceDefaults.Permissions; + +namespace HelloShop.ApiService.Services; + +public class PermissionService(HttpClient httpClient, IConfiguredServiceEndPointResolver serviceEndPointResolver) : IPermissionService +{ + public async Task> GetAllPermissionGorupDefinitionsAsync(CancellationToken cancellationToken = default) + { + List result = []; + + httpClient.Timeout = TimeSpan.FromSeconds(10); + + IReadOnlyCollection serviceEndPoints = await serviceEndPointResolver.GetConfiguredServiceEndpointsAsync(cancellationToken); + + await Parallel.ForEachAsync(serviceEndPoints, new ParallelOptions{ CancellationToken= cancellationToken}, async (serviceEndPoint, cancelToken) => + { + UriBuilder uriBuilder = new(serviceEndPoint.ServiceName) { Path = "api/Permissions/PermissionDefinitions" }; + + HttpResponseMessage response = await httpClient.GetAsync(uriBuilder.Uri, cancelToken); + + if (response.IsSuccessStatusCode) + { + List? permissionGroupDefinition = await response.Content.ReadFromJsonAsync>(cancelToken); + + if (permissionGroupDefinition != null) + { + result.AddRange(permissionGroupDefinition); + } + } + }); + + return result.DistinctBy(x => x.GroupName).ToList(); + } +} diff --git a/src/HelloShop.ServiceDefaults/Extensions/PermissionExtensions.cs b/src/HelloShop.ServiceDefaults/Extensions/PermissionExtensions.cs index befc61a..7d368bc 100644 --- a/src/HelloShop.ServiceDefaults/Extensions/PermissionExtensions.cs +++ b/src/HelloShop.ServiceDefaults/Extensions/PermissionExtensions.cs @@ -38,7 +38,7 @@ public static class PermissionExtensions { PermissionGroupDefinitionResponse permissionGroupDefinition = new() { - Name = permissionGroup.Name, + GroupName = permissionGroup.Name, DisplayName = permissionGroup.DisplayName, Permissions = [] }; diff --git a/src/HelloShop.ServiceDefaults/Models/Permissions/PermissionGroupDefinitionResponse.cs b/src/HelloShop.ServiceDefaults/Models/Permissions/PermissionGroupDefinitionResponse.cs index 6387b9f..d3a1eb1 100644 --- a/src/HelloShop.ServiceDefaults/Models/Permissions/PermissionGroupDefinitionResponse.cs +++ b/src/HelloShop.ServiceDefaults/Models/Permissions/PermissionGroupDefinitionResponse.cs @@ -2,7 +2,7 @@ public class PermissionGroupDefinitionResponse { - public required string Name { get; init; } + public required string GroupName { get; init; } public string? DisplayName { get; set; }