-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCartService.cs
69 lines (57 loc) · 2.59 KB
/
CartService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using Generic.Ecom.Models;
using CMS.Ecommerce;
using CMS.Helpers;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Generic.Ecom.RepositoryLibrary;
using System.Linq;
namespace Generic.Ecom.ServiceLibrary
{
public class CartService : ICartService
{
public IShoppingService ShoppingService { get; }
public ICartRepository CartRepository { get; }
public ISKUInfoProvider SKUInfoProvider { get; }
public IShoppingCartItemInfoProvider ShoppingCartItemInfoProvider { get; }
public IProgressiveCache ProgressiveCache { get; }
public CartService(IShoppingService shoppingService, ICartRepository cartRepository, ISKUInfoProvider sKUInfoProvider, IShoppingCartItemInfoProvider shoppingCartItemInfoProvider, IProgressiveCache progressiveCache)
{
ShoppingService = shoppingService;
CartRepository = cartRepository;
SKUInfoProvider = sKUInfoProvider;
ShoppingCartItemInfoProvider = shoppingCartItemInfoProvider;
ProgressiveCache = progressiveCache;
}
public async Task<ShoppingCartItemInfo> AddToCart(AddToCartViewModel cartViewModel)
{
var sku = await ProgressiveCache.LoadAsync(async (cs) =>
{
var item = await SKUInfoProvider.GetAsync(cartViewModel.SKUGUID);
return item;
}, new CacheSettings(30, "AddToCart", cartViewModel.SKUGUID));
var shoppingCartItem = new ShoppingCartItemParameters(sku.SKUID, cartViewModel.Quantity);
if (cartViewModel.CustomFields != null)
{
foreach(KeyValuePair<string, object> item in cartViewModel.CustomFields)
{
shoppingCartItem.CustomParameters.Add(item.Key.Replace("customField", "").ToLowerInvariant(), item.Value);
}
}
return ShoppingService.AddItemToCart(shoppingCartItem);
}
public async Task<string> UpdateItemQuantity(int itemID, int itemQuantity)
{
ShoppingService.UpdateItemQuantity(itemID, itemQuantity);
var cart = CartRepository.GetCart();
var item = cart.CartItems.Where(x=>x.CartItemID == itemID).FirstOrDefault();
var currency = cart.Currency.CurrencyFormatString;
return string.Format(currency, item?.TotalPrice);
}
public void RemoveItem(int itemID)
{
ShoppingService.RemoveItemFromCart(itemID);
}
}
}