From 7d25d88e77360915f3e3f3670fcc9cefabab9571 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Thu, 9 Dec 2021 14:57:07 +0300 Subject: [PATCH] Initial "add to basket" --- .../EShopOnAbpPublicWebModule.cs | 17 ++++++++- .../AdministrationServiceHttpApiHostModule.cs | 4 ++ .../BasketAppService.cs | 8 ++++ .../BasketProductService.cs | 4 +- .../EShopOnAbp.BasketService.Domain/Basket.cs | 2 +- .../BasketRepository.cs | 1 + .../BasketServiceHttpApiHostModule.cs | 12 +++++- .../appsettings.json | 4 +- .../Products/IPublicProductAppService.cs | 4 +- .../Products/PublicProductAppService.cs | 6 +++ .../PublicProductClientProxy.Generated.cs | 8 ++++ .../ClientProxies/catalog-generate-proxy.json | 37 +++++++++++++++++++ .../CatalogServiceHttpApiHostModule.cs | 2 +- .../appsettings.json | 2 +- 14 files changed, 101 insertions(+), 10 deletions(-) diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/EShopOnAbpPublicWebModule.cs b/apps/public-web/src/EShopOnAbp.PublicWeb/EShopOnAbpPublicWebModule.cs index 6cbf67ac..391c25aa 100644 --- a/apps/public-web/src/EShopOnAbp.PublicWeb/EShopOnAbpPublicWebModule.cs +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/EShopOnAbpPublicWebModule.cs @@ -1,11 +1,14 @@ using System; +using System.Net.Http.Headers; using EShopOnAbp.BasketService; using EShopOnAbp.CatalogService; using EShopOnAbp.Localization; using EShopOnAbp.PublicWeb.Menus; using EShopOnAbp.Shared.Hosting.AspNetCore; +using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.DataProtection; +using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.IdentityModel.Protocols.OpenIdConnect; @@ -26,6 +29,7 @@ using Volo.Abp.Modularity; using Volo.Abp.MultiTenancy; using Volo.Abp.UI.Navigation; using Volo.Abp.UI.Navigation.Urls; +using Yarp.ReverseProxy.Transforms; namespace EShopOnAbp.PublicWeb { @@ -124,7 +128,18 @@ namespace EShopOnAbp.PublicWeb context.Services .AddReverseProxy() - .LoadFromConfig(configuration.GetSection("ReverseProxy")); + .LoadFromConfig(configuration.GetSection("ReverseProxy")) + .AddTransforms(builderContext => + { + builderContext.AddRequestTransform(async (transformContext) => + { + transformContext.ProxyRequest.Headers + .Authorization = new AuthenticationHeaderValue( + "Bearer", + await transformContext.HttpContext.GetTokenAsync("access_token") + ); + }); + }); } public override void OnApplicationInitialization(ApplicationInitializationContext context) diff --git a/services/administration/src/EShopOnAbp.AdministrationService.HttpApi.Host/AdministrationServiceHttpApiHostModule.cs b/services/administration/src/EShopOnAbp.AdministrationService.HttpApi.Host/AdministrationServiceHttpApiHostModule.cs index 8253bf1e..e1c13daf 100644 --- a/services/administration/src/EShopOnAbp.AdministrationService.HttpApi.Host/AdministrationServiceHttpApiHostModule.cs +++ b/services/administration/src/EShopOnAbp.AdministrationService.HttpApi.Host/AdministrationServiceHttpApiHostModule.cs @@ -85,6 +85,10 @@ namespace EShopOnAbp.AdministrationService //app.UseHttpMetrics(); app.UseAuthentication(); app.UseAbpClaimsMap(); + app.Use(async (httpContext, func) => + { + await func(); + }); app.UseAuthorization(); app.UseSwagger(); app.UseSwaggerUI(options => diff --git a/services/basket/src/EShopOnAbp.BasketService.Application/BasketAppService.cs b/services/basket/src/EShopOnAbp.BasketService.Application/BasketAppService.cs index 24fba6cb..73eefdb3 100644 --- a/services/basket/src/EShopOnAbp.BasketService.Application/BasketAppService.cs +++ b/services/basket/src/EShopOnAbp.BasketService.Application/BasketAppService.cs @@ -34,6 +34,10 @@ public class BasketAppService : BasketServiceAppService, IBasketAppService var product = await _basketProductService.GetAsync(input.ProductId); basket.AddProduct(product.Id); + + await _basketRepository.UpdateAsync(basket); + + var basket2 = await _basketRepository.GetAsync(CurrentUser.GetId()); return await GetBasketDtoAsync(basket); } @@ -45,6 +49,8 @@ public class BasketAppService : BasketServiceAppService, IBasketAppService basket.RemoveProduct(product.Id, input.Count); + await _basketRepository.UpdateAsync(basket); + return await GetBasketDtoAsync(basket); } @@ -73,6 +79,8 @@ public class BasketAppService : BasketServiceAppService, IBasketAppService basketItemDto.ImageName = productDto.ImageName; basketItemDto.ProductName = productDto.Name; basketItemDto.TotalPrice = productDto.Price * basketItemDto.Count; + + basketDto.Items.Add(basketItemDto); } basketDto.TotalPrice = basketDto.Items.Sum(x => x.TotalPrice); diff --git a/services/basket/src/EShopOnAbp.BasketService.Application/BasketProductService.cs b/services/basket/src/EShopOnAbp.BasketService.Application/BasketProductService.cs index 3acd1116..0fc0f523 100644 --- a/services/basket/src/EShopOnAbp.BasketService.Application/BasketProductService.cs +++ b/services/basket/src/EShopOnAbp.BasketService.Application/BasketProductService.cs @@ -9,11 +9,11 @@ namespace EShopOnAbp.BasketService; public class BasketProductService : IBasketProductService, ITransientDependency { - private readonly IProductAppService _productAppService; + private readonly IPublicProductAppService _productAppService; private readonly IDistributedCache _cache; public BasketProductService( - IProductAppService productAppService, + IPublicProductAppService productAppService, IDistributedCache cache) { _productAppService = productAppService; diff --git a/services/basket/src/EShopOnAbp.BasketService.Domain/Basket.cs b/services/basket/src/EShopOnAbp.BasketService.Domain/Basket.cs index c2345aad..38a65728 100644 --- a/services/basket/src/EShopOnAbp.BasketService.Domain/Basket.cs +++ b/services/basket/src/EShopOnAbp.BasketService.Domain/Basket.cs @@ -7,7 +7,7 @@ namespace EShopOnAbp.BasketService; public class Basket : AggregateRoot { - public List Items { get; private set; } + public List Items { get; set; } private Basket() { diff --git a/services/basket/src/EShopOnAbp.BasketService.Domain/BasketRepository.cs b/services/basket/src/EShopOnAbp.BasketService.Domain/BasketRepository.cs index 0b851d72..ac9d6d2f 100644 --- a/services/basket/src/EShopOnAbp.BasketService.Domain/BasketRepository.cs +++ b/services/basket/src/EShopOnAbp.BasketService.Domain/BasketRepository.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Threading.Tasks; using Volo.Abp.Caching; diff --git a/services/basket/src/EShopOnAbp.BasketService.HttpApi.Host/BasketServiceHttpApiHostModule.cs b/services/basket/src/EShopOnAbp.BasketService.HttpApi.Host/BasketServiceHttpApiHostModule.cs index 34d3b0d9..e947d96a 100644 --- a/services/basket/src/EShopOnAbp.BasketService.HttpApi.Host/BasketServiceHttpApiHostModule.cs +++ b/services/basket/src/EShopOnAbp.BasketService.HttpApi.Host/BasketServiceHttpApiHostModule.cs @@ -9,6 +9,7 @@ using System.Collections.Generic; using System.Linq; using Volo.Abp; using Volo.Abp.AspNetCore.Mvc; +using Volo.Abp.AspNetCore.Mvc.AntiForgery; using Volo.Abp.Modularity; namespace EShopOnAbp.BasketService @@ -27,7 +28,7 @@ namespace EShopOnAbp.BasketService var configuration = context.Services.GetConfiguration(); var hostingEnvironment = context.Services.GetHostingEnvironment(); - JwtBearerConfigurationHelper.Configure(context, "AdministrationService"); + JwtBearerConfigurationHelper.Configure(context, "AdministrationService"); //TODO: Should be "BasketService", but didn't work :( // SwaggerConfigurationHelper.Configure(context, "Basket Service API"); SwaggerWithAuthConfigurationHelper.Configure( @@ -68,6 +69,11 @@ namespace EShopOnAbp.BasketService opts.RemoteServiceName = "Basket"; }); }); + + Configure(options => + { + options.AutoValidate = false; + }); } public override void OnApplicationInitialization(ApplicationInitializationContext context) @@ -88,6 +94,10 @@ namespace EShopOnAbp.BasketService // app.UseHttpMetrics(); app.UseAuthentication(); app.UseAbpClaimsMap(); + app.Use(async (httpContext, func) => + { + await func(); + }); app.UseAuthorization(); app.UseSwagger(); app.UseSwaggerUI(options => { options.SwaggerEndpoint("/swagger/v1/swagger.json", "Basket Service API"); }); diff --git a/services/basket/src/EShopOnAbp.BasketService.HttpApi.Host/appsettings.json b/services/basket/src/EShopOnAbp.BasketService.HttpApi.Host/appsettings.json index 0c1d6898..a3c983a3 100644 --- a/services/basket/src/EShopOnAbp.BasketService.HttpApi.Host/appsettings.json +++ b/services/basket/src/EShopOnAbp.BasketService.HttpApi.Host/appsettings.json @@ -1,7 +1,7 @@ { "App": { "SelfUrl": "https://localhost:44355", - "CorsOrigins": "https://localhost:44372,https://localhost:44373" + "CorsOrigins": "https://localhost:44372,https://localhost:44373,https://localhost:44335" }, "AuthServer": { "Authority": "https://localhost:44330", @@ -23,7 +23,7 @@ }, "AllowedHosts": "*", "ConnectionStrings": { - "AdministrationService": "Server=localhost,1434;Database=EShopOnAbp_Administration;User Id=sa;password=myPassw0rd;MultipleActiveResultSets=true" + "AdministrationService": "User ID=postgres;Password=myPassw0rd;Host=localhost;Port=5432;Database=EShopOnAbp_Administration;Pooling=false;" }, "StringEncryption": { "DefaultPassPhrase": "3PfxLgcuymUUbihz" diff --git a/services/catalog/src/EShopOnAbp.CatalogService.Application.Contracts/Products/IPublicProductAppService.cs b/services/catalog/src/EShopOnAbp.CatalogService.Application.Contracts/Products/IPublicProductAppService.cs index d9c71793..d9bf4500 100644 --- a/services/catalog/src/EShopOnAbp.CatalogService.Application.Contracts/Products/IPublicProductAppService.cs +++ b/services/catalog/src/EShopOnAbp.CatalogService.Application.Contracts/Products/IPublicProductAppService.cs @@ -1,4 +1,5 @@ -using System.Threading.Tasks; +using System; +using System.Threading.Tasks; using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Services; @@ -7,5 +8,6 @@ namespace EShopOnAbp.CatalogService.Products public interface IPublicProductAppService : IApplicationService { Task> GetListAsync(); + Task GetAsync(Guid id); } } \ No newline at end of file diff --git a/services/catalog/src/EShopOnAbp.CatalogService.Application/Products/PublicProductAppService.cs b/services/catalog/src/EShopOnAbp.CatalogService.Application/Products/PublicProductAppService.cs index 99eae783..feb6154c 100644 --- a/services/catalog/src/EShopOnAbp.CatalogService.Application/Products/PublicProductAppService.cs +++ b/services/catalog/src/EShopOnAbp.CatalogService.Application/Products/PublicProductAppService.cs @@ -24,5 +24,11 @@ namespace EShopOnAbp.CatalogService.Products ) ); } + + public async Task GetAsync(Guid id) + { + var product = await _productRepository.GetAsync(id); + return ObjectMapper.Map(product); + } } } diff --git a/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Client/ClientProxies/PublicProductClientProxy.Generated.cs b/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Client/ClientProxies/PublicProductClientProxy.Generated.cs index f599fd74..a0ed4b96 100644 --- a/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Client/ClientProxies/PublicProductClientProxy.Generated.cs +++ b/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Client/ClientProxies/PublicProductClientProxy.Generated.cs @@ -19,5 +19,13 @@ namespace EShopOnAbp.CatalogService.Products.ClientProxies { return await RequestAsync>(nameof(GetListAsync)); } + + public virtual async Task GetAsync(Guid id) + { + return await RequestAsync(nameof(GetAsync), new ClientProxyRequestTypeValue + { + { typeof(Guid), id } + }); + } } } diff --git a/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Client/ClientProxies/catalog-generate-proxy.json b/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Client/ClientProxies/catalog-generate-proxy.json index 3c140dbc..e5746deb 100644 --- a/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Client/ClientProxies/catalog-generate-proxy.json +++ b/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Client/ClientProxies/catalog-generate-proxy.json @@ -302,6 +302,43 @@ }, "allowAnonymous": null, "implementFrom": "EShopOnAbp.CatalogService.Products.IPublicProductAppService" + }, + "GetAsyncById": { + "uniqueName": "GetAsyncById", + "name": "GetAsync", + "httpMethod": "GET", + "url": "api/catalog/public-product/{id}", + "supportedVersions": [], + "parametersOnMethod": [ + { + "name": "id", + "typeAsString": "System.Guid, System.Private.CoreLib", + "type": "System.Guid", + "typeSimple": "string", + "isOptional": false, + "defaultValue": null + } + ], + "parameters": [ + { + "nameOnMethod": "id", + "name": "id", + "jsonName": null, + "type": "System.Guid", + "typeSimple": "string", + "isOptional": false, + "defaultValue": null, + "constraintTypes": [], + "bindingSourceId": "Path", + "descriptorName": "" + } + ], + "returnValue": { + "type": "EShopOnAbp.CatalogService.Products.ProductDto", + "typeSimple": "EShopOnAbp.CatalogService.Products.ProductDto" + }, + "allowAnonymous": null, + "implementFrom": "EShopOnAbp.CatalogService.Products.IPublicProductAppService" } } } diff --git a/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/CatalogServiceHttpApiHostModule.cs b/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/CatalogServiceHttpApiHostModule.cs index 44f2d53f..647de377 100644 --- a/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/CatalogServiceHttpApiHostModule.cs +++ b/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/CatalogServiceHttpApiHostModule.cs @@ -30,7 +30,7 @@ namespace EShopOnAbp.CatalogService { var configuration = context.Services.GetConfiguration(); - JwtBearerConfigurationHelper.Configure(context, "CatalogService"); + JwtBearerConfigurationHelper.Configure(context, "AdministrationService"); //TODO: Should be "CatalogService", but didn't work :( // SwaggerConfigurationHelper.Configure(context, "Catalog Service API"); SwaggerWithAuthConfigurationHelper.Configure( diff --git a/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/appsettings.json b/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/appsettings.json index b95fa07c..4f253c51 100644 --- a/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/appsettings.json +++ b/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/appsettings.json @@ -1,7 +1,7 @@ { "App": { "SelfUrl": "https://localhost:44354", - "CorsOrigins": "https://localhost:44372,https://localhost:44373" + "CorsOrigins": "https://localhost:44372,https://localhost:44373,https://localhost:44335" }, "AuthServer": { "Authority": "https://localhost:44330",