diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/Basket/UserBasketProvider.cs b/apps/public-web/src/EShopOnAbp.PublicWeb/Basket/UserBasketProvider.cs new file mode 100644 index 00000000..2fd7e5de --- /dev/null +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/Basket/UserBasketProvider.cs @@ -0,0 +1,76 @@ +using Castle.Core.Logging; +using EShopOnAbp.BasketService; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Volo.Abp.DependencyInjection; +using Volo.Abp.Guids; +using Volo.Abp.Users; + +namespace EShopOnAbp.PublicWeb.Basket +{ + public class UserBasketProvider : ITransientDependency + { + protected HttpContext HttpContext => httpContextAccessor.HttpContext; + + private readonly IHttpContextAccessor httpContextAccessor; + protected readonly ILogger logger; + protected readonly IGuidGenerator guidGenerator; + protected readonly IBasketAppService basketAppService; + protected readonly ICurrentUser currentUser; + + public UserBasketProvider( + IHttpContextAccessor httpContextAccessor, + ILogger logger, + IGuidGenerator guidGenerator, + IBasketAppService basketAppService, + ICurrentUser currentUser) + { + this.httpContextAccessor = httpContextAccessor; + this.logger = logger; + this.guidGenerator = guidGenerator; + this.basketAppService = basketAppService; + this.currentUser = currentUser; + } + + public virtual async Task GetBasketAsync() + { + try + { + // Get anonymous user id from cookie + HttpContext.Request.Cookies.TryGetValue(CookieConstants.AnonymousUserCookieName, + out string anonymousUserId); + logger.LogInformation($"========= Anonymous User Id from Cookie:{anonymousUserId} ========= "); + + // Generate new id for anonymous user + if (string.IsNullOrEmpty(anonymousUserId)) + { + anonymousUserId = guidGenerator.Create().ToString(); + HttpContext.Response.Cookies.Append(CookieConstants.AnonymousUserCookieName, anonymousUserId); + logger.LogInformation($"========= Generated new User Id:{anonymousUserId} ========= APPENDED TO COOKIE ====== "); + } + + if (!currentUser.IsAuthenticated) + { + logger.LogInformation( + $"========= Get Basket for Anonymous UserId:{anonymousUserId} ========= APPENDED TO COOKIE ====== "); + return await basketAppService.GetByAnonymousUserIdAsync(Guid.Parse(anonymousUserId)); + } + else + { + //TODO: Merge with anonymously stored cart if exist + return await basketAppService.GetAsync(); + } + } + catch (Exception ex) + { + logger.LogError(ex, ex.Message); + return null; + } + } + } +} 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 3659494f..621002ba 100644 --- a/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Basket/BasketWidgetViewComponent.cs +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Basket/BasketWidgetViewComponent.cs @@ -7,6 +7,7 @@ using EShopOnAbp.BasketService; using Microsoft.Extensions.Logging; using Volo.Abp.Guids; using Volo.Abp.Users; +using EShopOnAbp.PublicWeb.Basket; namespace EShopOnAbp.PublicWeb.Components.Basket; @@ -18,59 +19,18 @@ namespace EShopOnAbp.PublicWeb.Components.Basket; )] public class BasketWidgetViewComponent : AbpViewComponent { - private readonly IBasketAppService _basketAppService; - private readonly ICurrentUser _currentUser; - private readonly IGuidGenerator _guidGenerator; - private readonly ILogger _logger; + private readonly UserBasketProvider userBasketProvider; - public BasketWidgetViewComponent( - IBasketAppService basketAppService, - ICurrentUser currentUser, - IGuidGenerator guidGenerator, ILogger logger) + public BasketWidgetViewComponent(UserBasketProvider userBasketProvider) { - _basketAppService = basketAppService; - _currentUser = currentUser; - _guidGenerator = guidGenerator; - _logger = logger; + this.userBasketProvider = userBasketProvider; } public async Task InvokeAsync() { - BasketDto basketDto = new BasketDto(); - try - { - // Get anonymous user id from cookie - HttpContext.Request.Cookies.TryGetValue(CookieConstants.AnonymousUserCookieName, - out string anonymousUserId); - _logger.LogInformation($"========= Anonymous User Id from Cookie:{anonymousUserId} ========= "); - - // Generate new id for anonymous user - if (string.IsNullOrEmpty(anonymousUserId)) - { - anonymousUserId = _guidGenerator.Create().ToString(); - HttpContext.Response.Cookies.Append(CookieConstants.AnonymousUserCookieName, anonymousUserId); - _logger.LogInformation($"========= Generated new User Id:{anonymousUserId} ========= APPENDED TO COOKIE ====== "); - } - - if (!_currentUser.IsAuthenticated) - { - basketDto = await _basketAppService.GetByAnonymousUserIdAsync(Guid.Parse(anonymousUserId)); - _logger.LogInformation( - $"========= Get Basket for Anonymous UserId:{anonymousUserId} ========= APPENDED TO COOKIE ====== "); - _logger.LogInformation($"========= Basket Item Count:{basketDto.Items.Count} ========= "); - } - else - { - //TODO: Merge with anonymously stored cart if exist - basketDto = await _basketAppService.GetAsync(); - } - } - catch (Exception e) - { - basketDto = null; - Console.WriteLine(e); - } - return View("~/Components/Basket/Default.cshtml", basketDto); + return View( + "~/Components/Basket/Default.cshtml", + await userBasketProvider.GetBasketAsync()); } } \ 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 b3b09e8d..ad7c1aea 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,6 +1,7 @@ using System; using System.Threading.Tasks; using EShopOnAbp.BasketService; +using EShopOnAbp.PublicWeb.Basket; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using Volo.Abp.AspNetCore.Mvc; @@ -18,60 +19,17 @@ namespace EShopOnAbp.PublicWeb.Components.Toolbar.Cart; )] public class CartWidgetViewComponent : AbpViewComponent { - private readonly IBasketAppService _basketAppService; - private readonly IGuidGenerator _guidGenerator; - private readonly ICurrentUser _currentUser; - private readonly ILogger _logger; + private readonly UserBasketProvider userBasketProvider; - public CartWidgetViewComponent( - IBasketAppService basketAppService, - ICurrentUser currentUser, - IGuidGenerator guidGenerator, - ILogger logger) + public CartWidgetViewComponent(UserBasketProvider userBasketProvider) { - _basketAppService = basketAppService; - _currentUser = currentUser; - _guidGenerator = guidGenerator; - _logger = logger; + this.userBasketProvider = userBasketProvider; } public async Task InvokeAsync() { - BasketDto basketDto = new BasketDto(); - try - { - // Get anonymous user id from cookie - HttpContext.Request.Cookies.TryGetValue(CookieConstants.AnonymousUserCookieName, - out string anonymousUserId); - _logger.LogInformation($"========= Anonymous User Id from Cookie:{anonymousUserId} ========= "); - - // Generate new id for anonymous user - if (string.IsNullOrEmpty(anonymousUserId)) - { - anonymousUserId = _guidGenerator.Create().ToString(); - HttpContext.Response.Cookies.Append(CookieConstants.AnonymousUserCookieName, anonymousUserId); - _logger.LogInformation($"========= Generated new User Id:{anonymousUserId} ========= APPENDED TO COOKIE ====== "); - } - - if (!_currentUser.IsAuthenticated) - { - basketDto = await _basketAppService.GetByAnonymousUserIdAsync(Guid.Parse(anonymousUserId)); - _logger.LogInformation( - $"========= Get Basket for Anonymous UserId:{anonymousUserId} ========= APPENDED TO COOKIE ====== "); - _logger.LogInformation($"========= Basket Item Count:{basketDto.Items.Count} ========= "); - } - 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); + return View( + "~/Components/Toolbar/Cart/Default.cshtml", + await userBasketProvider.GetBasketAsync()); } } \ No newline at end of file