22using System . Threading . Tasks ;
33using Microsoft . AspNetCore . Identity ;
44using Microsoft . AspNetCore . Mvc ;
5- using Microsoft . eShopWeb . ApplicationCore . Interfaces ;
6- using Microsoft . eShopWeb . ApplicationCore . Specifications ;
75using Microsoft . eShopWeb . Infrastructure . Identity ;
86using Microsoft . eShopWeb . Web . Interfaces ;
97using Microsoft . eShopWeb . Web . ViewModels ;
10- using BasketEntity = Microsoft . eShopWeb . ApplicationCore . Entities . BasketAggregate . Basket ;
118
129namespace Microsoft . eShopWeb . Web . Pages . Shared . Components . BasketComponent ;
1310
1411public class Basket : ViewComponent
1512{
16- private readonly IBasketViewModelService _basketViewModelService ;
17- private readonly IReadRepository < BasketEntity > _basketRepository ;
13+ private readonly IBasketViewModelService _basketService ;
1814 private readonly SignInManager < ApplicationUser > _signInManager ;
1915
20- public Basket ( IBasketViewModelService basketViewModelService ,
21- IReadRepository < BasketEntity > basketRepository ,
22- SignInManager < ApplicationUser > signInManager )
16+ public Basket ( IBasketViewModelService basketService ,
17+ SignInManager < ApplicationUser > signInManager )
2318 {
24- _basketViewModelService = basketViewModelService ;
25- _basketRepository = basketRepository ;
19+ _basketService = basketService ;
2620 _signInManager = signInManager ;
2721 }
2822
@@ -39,26 +33,18 @@ private async Task<int> CountTotalBasketItems()
3933 {
4034 if ( _signInManager . IsSignedIn ( HttpContext . User ) )
4135 {
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 ;
36+ return await _basketService . CountTotalBasketItems ( User . Identity . Name ) ;
4737 }
4838
4939 string anonymousId = GetAnnonymousIdFromCookie ( ) ;
5040 if ( anonymousId == null )
5141 return 0 ;
5242
53- var anonymousSpec = new BasketWithItemsSpecification ( anonymousId ) ;
54- var anonymousBasket = await _basketRepository . GetBySpecAsync ( anonymousSpec ) ;
55- if ( anonymousBasket == null ) return 0 ;
56- return anonymousBasket . TotalItems ;
43+ return await _basketService . CountTotalBasketItems ( anonymousId ) ;
5744 }
5845
5946 private string GetAnnonymousIdFromCookie ( )
6047 {
61- // TODO: Add a prefix to anonymous cookie values so they cannot collide with user names
6248 if ( Request . Cookies . ContainsKey ( Constants . BASKET_COOKIENAME ) )
6349 {
6450 var id = Request . Cookies [ Constants . BASKET_COOKIENAME ] ;
0 commit comments