From 0f7e4d787e4b842c93614e8e668f24701742726c Mon Sep 17 00:00:00 2001 From: Galip Tolga Erdem Date: Tue, 21 Dec 2021 11:12:43 +0300 Subject: [PATCH 01/22] moved catalog to public index page --- .../EShopOnAbpPublicWebMenuContributor.cs | 10 -- .../Menus/EShopOnAbpPublicWebMenus.cs | 1 - .../EShopOnAbp.PublicWeb/Pages/Index.cshtml | 122 ++++++++---------- .../Pages/Index.cshtml.cs | 14 +- .../Pages/{Catalog.css => index.css} | 0 .../Pages/{Catalog.cshtml.js => index.js} | 5 +- 6 files changed, 70 insertions(+), 82 deletions(-) rename apps/public-web/src/EShopOnAbp.PublicWeb/Pages/{Catalog.css => index.css} (100%) rename apps/public-web/src/EShopOnAbp.PublicWeb/Pages/{Catalog.cshtml.js => index.js} (93%) diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/Menus/EShopOnAbpPublicWebMenuContributor.cs b/apps/public-web/src/EShopOnAbp.PublicWeb/Menus/EShopOnAbpPublicWebMenuContributor.cs index f78e4c34..f2ea9b52 100644 --- a/apps/public-web/src/EShopOnAbp.PublicWeb/Menus/EShopOnAbpPublicWebMenuContributor.cs +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/Menus/EShopOnAbpPublicWebMenuContributor.cs @@ -44,16 +44,6 @@ namespace EShopOnAbp.PublicWeb.Menus order: 0 ) ); - // Products - context.Menu.AddItem( - new ApplicationMenuItem( - EShopOnAbpPublicWebMenus.Catalog, - l["Catalog"], - "/Catalog", - icon: "fa fa-product-hunt", - order: 1 - ) - ); return Task.CompletedTask; } diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/Menus/EShopOnAbpPublicWebMenus.cs b/apps/public-web/src/EShopOnAbp.PublicWeb/Menus/EShopOnAbpPublicWebMenus.cs index 0c9dc4e4..cb80e44b 100644 --- a/apps/public-web/src/EShopOnAbp.PublicWeb/Menus/EShopOnAbpPublicWebMenus.cs +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/Menus/EShopOnAbpPublicWebMenus.cs @@ -5,6 +5,5 @@ private const string Prefix = "EShopOnAbp.Public"; public const string HomePage = Prefix + ".HomePage"; - public const string Catalog = Prefix + ".Catalog"; } } diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/Pages/Index.cshtml b/apps/public-web/src/EShopOnAbp.PublicWeb/Pages/Index.cshtml index 5c9faabd..7d32de7b 100644 --- a/apps/public-web/src/EShopOnAbp.PublicWeb/Pages/Index.cshtml +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/Pages/Index.cshtml @@ -1,72 +1,62 @@ @page @model EShopOnAbp.PublicWeb.Pages.IndexModel +@{ + const int productsColumnSize = 3; +} - - - - - - - - - +@section styles{ + +} +@section scripts { + + +} -
-
-
- -

Heading

-

Vestibulum id ligula porta.

-

Donec sed odio dui. Etiam porta sem malesuada magna mollis euismod. Morbi leo risus, porta ac consectetur ac, vestibulum at eros. Praesent commodo cursus magna.

-

View details »

-
-
- -

Heading

-

Nulla non metus auctor.

-

Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit.Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh.

-

View details »

-
-
- -

Heading

-

Metus auctor fringilla.

-

Donec sed odio dui. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, sit amet risus.

-

View details »

-
-
- -
-
-

First featurette heading. It'll blow your mind.

-

Donec ullamcorper nulla non metus auctor fringilla. Vestibulum id ligula porta felis euismod semper.

-

Praesent commodo cursus magna, vel scelerisque nisl consectetur. Vestibulum id ligula porta felis euismod semper. Fusce dapibus, tellus ac cursus commodo.

-
-
- -
-
- -
-
-

Oh yeah, it's that good. See for yourself.

-

Donec ullamcorper nulla non metus auctor fringilla. Vestibulum id ligula porta felis euismod semper.

-

Praesent commodo cursus magna, vel scelerisque nisl consectetur. Vestibulum id ligula porta felis euismod semper. Fusce dapibus, tellus ac cursus commodo.

-
-
- +
+
+

Our Products

+
+ @for (int i = 0; i < Math.Ceiling(Model.Products.Count / (double)productsColumnSize); i++) + { + + @for (int j = 0; j < productsColumnSize; j++) + { + + @if ((i * productsColumnSize) + j < Model.Products.Count) + { + var product = Model.Products[(i * productsColumnSize) + j]; +
+ @if (!product.ImageName.IsNullOrEmpty()) + { + + } + else + { + + } +
+
+
+
@product.Name
+ @product.Code - Stock count: @product.StockCount +
+
+
+ $@product.Price +
+
+
+ } +
+ } +
+ }
- -
-
-

And lastly, this one. Checkmate.

-

Donec ullamcorper nulla non metus auctor fringilla. Vestibulum id ligula porta felis euismod semper.

-

Praesent commodo cursus magna, vel scelerisque nisl consectetur. Vestibulum id ligula porta felis euismod semper. Fusce dapibus, tellus ac cursus commodo.

-
-
- -
-
- -
+ @*
*@ + @*

Your Basket

*@ + @*
*@ + @* @await Component.InvokeAsync(typeof(BasketWidgetViewComponent)) *@ + @*
*@ + @*
*@ +
\ No newline at end of file diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/Pages/Index.cshtml.cs b/apps/public-web/src/EShopOnAbp.PublicWeb/Pages/Index.cshtml.cs index 22c79292..9e02dd2c 100644 --- a/apps/public-web/src/EShopOnAbp.PublicWeb/Pages/Index.cshtml.cs +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/Pages/Index.cshtml.cs @@ -1,4 +1,6 @@ -using System.Threading.Tasks; +using System.Collections.Generic; +using System.Threading.Tasks; +using EShopOnAbp.CatalogService.Products; using Microsoft.AspNetCore.Authentication; using Volo.Abp.AspNetCore.Mvc.UI.RazorPages; @@ -6,9 +8,17 @@ namespace EShopOnAbp.PublicWeb.Pages { public class IndexModel : AbpPageModel { - public void OnGet() + public IReadOnlyList Products { get; private set; } + private readonly IPublicProductAppService _productAppService; + + public IndexModel(IPublicProductAppService productAppService) { + _productAppService = productAppService; + } + public async Task OnGet() + { + Products = (await _productAppService.GetListAsync()).Items; } public async Task OnPostLoginAsync() diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/Pages/Catalog.css b/apps/public-web/src/EShopOnAbp.PublicWeb/Pages/index.css similarity index 100% rename from apps/public-web/src/EShopOnAbp.PublicWeb/Pages/Catalog.css rename to apps/public-web/src/EShopOnAbp.PublicWeb/Pages/index.css diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/Pages/Catalog.cshtml.js b/apps/public-web/src/EShopOnAbp.PublicWeb/Pages/index.js similarity index 93% rename from apps/public-web/src/EShopOnAbp.PublicWeb/Pages/Catalog.cshtml.js rename to apps/public-web/src/EShopOnAbp.PublicWeb/Pages/index.js index fefd8436..c3bad1c6 100644 --- a/apps/public-web/src/EShopOnAbp.PublicWeb/Pages/Catalog.cshtml.js +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/Pages/index.js @@ -1,8 +1,7 @@ -(function(){ +(function(){ $(function(){ + var basketWidget = new abp.WidgetManager('#BasketArea'); - var basketWidget = new abp.WidgetManager('#BasketArea'); - $('.product-list-item').click(function(){ var $this = $(this); var productId = $this.attr('data-product-id'); From 205e68fa62f5347f85b6bba736bfb752c78fe3dc Mon Sep 17 00:00:00 2001 From: Galip Tolga Erdem Date: Tue, 21 Dec 2021 12:02:58 +0300 Subject: [PATCH 02/22] added cart widget This is not done since it requires CurrentUserId to perform basket requests. --- .../Cart/CartWidgetScriptContributor.cs | 15 +++++++++ .../Toolbar/Cart/CartWidgetViewComponent.cs | 29 +++++++++++++++++ .../Components/Toolbar/Cart/Default.cshtml | 10 ++++++ .../EShopOnAbpPublicWebToolbarContributor.cs | 3 ++ .../wwwroot/components/cart/cart-widget.css | 0 .../wwwroot/components/cart/cart-widget.js | 32 +++++++++++++++++++ 6 files changed, 89 insertions(+) create mode 100644 apps/public-web/src/EShopOnAbp.PublicWeb/Components/Toolbar/Cart/CartWidgetScriptContributor.cs create mode 100644 apps/public-web/src/EShopOnAbp.PublicWeb/Components/Toolbar/Cart/CartWidgetViewComponent.cs create mode 100644 apps/public-web/src/EShopOnAbp.PublicWeb/Components/Toolbar/Cart/Default.cshtml create mode 100644 apps/public-web/src/EShopOnAbp.PublicWeb/wwwroot/components/cart/cart-widget.css create mode 100644 apps/public-web/src/EShopOnAbp.PublicWeb/wwwroot/components/cart/cart-widget.js diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Toolbar/Cart/CartWidgetScriptContributor.cs b/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Toolbar/Cart/CartWidgetScriptContributor.cs new file mode 100644 index 00000000..b7027a15 --- /dev/null +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Toolbar/Cart/CartWidgetScriptContributor.cs @@ -0,0 +1,15 @@ +using System.Collections.Generic; +using Volo.Abp.AspNetCore.Mvc.UI.Bundling; +using Volo.Abp.AspNetCore.Mvc.UI.Packages.SignalR; +using Volo.Abp.Modularity; + +namespace EShopOnAbp.PublicWeb.Components.Toolbar.Cart; + +[DependsOn(typeof(SignalRBrowserScriptContributor))] +public class CartWidgetScriptContributor: BundleContributor +{ + public override void ConfigureBundle(BundleConfigurationContext context) + { + context.Files.AddIfNotContains("/components/cart/cart-widget.js"); + } +} \ No newline at end of file diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Toolbar/Cart/CartWidgetViewComponent.cs b/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Toolbar/Cart/CartWidgetViewComponent.cs new file mode 100644 index 00000000..5211dea0 --- /dev/null +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Toolbar/Cart/CartWidgetViewComponent.cs @@ -0,0 +1,29 @@ +using System.Threading.Tasks; +using EShopOnAbp.BasketService; +using Microsoft.AspNetCore.Mvc; +using Volo.Abp.AspNetCore.Mvc; +using Volo.Abp.AspNetCore.Mvc.UI.Widgets; + +namespace EShopOnAbp.PublicWeb.Components.Toolbar.Cart; + +[Widget( + AutoInitialize = true, + RefreshUrl = "/Widgets/Cart", + StyleFiles = new[] {"/components/cart/cart-widget.css"}, + ScriptTypes = new[] {typeof(CartWidgetScriptContributor)} +)] +public class CartWidgetViewComponent : AbpViewComponent +{ + private readonly IBasketAppService _basketAppService; + + public CartWidgetViewComponent(IBasketAppService basketAppService) + { + _basketAppService = basketAppService; + } + + public async Task InvokeAsync() + { + var basketDto = await _basketAppService.GetAsync(); + return View("~/Components/Toolbar/Cart/Default.cshtml", basketDto); + } +} \ No newline at end of file diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Toolbar/Cart/Default.cshtml b/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Toolbar/Cart/Default.cshtml new file mode 100644 index 00000000..88c4b6fa --- /dev/null +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Toolbar/Cart/Default.cshtml @@ -0,0 +1,10 @@ +@using EShopOnAbp.Localization +@using Microsoft.AspNetCore.Mvc.Localization +@inject IHtmlLocalizer L +@model EShopOnAbp.BasketService.BasketDto + +
+ + @L["Toolbar:Basket"] @Model.Items.Count + +
diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/Menus/EShopOnAbpPublicWebToolbarContributor.cs b/apps/public-web/src/EShopOnAbp.PublicWeb/Menus/EShopOnAbpPublicWebToolbarContributor.cs index cf1a59a0..03342e66 100644 --- a/apps/public-web/src/EShopOnAbp.PublicWeb/Menus/EShopOnAbpPublicWebToolbarContributor.cs +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/Menus/EShopOnAbpPublicWebToolbarContributor.cs @@ -1,4 +1,5 @@ using System.Threading.Tasks; +using EShopOnAbp.PublicWeb.Components.Toolbar.Cart; using EShopOnAbp.PublicWeb.Components.Toolbar.LoginLink; using Microsoft.Extensions.DependencyInjection; using Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Toolbars; @@ -19,6 +20,8 @@ namespace EShopOnAbp.PublicWeb.Menus { context.Toolbar.Items.Add(new ToolbarItem(typeof(LoginLinkViewComponent))); } + + context.Toolbar.Items.Add(new ToolbarItem(typeof(CartWidgetViewComponent))); return Task.CompletedTask; } diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/wwwroot/components/cart/cart-widget.css b/apps/public-web/src/EShopOnAbp.PublicWeb/wwwroot/components/cart/cart-widget.css new file mode 100644 index 00000000..e69de29b diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/wwwroot/components/cart/cart-widget.js b/apps/public-web/src/EShopOnAbp.PublicWeb/wwwroot/components/cart/cart-widget.js new file mode 100644 index 00000000..d3316681 --- /dev/null +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/wwwroot/components/cart/cart-widget.js @@ -0,0 +1,32 @@ +(function (){ + var connection = new signalR.HubConnectionBuilder() + .withUrl("/signalr-hubs/cart") + .build(); + + connection.on("BasketProductUpdated", function (data) { + var widgetManager = $wrapper.data('abp-widget-manager'); + debugger; + $('#data-cart-count-id').text(data); + widgetManager.refresh(); + abp.notify.info('The product "' + data.productName + '" has been changed!', 'Your basket has been updated!'); + + // $('.basket-list') + // .closest('.abp-widget-wrapper') + // .each(function(){ + // var $wrapper = $(this); + // if ($wrapper.find('[data-product-id=' + data.productId + ']').length){ + // var widgetManager = new abp.WidgetManager({ + // wrapper: $wrapper + // }); + // widgetManager.refresh(); + // abp.notify.info('The product "' + data.productName + '" has been changed!', 'Your basket has been updated!'); + // } + // }); + }); + + connection.start().then(function () { + }).catch(function (err) { + return console.error(err.toString()); + }); + +})(); \ No newline at end of file From 7ebac77d11f6da67129960fdd971360416a37454 Mon Sep 17 00:00:00 2001 From: Galip Tolga Erdem Date: Tue, 21 Dec 2021 15:20:06 +0300 Subject: [PATCH 03/22] updated basket appservice with new method --- .../wwwroot/client-proxies/basket-proxy.js | 7 +++ .../IBasketAppService.cs | 4 +- .../BasketAppService.cs | 23 ++++++---- .../BasketServiceDomainModule.cs | 2 +- .../BasketServiceHttpApiClientModule.cs | 2 +- .../BasketClientProxy.Generated.cs | 8 ++++ .../ClientProxies/basket-generate-proxy.json | 45 +++++++++++++++++-- 7 files changed, 75 insertions(+), 16 deletions(-) diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/wwwroot/client-proxies/basket-proxy.js b/apps/public-web/src/EShopOnAbp.PublicWeb/wwwroot/client-proxies/basket-proxy.js index c3877617..ec6cf88e 100644 --- a/apps/public-web/src/EShopOnAbp.PublicWeb/wwwroot/client-proxies/basket-proxy.js +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/wwwroot/client-proxies/basket-proxy.js @@ -18,6 +18,13 @@ }, ajaxParams)); }; + eShopOnAbp.basketService.basket.getByAnonymousUserId = function(id, ajaxParams) { + return abp.ajax($.extend(true, { + url: abp.appPath + 'api/basket/basket/' + id + '/by-anonymous-user-id', + type: 'GET' + }, ajaxParams)); + }; + eShopOnAbp.basketService.basket.addProduct = function(input, ajaxParams) { return abp.ajax($.extend(true, { url: abp.appPath + 'api/basket/basket/product', diff --git a/services/basket/src/EShopOnAbp.BasketService.Application.Contracts/IBasketAppService.cs b/services/basket/src/EShopOnAbp.BasketService.Application.Contracts/IBasketAppService.cs index 00266aa7..55a4ba0a 100644 --- a/services/basket/src/EShopOnAbp.BasketService.Application.Contracts/IBasketAppService.cs +++ b/services/basket/src/EShopOnAbp.BasketService.Application.Contracts/IBasketAppService.cs @@ -1,4 +1,5 @@ -using System.Threading.Tasks; +using System; +using System.Threading.Tasks; using Volo.Abp.Application.Services; namespace EShopOnAbp.BasketService; @@ -6,6 +7,7 @@ namespace EShopOnAbp.BasketService; public interface IBasketAppService : IApplicationService { Task GetAsync(); + Task GetByAnonymousUserIdAsync(Guid id); Task AddProductAsync(AddProductDto input); Task RemoveProductAsync(RemoveProductDto input); Task PurchaseAsync(); diff --git a/services/basket/src/EShopOnAbp.BasketService.Application/BasketAppService.cs b/services/basket/src/EShopOnAbp.BasketService.Application/BasketAppService.cs index 76d77784..c7e06212 100644 --- a/services/basket/src/EShopOnAbp.BasketService.Application/BasketAppService.cs +++ b/services/basket/src/EShopOnAbp.BasketService.Application/BasketAppService.cs @@ -10,7 +10,6 @@ using Volo.Abp.EventBus.Distributed; namespace EShopOnAbp.BasketService; -[Authorize] public class BasketAppService : BasketServiceAppService, IBasketAppService { private readonly IBasketRepository _basketRepository; @@ -26,13 +25,19 @@ public class BasketAppService : BasketServiceAppService, IBasketAppService _basketProductService = basketProductService; _distributedEventBus = distributedEventBus; } - + public async Task GetAsync() { var basket = await _basketRepository.GetAsync(CurrentUser.GetId()); return await GetBasketDtoAsync(basket); } + public async Task GetByAnonymousUserIdAsync(Guid id) + { + var basket = await _basketRepository.GetAsync(id); + return await GetBasketDtoAsync(basket); + } + public async Task AddProductAsync(AddProductDto input) { var basket = await _basketRepository.GetAsync(CurrentUser.GetId()); @@ -42,11 +47,11 @@ public class BasketAppService : BasketServiceAppService, IBasketAppService { throw new UserFriendlyException("There is not enough product in stock, sorry :("); } - + basket.AddProduct(product.Id); await _basketRepository.UpdateAsync(basket); - + return await GetBasketDtoAsync(basket); } @@ -54,11 +59,11 @@ public class BasketAppService : BasketServiceAppService, IBasketAppService { var basket = await _basketRepository.GetAsync(CurrentUser.GetId()); var product = await _basketProductService.GetAsync(input.ProductId); - + basket.RemoveProduct(product.Id, input.Count); - + await _basketRepository.UpdateAsync(basket); - + return await GetBasketDtoAsync(basket); } @@ -85,7 +90,7 @@ public class BasketAppService : BasketServiceAppService, IBasketAppService productDto = await _basketProductService.GetAsync(basketItem.ProductId); products[productDto.Id] = productDto; } - + //Removing the products if not available in the stock if (basketItem.Count > productDto.StockCount) { @@ -110,7 +115,7 @@ public class BasketAppService : BasketServiceAppService, IBasketAppService { await _basketRepository.UpdateAsync(basket); } - + return basketDto; } } \ No newline at end of file diff --git a/services/basket/src/EShopOnAbp.BasketService.Domain/BasketServiceDomainModule.cs b/services/basket/src/EShopOnAbp.BasketService.Domain/BasketServiceDomainModule.cs index e3992597..39230157 100644 --- a/services/basket/src/EShopOnAbp.BasketService.Domain/BasketServiceDomainModule.cs +++ b/services/basket/src/EShopOnAbp.BasketService.Domain/BasketServiceDomainModule.cs @@ -11,7 +11,7 @@ namespace EShopOnAbp.BasketService typeof(AbpDddDomainModule), typeof(AbpCachingModule) )] - public class BasketServiceDomainModule : AbpModule + public class BasketServiceDomainModule : AbpModule { public override void ConfigureServices(ServiceConfigurationContext context) { diff --git a/services/basket/src/EShopOnAbp.BasketService.HttpApi.Client/BasketServiceHttpApiClientModule.cs b/services/basket/src/EShopOnAbp.BasketService.HttpApi.Client/BasketServiceHttpApiClientModule.cs index 36b13872..093c7352 100644 --- a/services/basket/src/EShopOnAbp.BasketService.HttpApi.Client/BasketServiceHttpApiClientModule.cs +++ b/services/basket/src/EShopOnAbp.BasketService.HttpApi.Client/BasketServiceHttpApiClientModule.cs @@ -26,4 +26,4 @@ namespace EShopOnAbp.BasketService }); } } -} +} \ No newline at end of file diff --git a/services/basket/src/EShopOnAbp.BasketService.HttpApi.Client/ClientProxies/BasketClientProxy.Generated.cs b/services/basket/src/EShopOnAbp.BasketService.HttpApi.Client/ClientProxies/BasketClientProxy.Generated.cs index 014328ed..6a723592 100644 --- a/services/basket/src/EShopOnAbp.BasketService.HttpApi.Client/ClientProxies/BasketClientProxy.Generated.cs +++ b/services/basket/src/EShopOnAbp.BasketService.HttpApi.Client/ClientProxies/BasketClientProxy.Generated.cs @@ -20,6 +20,14 @@ namespace EShopOnAbp.BasketService.ClientProxies return await RequestAsync(nameof(GetAsync)); } + public virtual async Task GetByAnonymousUserIdAsync(Guid id) + { + return await RequestAsync(nameof(GetByAnonymousUserIdAsync), new ClientProxyRequestTypeValue + { + { typeof(Guid), id } + }); + } + public virtual async Task AddProductAsync(AddProductDto input) { return await RequestAsync(nameof(AddProductAsync), new ClientProxyRequestTypeValue diff --git a/services/basket/src/EShopOnAbp.BasketService.HttpApi.Client/ClientProxies/basket-generate-proxy.json b/services/basket/src/EShopOnAbp.BasketService.HttpApi.Client/ClientProxies/basket-generate-proxy.json index c6f742a4..0ff4ec2a 100644 --- a/services/basket/src/EShopOnAbp.BasketService.HttpApi.Client/ClientProxies/basket-generate-proxy.json +++ b/services/basket/src/EShopOnAbp.BasketService.HttpApi.Client/ClientProxies/basket-generate-proxy.json @@ -35,7 +35,44 @@ "type": "EShopOnAbp.BasketService.BasketDto", "typeSimple": "EShopOnAbp.BasketService.BasketDto" }, - "allowAnonymous": false, + "allowAnonymous": null, + "implementFrom": "EShopOnAbp.BasketService.IBasketAppService" + }, + "GetByAnonymousUserIdAsyncById": { + "uniqueName": "GetByAnonymousUserIdAsyncById", + "name": "GetByAnonymousUserIdAsync", + "httpMethod": "GET", + "url": "api/basket/basket/{id}/by-anonymous-user-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.BasketService.BasketDto", + "typeSimple": "EShopOnAbp.BasketService.BasketDto" + }, + "allowAnonymous": null, "implementFrom": "EShopOnAbp.BasketService.IBasketAppService" }, "AddProductAsyncByInput": { @@ -72,7 +109,7 @@ "type": "EShopOnAbp.BasketService.BasketDto", "typeSimple": "EShopOnAbp.BasketService.BasketDto" }, - "allowAnonymous": false, + "allowAnonymous": null, "implementFrom": "EShopOnAbp.BasketService.IBasketAppService" }, "RemoveProductAsyncByInput": { @@ -121,7 +158,7 @@ "type": "EShopOnAbp.BasketService.BasketDto", "typeSimple": "EShopOnAbp.BasketService.BasketDto" }, - "allowAnonymous": false, + "allowAnonymous": null, "implementFrom": "EShopOnAbp.BasketService.IBasketAppService" }, "PurchaseAsync": { @@ -136,7 +173,7 @@ "type": "System.Void", "typeSimple": "System.Void" }, - "allowAnonymous": false, + "allowAnonymous": null, "implementFrom": "EShopOnAbp.BasketService.IBasketAppService" } } From 95f482c76b2228000d339b4c0e1ce45f662f72bd Mon Sep 17 00:00:00 2001 From: Galip Tolga Erdem Date: Tue, 21 Dec 2021 15:20:16 +0300 Subject: [PATCH 04/22] added cart-widget --- .../Cart/CartWidgetStyleContributor.cs | 12 +++++ .../Toolbar/Cart/CartWidgetViewComponent.cs | 47 +++++++++++++++++-- .../Components/Toolbar/Cart/Default.cshtml | 20 +++++--- .../src/EShopOnAbp.PublicWeb/Pages/index.js | 14 ++++-- .../wwwroot/components/cart/cart-widget.css | 4 ++ 5 files changed, 82 insertions(+), 15 deletions(-) create mode 100644 apps/public-web/src/EShopOnAbp.PublicWeb/Components/Toolbar/Cart/CartWidgetStyleContributor.cs diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Toolbar/Cart/CartWidgetStyleContributor.cs b/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Toolbar/Cart/CartWidgetStyleContributor.cs new file mode 100644 index 00000000..d0b42f47 --- /dev/null +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Toolbar/Cart/CartWidgetStyleContributor.cs @@ -0,0 +1,12 @@ +using System.Collections.Generic; +using Volo.Abp.AspNetCore.Mvc.UI.Bundling; + +namespace EShopOnAbp.PublicWeb.Components.Toolbar.Cart; + +public class CartWidgetStyleContributor : BundleContributor +{ + public override void ConfigureBundle(BundleConfigurationContext context) + { + context.Files.AddIfNotContains("/components/cart/cart-widget.css"); + } +} \ No newline at end of file diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Toolbar/Cart/CartWidgetViewComponent.cs b/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Toolbar/Cart/CartWidgetViewComponent.cs index 5211dea0..17d54382 100644 --- a/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Toolbar/Cart/CartWidgetViewComponent.cs +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Toolbar/Cart/CartWidgetViewComponent.cs @@ -1,29 +1,68 @@ -using System.Threading.Tasks; +using System; +using System.Security.Claims; +using System.Threading.Tasks; using EShopOnAbp.BasketService; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Volo.Abp.AspNetCore.Mvc; using Volo.Abp.AspNetCore.Mvc.UI.Widgets; +using Volo.Abp.Guids; +using Volo.Abp.Security.Claims; +using Volo.Abp.Users; namespace EShopOnAbp.PublicWeb.Components.Toolbar.Cart; [Widget( AutoInitialize = true, RefreshUrl = "/Widgets/Cart", - StyleFiles = new[] {"/components/cart/cart-widget.css"}, + StyleTypes = new[] {typeof(CartWidgetStyleContributor)}, ScriptTypes = new[] {typeof(CartWidgetScriptContributor)} )] public class CartWidgetViewComponent : AbpViewComponent { private readonly IBasketAppService _basketAppService; + private readonly IGuidGenerator _guidGenerator; + private readonly ICurrentUser _currentUser; + private readonly IHttpContextAccessor _httpContextAccessor; - public CartWidgetViewComponent(IBasketAppService basketAppService) + protected HttpContext HttpContext => _httpContextAccessor.HttpContext; + protected const string AnonymousUserCookieName = "eshop_anonymousId"; + + public CartWidgetViewComponent( + IBasketAppService basketAppService, + ICurrentUser currentUser, + IGuidGenerator guidGenerator, + IHttpContextAccessor httpContextAccessor) { _basketAppService = basketAppService; + _currentUser = currentUser; + _httpContextAccessor = httpContextAccessor; + _guidGenerator = guidGenerator; } public async Task InvokeAsync() { - var basketDto = await _basketAppService.GetAsync(); + BasketDto basketDto = new BasketDto(); + try + { + if (!_currentUser.IsAuthenticated) + { + var anonymousUserId = _guidGenerator.Create(); + HttpContext.Response.Cookies.Append(AnonymousUserCookieName, anonymousUserId.ToString()); + basketDto = await _basketAppService.GetByAnonymousUserIdAsync(anonymousUserId); + } + else + { + //TODO: Merge with anonymously stored cart if exist + basketDto = await _basketAppService.GetAsync(); + } + } + catch (Exception e) + { + basketDto = null; + Console.WriteLine(e); + } + return View("~/Components/Toolbar/Cart/Default.cshtml", basketDto); } } \ No newline at end of file diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Toolbar/Cart/Default.cshtml b/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Toolbar/Cart/Default.cshtml index 88c4b6fa..f4e72890 100644 --- a/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Toolbar/Cart/Default.cshtml +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Toolbar/Cart/Default.cshtml @@ -1,10 +1,16 @@ -@using EShopOnAbp.Localization +@using EShopOnAbp.BasketService.Localization @using Microsoft.AspNetCore.Mvc.Localization -@inject IHtmlLocalizer L +@inject IHtmlLocalizer L @model EShopOnAbp.BasketService.BasketDto - -
- - @L["Toolbar:Basket"] @Model.Items.Count + + } +
\ No newline at end of file diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/Pages/index.js b/apps/public-web/src/EShopOnAbp.PublicWeb/Pages/index.js index c3bad1c6..24c02b0f 100644 --- a/apps/public-web/src/EShopOnAbp.PublicWeb/Pages/index.js +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/Pages/index.js @@ -1,13 +1,19 @@ -(function(){ - $(function(){ +(function () { + //set anonymous user + let anonymousId = abp.utils.getCookieValue("eshop_anonymousId"); + if (anonymousId != null) { + console.info("Anonymous user set:" + anonymousId); + } + + $(function () { var basketWidget = new abp.WidgetManager('#BasketArea'); - $('.product-list-item').click(function(){ + $('.product-list-item').click(function () { var $this = $(this); var productId = $this.attr('data-product-id'); eShopOnAbp.basketService.basket.addProduct({ productId: productId - }).then(function(){ + }).then(function () { basketWidget.refresh(); abp.notify.success("Added product to your basket.", "Successfully added"); }); diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/wwwroot/components/cart/cart-widget.css b/apps/public-web/src/EShopOnAbp.PublicWeb/wwwroot/components/cart/cart-widget.css index e69de29b..083ca3ea 100644 --- a/apps/public-web/src/EShopOnAbp.PublicWeb/wwwroot/components/cart/cart-widget.css +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/wwwroot/components/cart/cart-widget.css @@ -0,0 +1,4 @@ +.cart-error { + vertical-align: -webkit-baseline-middle; + color: rgba(255, 255, 255, 0.75); +} \ No newline at end of file From 93df962044552a711befabbd6b89fdce91edb9a4 Mon Sep 17 00:00:00 2001 From: Galip Tolga Erdem Date: Tue, 21 Dec 2021 15:21:40 +0300 Subject: [PATCH 05/22] updated localization for basket --- .../Localization/BasketService/en.json | 4 +++- .../Localization/BasketService/tr.json | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/services/basket/src/EShopOnAbp.BasketService.Domain.Shared/Localization/BasketService/en.json b/services/basket/src/EShopOnAbp.BasketService.Domain.Shared/Localization/BasketService/en.json index d2a6a983..d1b6949a 100644 --- a/services/basket/src/EShopOnAbp.BasketService.Domain.Shared/Localization/BasketService/en.json +++ b/services/basket/src/EShopOnAbp.BasketService.Domain.Shared/Localization/BasketService/en.json @@ -3,6 +3,8 @@ "texts": { "Menu:Home": "Home", "Welcome": "Welcome", - "LongWelcomeMessage": "Welcome to the application. This is a startup project based on the ABP framework. For more information, visit abp.io." + "LongWelcomeMessage": "Welcome to the application. This is a startup project based on the ABP framework. For more information, visit abp.io.", + "Toolbar:Cart": "My Cart", + "Toolbar:BasketNotLoaded": "Couldn't Load!" } } diff --git a/services/basket/src/EShopOnAbp.BasketService.Domain.Shared/Localization/BasketService/tr.json b/services/basket/src/EShopOnAbp.BasketService.Domain.Shared/Localization/BasketService/tr.json index 2cc911e4..60577366 100644 --- a/services/basket/src/EShopOnAbp.BasketService.Domain.Shared/Localization/BasketService/tr.json +++ b/services/basket/src/EShopOnAbp.BasketService.Domain.Shared/Localization/BasketService/tr.json @@ -3,6 +3,8 @@ "texts": { "Menu:Home": "Ana sayfa", "Welcome": "Hoşgeldiniz", - "LongWelcomeMessage": "Uygulamaya hoşgeldiniz. Bu, ABP framework'ü üzerine bina edilmiş bir başlangıç projesidir. Daha fazla bilgi için abp.io adresini ziyaret edebilirsiniz." + "LongWelcomeMessage": "Uygulamaya hoşgeldiniz. Bu, ABP framework'ü üzerine bina edilmiş bir başlangıç projesidir. Daha fazla bilgi için abp.io adresini ziyaret edebilirsiniz.", + "Toolbar:Cart": "Sepetim", + "Toolbar:BasketNotLoaded": "Sepet Hatası!" } } \ No newline at end of file From a0546fb45f4b5a97117acf19771ce5a6e3bc4e23 Mon Sep 17 00:00:00 2001 From: Galip Tolga Erdem Date: Tue, 21 Dec 2021 15:59:55 +0300 Subject: [PATCH 06/22] basket service update for anonymous users --- .../BasketAppService.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/services/basket/src/EShopOnAbp.BasketService.Application/BasketAppService.cs b/services/basket/src/EShopOnAbp.BasketService.Application/BasketAppService.cs index c7e06212..c404ccb6 100644 --- a/services/basket/src/EShopOnAbp.BasketService.Application/BasketAppService.cs +++ b/services/basket/src/EShopOnAbp.BasketService.Application/BasketAppService.cs @@ -15,6 +15,7 @@ public class BasketAppService : BasketServiceAppService, IBasketAppService private readonly IBasketRepository _basketRepository; private readonly IBasketProductService _basketProductService; private readonly IDistributedEventBus _distributedEventBus; + private Guid _anonymousUserId { get; set; } public BasketAppService( IBasketRepository basketRepository, @@ -35,12 +36,15 @@ public class BasketAppService : BasketServiceAppService, IBasketAppService public async Task GetByAnonymousUserIdAsync(Guid id) { var basket = await _basketRepository.GetAsync(id); + _anonymousUserId = id; return await GetBasketDtoAsync(basket); } public async Task AddProductAsync(AddProductDto input) { - var basket = await _basketRepository.GetAsync(CurrentUser.GetId()); + Guid userId = CurrentUser.IsAuthenticated ? CurrentUser.GetId() : _anonymousUserId; + + var basket = await _basketRepository.GetAsync(userId); var product = await _basketProductService.GetAsync(input.ProductId); if (basket.GetProductCount(product.Id) >= product.StockCount) From 74296d283bcdf9a40a1678fd61c71036bebb2af0 Mon Sep 17 00:00:00 2001 From: Galip Tolga Erdem Date: Tue, 21 Dec 2021 16:00:03 +0300 Subject: [PATCH 07/22] updated cart widget --- .../Components/Basket/BasketWidgetViewComponent.cs | 4 ++-- .../Components/Toolbar/Cart/Default.cshtml | 8 ++++---- .../src/EShopOnAbp.PublicWeb/Pages/Index.cshtml | 2 +- .../src/EShopOnAbp.PublicWeb/Pages/Index.cshtml.cs | 2 +- .../src/EShopOnAbp.PublicWeb/Pages/index.js | 4 ++-- .../wwwroot/components/cart/cart-widget.js | 11 ++++++----- 6 files changed, 16 insertions(+), 15 deletions(-) diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Basket/BasketWidgetViewComponent.cs b/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Basket/BasketWidgetViewComponent.cs index 71e36800..73ea746f 100644 --- a/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Basket/BasketWidgetViewComponent.cs +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Basket/BasketWidgetViewComponent.cs @@ -9,8 +9,8 @@ namespace EShopOnAbp.PublicWeb.Components.Basket; [Widget( AutoInitialize = true, RefreshUrl = "/Widgets/Basket", - StyleFiles = new[] { "/components/basket-widget.css" }, - ScriptTypes = new[] { typeof(BasketWidgetScriptContributor) } + StyleFiles = new[] {"/components/basket-widget.css"}, + ScriptTypes = new[] {typeof(BasketWidgetScriptContributor)} )] public class BasketWidgetViewComponent : AbpViewComponent { diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Toolbar/Cart/Default.cshtml b/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Toolbar/Cart/Default.cshtml index f4e72890..46be8b76 100644 --- a/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Toolbar/Cart/Default.cshtml +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Toolbar/Cart/Default.cshtml @@ -2,15 +2,15 @@ @using Microsoft.AspNetCore.Mvc.Localization @inject IHtmlLocalizer L @model EShopOnAbp.BasketService.BasketDto -