First version of BFF
This commit is contained in:
		| @@ -0,0 +1,65 @@ | ||||
| using Core.Inventory.External.Clients.Inventory; | ||||
| using Core.Inventory.External.GatewayConfigurations; | ||||
| using Lib.Architecture.BuildingBlocks.Helpers.Token; | ||||
| using Microsoft.AspNetCore.Http; | ||||
| using Microsoft.Extensions.Configuration; | ||||
| using Microsoft.Extensions.DependencyInjection; | ||||
| using Refit; | ||||
|  | ||||
| namespace Core.Inventory.External.ClientConfiguration | ||||
| { | ||||
|     public static class RegisterClientConfiguration | ||||
|     { | ||||
|         public static IServiceCollection RegisterExternalLayer(this IServiceCollection services, IConfiguration configuration) | ||||
|         { | ||||
|             var gatewayConfiguration = new GatewayConfiguration(); | ||||
|             var gatewaySettingsConfiguration = new GatewaySettingsConfigurations(configuration); | ||||
|  | ||||
|             // Register GatewayConfiguration as a singleton | ||||
|             services.AddSingleton(gatewayConfiguration); | ||||
|  | ||||
|             // Register IHttpContextAccessor | ||||
|             services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); | ||||
|  | ||||
|             // Register ITokenProvider | ||||
|             services.AddSingleton<ITokenProvider, HttpContextTokenProvider>(); | ||||
|  | ||||
|             // Register the custom AuthenticatedHttpClientHandler | ||||
|             services.AddTransient(provider => | ||||
|             { | ||||
|                 var tokenProvider = provider.GetRequiredService<ITokenProvider>(); | ||||
|                 var trackingIdHandler = new TrackingMechanismExtension(provider.GetRequiredService<IHttpContextAccessor>()); | ||||
|  | ||||
|                 var handler = new AuthenticatedHttpClientHandler(tokenProvider) | ||||
|                 { | ||||
|                     InnerHandler = new HttpClientHandler() // Setting the InnerHandler manually | ||||
|                 }; | ||||
|  | ||||
|                 // Attach the TrackingIdHandler as the outermost handler | ||||
|                 trackingIdHandler.InnerHandler = handler; | ||||
|  | ||||
|                 return handler; | ||||
|             }); | ||||
|  | ||||
|             var inventoryServiceApiUrl = GatewaySettingsConfigurations.GetInventoryServiceAPIEndpoint().Endpoint.Url; | ||||
|  | ||||
|             // Register IDashBoardServiceClient with the manually created HttpClient | ||||
|             services.AddScoped(provider => | ||||
|             { | ||||
|                 var handler = provider.GetRequiredService<AuthenticatedHttpClientHandler>(); | ||||
|                 var handlerTrackingId = new TrackingMechanismExtension(provider.GetRequiredService<IHttpContextAccessor>()); // Using the TrackingIdHandler here | ||||
|                                                                                                                              // Chain the handlers | ||||
|                 handlerTrackingId.InnerHandler = handler; //chaining | ||||
|  | ||||
|                 var httpClient = new HttpClient(handlerTrackingId) | ||||
|                 { | ||||
|                     BaseAddress = new Uri(inventoryServiceApiUrl), | ||||
|                     Timeout = TimeSpan.FromMinutes(1) | ||||
|                 }; | ||||
|                 return RestService.For<IInventoryServiceClient>(httpClient); | ||||
|             }); | ||||
|  | ||||
|             return services; | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,51 @@ | ||||
| using Core.Adapters.Lib; | ||||
| using Core.Inventory.External.Clients.Inventory.Requests.Base; | ||||
| using Core.Inventory.External.Clients.Inventory.Requests.Variant; | ||||
| using Refit; | ||||
|  | ||||
| namespace Core.Inventory.External.Clients.Inventory | ||||
| { | ||||
|     public interface IInventoryServiceClient | ||||
|     { | ||||
|         #region Furniture Base | ||||
|  | ||||
|         [Get("/api/v1/FurnitureBase/GetAll")] | ||||
|         Task<ApiResponse<IEnumerable<FurnitureBase>>> GetAllAsync([Header("TrackingId")] string trackingId, CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Post("/api/v1/FurnitureBase/GetById")] | ||||
|         Task<ApiResponse<FurnitureBase>> GetByIdAsync([Header("TrackingId")] string trackingId, [Body] GetFurnitureBaseByIdRequest request, CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Post("/api/v1/FurnitureBase/Create")] | ||||
|         Task<ApiResponse<FurnitureBase>> CreateAsync([Header("TrackingId")] string trackingId, [Body] CreateFurnitureBaseRequest request, CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Put("/api/v1/FurnitureBase/Update")] | ||||
|         Task<ApiResponse<FurnitureBase>> UpdateFurnitureBaseAsync([Header("TrackingId")] string trackingId, [Body] UpdateFurnitureBaseRequest request, CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Patch("/api/v1/FurnitureBase/ChangeStatus")] | ||||
|         Task<ApiResponse<FurnitureBase>> ChangeFurnitureBaseStatusAsync([Header("TrackingId")] string trackingId, [Body] ChangeFurnitureBaseStatusRequest request, CancellationToken cancellationToken = default); | ||||
|  | ||||
|         #endregion | ||||
|  | ||||
|         #region Furniture Variant | ||||
|  | ||||
|         [Post("/api/v1/FurnitureVariant/GetAllByModelId")] | ||||
|         Task<ApiResponse<IEnumerable<FurnitureVariant>>> GetVariantsByModelIdAsync([Header("TrackingId")] string trackingId, [Body] GetAllFurnitureVariantsByModelIdRequest request, CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Post("/api/v1/FurnitureVariant/GetById")] | ||||
|         Task<ApiResponse<FurnitureVariant>> GetFurnitureVariantByIdAsync([Header("TrackingId")] string trackingId, [Body] GetFurnitureVariantByIdRequest request, CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Post("/api/v1/FurnitureVariant/GetByIds")] | ||||
|         Task<ApiResponse<IEnumerable<FurnitureVariant>>> GetFurnitureVariantsByIdsAsync([Header("TrackingId")] string trackingId, [Body] GetFurnitureVariantsByIdsRequest request, CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Post("/api/v1/FurnitureVariant/Create")] | ||||
|         Task<ApiResponse<FurnitureVariant>> CreateFurnitureVariantAsync([Header("TrackingId")] string trackingId, [Body] CreateFurnitureVariantRequest request, CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Put("/api/v1/FurnitureVariant/Update")] | ||||
|         Task<ApiResponse<FurnitureVariant>> UpdateFurnitureVariantAsync([Header("TrackingId")] string trackingId, string id, [Body] UpdateFurnitureVariantRequest entity, CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Patch("/api/v1/FurnitureVariant/ChangeStatus")] | ||||
|         Task<ApiResponse<FurnitureVariant>> ChangeFurnitureVariantStatusAsync([Header("TrackingId")] string trackingId, [Body] ChangeFurnitureVariantStatusRequest request, CancellationToken cancellationToken = default); | ||||
|  | ||||
|         #endregion | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,10 @@ | ||||
| using Core.Blueprint.Mongo; | ||||
|  | ||||
| namespace Core.Inventory.External.Clients.Inventory.Requests.Base | ||||
| { | ||||
|     public class ChangeFurnitureBaseStatusRequest | ||||
|     { | ||||
|         public string MongoId { get; set; } = default!; | ||||
|         public StatusEnum Status { get; set; } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,16 @@ | ||||
| using Core.Inventory.External.Clients.Inventory.Requests.Common; | ||||
|  | ||||
| namespace Core.Inventory.External.Clients.Inventory.Requests.Base | ||||
| { | ||||
|     public class CreateFurnitureBaseRequest | ||||
|     { | ||||
|         public string Name { get; set; } = default!; | ||||
|         public string Material { get; set; } = default!; | ||||
|         public string Condition { get; set; } = default!; | ||||
|         public string? Description { get; set; } | ||||
|         public string? Representation { get; set; } | ||||
|         public string? Notes { get; set; } | ||||
|         public DimensionsRequest Dimensions { get; set; } = new(); | ||||
|         public List<string>? VariantIds { get; set; } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,6 @@ | ||||
| namespace Core.Inventory.External.Clients.Inventory.Requests.Base | ||||
| { | ||||
|     public class GetAllFurnitureBaseRequest | ||||
|     { | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,7 @@ | ||||
| namespace Core.Inventory.External.Clients.Inventory.Requests.Base | ||||
| { | ||||
|     public class GetFurnitureBaseByIdRequest | ||||
|     { | ||||
|         public string MongoId { get; set; } = default!; | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,19 @@ | ||||
| using Core.Blueprint.Mongo; | ||||
| using Core.Inventory.External.Clients.Inventory.Requests.Common; | ||||
|  | ||||
| namespace Core.Inventory.External.Clients.Inventory.Requests.Base | ||||
| { | ||||
|     public class UpdateFurnitureBaseRequest | ||||
|     { | ||||
|         public string Id { get; set; } = default!; | ||||
|         public string Name { get; set; } = default!; | ||||
|         public string Material { get; set; } = default!; | ||||
|         public string Condition { get; set; } = default!; | ||||
|         public string? Description { get; set; } | ||||
|         public string? Representation { get; set; } | ||||
|         public string? Notes { get; set; } | ||||
|         public DimensionsRequest Dimensions { get; set; } = new(); | ||||
|         public List<string>? VariantIds { get; set; } | ||||
|         public StatusEnum Status { get; set; } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,9 @@ | ||||
| namespace Core.Inventory.External.Clients.Inventory.Requests.Common | ||||
| { | ||||
|     public class DimensionsRequest | ||||
|     { | ||||
|         public decimal Width { get; set; } | ||||
|         public decimal Height { get; set; } | ||||
|         public decimal Depth { get; set; } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,10 @@ | ||||
| using Core.Blueprint.Mongo; | ||||
|  | ||||
| namespace Core.Inventory.External.Clients.Inventory.Requests.Variant | ||||
| { | ||||
|     public class ChangeFurnitureVariantStatusRequest | ||||
|     { | ||||
|         public string MongoId { get; set; } = default!; | ||||
|         public StatusEnum Status { get; set; } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,19 @@ | ||||
| namespace Core.Inventory.External.Clients.Inventory.Requests.Variant | ||||
| { | ||||
|     public class CreateFurnitureVariantRequest | ||||
|     { | ||||
|         public string ModelId { get; set; } = default!; | ||||
|         public string Name { get; set; } = default!; | ||||
|         public string Color { get; set; } = default!; | ||||
|         public string? Line { get; set; } | ||||
|  | ||||
|         public decimal Price { get; set; } | ||||
|         public string Currency { get; set; } = "USD"; | ||||
|         public int Stock { get; set; } | ||||
|  | ||||
|         public Guid CategoryId { get; set; } | ||||
|         public Guid ProviderId { get; set; } | ||||
|  | ||||
|         public Dictionary<string, object> Attributes { get; set; } = []; | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,7 @@ | ||||
| namespace Core.Inventory.External.Clients.Inventory.Requests.Variant | ||||
| { | ||||
|     public class GetAllFurnitureVariantsByModelIdRequest | ||||
|     { | ||||
|         public string ModelId { get; set; } = default!; | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,7 @@ | ||||
| namespace Core.Inventory.External.Clients.Inventory.Requests.Variant | ||||
| { | ||||
|     public class GetFurnitureVariantByIdRequest | ||||
|     { | ||||
|         public string MongoId { get; set; } = default!; | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,7 @@ | ||||
| namespace Core.Inventory.External.Clients.Inventory.Requests.Variant | ||||
| { | ||||
|     public class GetFurnitureVariantsByIdsRequest | ||||
|     { | ||||
|         public List<string> Ids { get; set; } = []; | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,20 @@ | ||||
| namespace Core.Inventory.External.Clients.Inventory.Requests.Variant | ||||
| { | ||||
|     public class UpdateFurnitureVariantRequest | ||||
|     { | ||||
|         public string Id { get; set; } = default!; | ||||
|         public string ModelId { get; set; } = default!; | ||||
|         public string Name { get; set; } = default!; | ||||
|         public string Color { get; set; } = default!; | ||||
|         public string? Line { get; set; } | ||||
|  | ||||
|         public int Stock { get; set; } | ||||
|         public decimal Price { get; set; } | ||||
|         public string Currency { get; set; } = "USD"; | ||||
|  | ||||
|         public Guid CategoryId { get; set; } | ||||
|         public Guid ProviderId { get; set; } | ||||
|  | ||||
|         public Dictionary<string, object> Attributes { get; set; } = new(); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										15
									
								
								Core.Inventory.External/Core.Inventory.External.csproj
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								Core.Inventory.External/Core.Inventory.External.csproj
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,15 @@ | ||||
| <Project Sdk="Microsoft.NET.Sdk"> | ||||
|  | ||||
|   <PropertyGroup> | ||||
|     <TargetFramework>net8.0</TargetFramework> | ||||
|     <ImplicitUsings>enable</ImplicitUsings> | ||||
|     <Nullable>enable</Nullable> | ||||
|   </PropertyGroup> | ||||
|  | ||||
|   <ItemGroup> | ||||
|     <PackageReference Include="Adapters.Lib" Version="1.0.3" /> | ||||
|     <PackageReference Include="BuildingBlocks.Library" Version="1.0.0" /> | ||||
|     <PackageReference Include="Refit" Version="8.0.0" /> | ||||
|   </ItemGroup> | ||||
|  | ||||
| </Project> | ||||
| @@ -0,0 +1,19 @@ | ||||
| using Core.Blueprint.External; | ||||
|  | ||||
| namespace Core.Inventory.External.GatewayConfigurations | ||||
| { | ||||
|     public record GatewayConfiguration | ||||
|     { | ||||
|         public GatewayConfiguration() | ||||
|         { | ||||
|             InventoryService = new InventoryServiceAPI(); | ||||
|         } | ||||
|  | ||||
|         public InventoryServiceAPI InventoryService { get; set; } | ||||
|     } | ||||
|     public record InventoryServiceAPI | ||||
|     { | ||||
|         public string Channel { get; set; } | ||||
|         public BaseEndpoint Endpoint { get; set; } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,48 @@ | ||||
| using Core.Blueprint.External; | ||||
| using Microsoft.Extensions.Configuration; | ||||
|  | ||||
| namespace Core.Inventory.External.GatewayConfigurations | ||||
| { | ||||
|     public class GatewaySettingsConfigurations | ||||
|     { | ||||
|         private static GatewayConfiguration GatewayConfigurations { get; set; } = new GatewayConfiguration(); | ||||
|         private readonly IConfiguration _configuration; | ||||
|  | ||||
|         public GatewaySettingsConfigurations(IConfiguration configuration) | ||||
|         { | ||||
|             _configuration = configuration; | ||||
|             SetDashboardServiceAPIEndpoint(); | ||||
|         } | ||||
|         public static InventoryServiceAPI GetInventoryServiceAPIEndpoint() | ||||
|         { | ||||
|             return GatewayConfigurations.InventoryService; | ||||
|         } | ||||
|         private GatewayConfiguration SetDashboardServiceAPIEndpoint() | ||||
|         { | ||||
|             IConfigurationSection source; | ||||
|             var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? string.Empty; | ||||
|  | ||||
|             if (environment == "Local") | ||||
|                 source = _configuration.GetSection("LocalGateways"); | ||||
|             else | ||||
|                 source = _configuration.GetSection("Gateways"); | ||||
|  | ||||
|             var endpoint = source["InventoryService"] ?? string.Empty; | ||||
|  | ||||
|             if (string.IsNullOrEmpty(endpoint)) throw new Exception("Inventory Service endpoint is empty or null"); | ||||
|  | ||||
|             GatewayConfigurations.InventoryService = new InventoryServiceAPI() | ||||
|             { | ||||
|                 Endpoint = new BaseEndpoint() | ||||
|                 { | ||||
|                     Uri = new Uri(endpoint), | ||||
|                     Url = endpoint, | ||||
|                     Token = string.Empty, | ||||
|                     APIName = "Inventory Service" | ||||
|                 } | ||||
|             }; | ||||
|  | ||||
|             return GatewayConfigurations; | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										18
									
								
								Core.Inventory.External/TrackingMechanismExtension.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								Core.Inventory.External/TrackingMechanismExtension.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,18 @@ | ||||
| using Microsoft.AspNetCore.Http; | ||||
|  | ||||
| namespace Core.Inventory.External | ||||
| { | ||||
|     public sealed class TrackingMechanismExtension(IHttpContextAccessor httpContextAccessor) : DelegatingHandler | ||||
|     { | ||||
|         private readonly IHttpContextAccessor _httpContextAccessor = httpContextAccessor ?? throw new ArgumentNullException(nameof(httpContextAccessor)); | ||||
|  | ||||
|         protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | ||||
|         { | ||||
|             if (_httpContextAccessor.HttpContext != null) | ||||
|             { | ||||
|                 request.Headers.Add("TrackingId", Guid.NewGuid().ToString()); | ||||
|             } | ||||
|             return await base.SendAsync(request, cancellationToken).ConfigureAwait(false); | ||||
|         } | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user