Browse Source

Extract common code from ViewComponents

pull/41/head
enisn 5 years ago
parent
commit
ea660811b8
  1. 76
      apps/public-web/src/EShopOnAbp.PublicWeb/Basket/UserBasketProvider.cs
  2. 54
      apps/public-web/src/EShopOnAbp.PublicWeb/Components/Basket/BasketWidgetViewComponent.cs
  3. 56
      apps/public-web/src/EShopOnAbp.PublicWeb/Components/Toolbar/Cart/CartWidgetViewComponent.cs

76
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<UserBasketProvider> logger;
protected readonly IGuidGenerator guidGenerator;
protected readonly IBasketAppService basketAppService;
protected readonly ICurrentUser currentUser;
public UserBasketProvider(
IHttpContextAccessor httpContextAccessor,
ILogger<UserBasketProvider> 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<BasketDto> 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;
}
}
}
}

54
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<BasketWidgetViewComponent> _logger;
private readonly UserBasketProvider userBasketProvider;
public BasketWidgetViewComponent(
IBasketAppService basketAppService,
ICurrentUser currentUser,
IGuidGenerator guidGenerator, ILogger<BasketWidgetViewComponent> logger)
public BasketWidgetViewComponent(UserBasketProvider userBasketProvider)
{
_basketAppService = basketAppService;
_currentUser = currentUser;
_guidGenerator = guidGenerator;
_logger = logger;
this.userBasketProvider = userBasketProvider;
}
public async Task<IViewComponentResult> 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());
}
}

56
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<CartWidgetViewComponent> _logger;
private readonly UserBasketProvider userBasketProvider;
public CartWidgetViewComponent(
IBasketAppService basketAppService,
ICurrentUser currentUser,
IGuidGenerator guidGenerator,
ILogger<CartWidgetViewComponent> logger)
public CartWidgetViewComponent(UserBasketProvider userBasketProvider)
{
_basketAppService = basketAppService;
_currentUser = currentUser;
_guidGenerator = guidGenerator;
_logger = logger;
this.userBasketProvider = userBasketProvider;
}
public async Task<IViewComponentResult> 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());
}
}
Loading…
Cancel
Save