Skip to content

Commit 833dc3b

Browse files
committed
Refactor to eliminate unneeded BasketQueryService
1 parent 6f5a6c0 commit 833dc3b

File tree

11 files changed

+69
-65
lines changed

11 files changed

+69
-65
lines changed

src/ApplicationCore/Entities/BasketAggregate/Basket.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ public class Basket : BaseEntity, IAggregateRoot
1010
private readonly List<BasketItem> _items = new List<BasketItem>();
1111
public IReadOnlyCollection<BasketItem> Items => _items.AsReadOnly();
1212

13+
public int TotalItems => _items.Sum(i => i.Quantity);
14+
15+
1316
public Basket(string buyerId)
1417
{
1518
BuyerId = buyerId;

src/ApplicationCore/Interfaces/IBasketQueryService.cs

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/Infrastructure/Data/BasketQueryService.cs

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/Web/Configuration/ConfigureCoreServices.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public static IServiceCollection AddCoreServices(this IServiceCollection service
1818

1919
services.AddScoped<IBasketService, BasketService>();
2020
services.AddScoped<IOrderService, OrderService>();
21-
services.AddScoped<IBasketQueryService, BasketQueryService>();
2221
services.AddSingleton<IUriComposer>(new UriComposer(configuration.Get<CatalogSettings>()));
2322
services.AddScoped(typeof(IAppLogger<>), typeof(LoggerAdapter<>));
2423
services.AddTransient<IEmailSender, EmailSender>();

src/Web/Interfaces/IBasketViewModelService.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,5 @@ namespace Microsoft.eShopWeb.Web.Interfaces;
77
public interface IBasketViewModelService
88
{
99
Task<BasketViewModel> GetOrCreateBasketForUser(string userName);
10-
11-
Task<int> CountTotalBasketItems(string username);
12-
1310
Task<BasketViewModel> Map(Basket basket);
1411
}

src/Web/Pages/Basket/Checkout.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
</h3>
8181

8282
<section class="esh-basket-item">
83-
<a asp-page="/Index" class="btn esh-basket-checkout text-white">[ Continue Shopping..]</a>
83+
<a asp-page="/Index" class="btn esh-basket-checkout text-white">[ Continue Shopping ]</a>
8484
</section>
8585
}
8686
</div>

src/Web/Pages/Basket/Index.cshtml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
</div>
6464
<div class="row">
6565
<section class="esh-basket-item col-xs-1">
66-
<a asp-page="/Index" class="btn esh-basket-checkout text-white">[ Continue Shopping..]</a>
66+
<a asp-page="/Index" class="btn esh-basket-checkout text-white">[ Continue Shopping ]</a>
6767
</section>
6868
<section class="esh-basket-item col-xs-push-7 col-xs-4">
6969

@@ -84,7 +84,7 @@
8484
</h3>
8585

8686
<section class="esh-basket-item">
87-
<a asp-page="/Index" class="btn esh-basket-checkout text-white">[ Continue Shopping..]</a>
87+
<a asp-page="/Index" class="btn esh-basket-checkout text-white">[ Continue Shopping ]</a>
8888
</section>
8989
}
9090
</div>

src/Web/Pages/Shared/Components/BasketComponent/Basket.cs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,27 @@
22
using System.Threading.Tasks;
33
using Microsoft.AspNetCore.Identity;
44
using Microsoft.AspNetCore.Mvc;
5+
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
6+
using Microsoft.eShopWeb.ApplicationCore.Specifications;
57
using Microsoft.eShopWeb.Infrastructure.Identity;
68
using Microsoft.eShopWeb.Web.Interfaces;
79
using Microsoft.eShopWeb.Web.ViewModels;
10+
using BasketEntity = Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate.Basket;
811

912
namespace Microsoft.eShopWeb.Web.Pages.Shared.Components.BasketComponent;
1013

1114
public class Basket : ViewComponent
1215
{
13-
private readonly IBasketViewModelService _basketService;
16+
private readonly IBasketViewModelService _basketViewModelService;
17+
private readonly IReadRepository<BasketEntity> _basketRepository;
1418
private readonly SignInManager<ApplicationUser> _signInManager;
1519

16-
public Basket(IBasketViewModelService basketService,
17-
SignInManager<ApplicationUser> signInManager)
20+
public Basket(IBasketViewModelService basketViewModelService,
21+
IReadRepository<BasketEntity> basketRepository,
22+
SignInManager<ApplicationUser> signInManager)
1823
{
19-
_basketService = basketService;
24+
_basketViewModelService = basketViewModelService;
25+
_basketRepository = basketRepository;
2026
_signInManager = signInManager;
2127
}
2228

@@ -33,18 +39,26 @@ private async Task<int> CountTotalBasketItems()
3339
{
3440
if (_signInManager.IsSignedIn(HttpContext.User))
3541
{
36-
return await _basketService.CountTotalBasketItems(User.Identity.Name);
42+
var userNameSpec = new BasketWithItemsSpecification(User.Identity.Name);
43+
44+
var userBasket = await _basketRepository.GetBySpecAsync(userNameSpec);
45+
if (userBasket == null) return 0;
46+
return userBasket.TotalItems;
3747
}
3848

3949
string anonymousId = GetAnnonymousIdFromCookie();
4050
if (anonymousId == null)
4151
return 0;
4252

43-
return await _basketService.CountTotalBasketItems(anonymousId);
53+
var anonymousSpec = new BasketWithItemsSpecification(anonymousId);
54+
var anonymousBasket = await _basketRepository.GetBySpecAsync(anonymousSpec);
55+
if (anonymousBasket == null) return 0;
56+
return anonymousBasket.TotalItems;
4457
}
4558

4659
private string GetAnnonymousIdFromCookie()
4760
{
61+
// TODO: Add a prefix to anonymous cookie values so they cannot collide with user names
4862
if (Request.Cookies.ContainsKey(Constants.BASKET_COOKIENAME))
4963
{
5064
var id = Request.Cookies[Constants.BASKET_COOKIENAME];

src/Web/Services/BasketViewModelService.cs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,14 @@ public class BasketViewModelService : IBasketViewModelService
1414
{
1515
private readonly IRepository<Basket> _basketRepository;
1616
private readonly IUriComposer _uriComposer;
17-
private readonly IBasketQueryService _basketQueryService;
18-
private readonly IRepository<CatalogItem> _itemRepository;
17+
private readonly IReadRepository<CatalogItem> _itemRepository;
1918

2019
public BasketViewModelService(IRepository<Basket> basketRepository,
21-
IRepository<CatalogItem> itemRepository,
22-
IUriComposer uriComposer,
23-
IBasketQueryService basketQueryService)
20+
IReadRepository<CatalogItem> itemRepository,
21+
IUriComposer uriComposer)
2422
{
2523
_basketRepository = basketRepository;
2624
_uriComposer = uriComposer;
27-
_basketQueryService = basketQueryService;
2825
_itemRepository = itemRepository;
2926
}
3027

@@ -86,11 +83,4 @@ public async Task<BasketViewModel> Map(Basket basket)
8683
Items = await GetBasketItems(basket.Items)
8784
};
8885
}
89-
90-
public async Task<int> CountTotalBasketItems(string username)
91-
{
92-
var counter = await _basketQueryService.CountTotalBasketItems(username);
93-
94-
return counter;
95-
}
9686
}

tests/UnitTests/ApplicationCore/Entities/BasketTests/BasketAddItem.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using System;
2-
using System.Linq;
3-
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
4-
using Xunit;
5-
2+
using System.Linq;
3+
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
4+
using Xunit;
5+
66
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Entities.BasketTests;
77

88
public class BasketAddItem
@@ -72,4 +72,4 @@ public void CantModifyQuantityToNegativeNumber()
7272

7373
Assert.Throws<ArgumentOutOfRangeException>(() => basket.AddItem(_testCatalogItemId, _testUnitPrice, -2));
7474
}
75-
}
75+
}

0 commit comments

Comments
 (0)