public PromoModels.PromofiedCart ApplyPromo(string id) { if (string.IsNullOrEmpty(id)) { throw new Exception("Not a valid cart id"); } CartModels.Cart cart = GetCart(id); // assuming that promo code ids are sent in the controller request var promoCodes = new List <string>(); var promoCollection = new List <PromoModels.Promo>(); foreach (var pc in promoCodes) { var promo = _promoRepo.Get(pc); if (promo != null) { promoCollection.Add(promo); } } // process promo code and get total price PromoModels.PromofiedCart promofiedCart = _promoEngine.Process(promoCollection, cart); return(promofiedCart); }
public string AddCart(CartModels.Cart cart) { if (cart == null) { throw new Exception("Not a valid cart"); } if (string.IsNullOrEmpty(cart.Id)) { cart.Id = new Guid().ToString(); } inMemoryCartList.Add(cart); return(cart.Id); }
public void processEnginePromo2() { // Arrange CartModels.Cart cart = new CartModels.Cart { Id = "1", Items = new List <CartModels.Item> { new CartModels.Item { //Id = "1", //Name = "A", Category = "Product A", Price = 100F }, new CartModels.Item { //Id = "1", //Name = "A", Category = "Product A", Price = 200F }, new CartModels.Item { //Id = "1", //Name = "A", Category = "Product A", Price = 130F }, new CartModels.Item { //Id = "1", //Name = "A", Category = "Product A", Price = 50F } } }; List <PromoModels.Promo> promos = new List <PromoModels.Promo> { new PromoModels.Promo { Id = "promo1", Buys = new List <PromoModels.Buy> { new PromoModels.Buy { Category = "Product A", Count = 2 } }, Description = "buy 2 and get 1 free", Gets = new List <PromoModels.Get> { new PromoModels.Get { Category = "Product A", //All = false, Count = 1, Off = new PromoModels.Off { //Fixed = new PromoModels.Fixed{ Price = 0F }, Discount = new PromoModels.Discount { Percentage = 100F } } } } } }; // Act var promoFied = promoEngine.Process(promos, cart); // Assert Assert.AreEqual(promoFied.TotalPrice, 480F); Assert.AreEqual(promoFied.TotalOffPrice, 350F); }
public void processEnginePromo1() { // Arrange CartModels.Cart cart = new CartModels.Cart { Id = "1", Items = new List <CartModels.Item> { new CartModels.Item { Id = "1", Name = "A", Category = "Product A", Price = 50F }, new CartModels.Item { Id = "1", Name = "A", Category = "Product A", Price = 50F }, new CartModels.Item { Id = "1", Name = "A", Category = "Product A", Price = 50F } } }; List <PromoModels.Promo> promos = new List <PromoModels.Promo> { new PromoModels.Promo { Id = "promo1", Buys = new List <PromoModels.Buy> { new PromoModels.Buy { Category = "Product A", Count = 2 } }, Description = "3 of A's for 130", // same as Buy 2 A's and get third for 30 Gets = new List <PromoModels.Get> { new PromoModels.Get { Category = "Product A", All = true, Count = 0, Off = new PromoModels.Off { Fixed = new PromoModels.Fixed { Price = 30F } } } } } }; // Act var promoFied = promoEngine.Process(promos, cart); // Assert Assert.AreEqual(promoFied.TotalPrice, 150F); Assert.AreEqual(promoFied.TotalOffPrice, 130F); }
// Implement the calculation process for promo engine PromoModels.PromofiedCart IPromoEngine.Process(List <PromoModels.Promo> promos, CartModels.Cart cart) { var groupedItems = new Dictionary <string, List <PromoModels.MarkedItem> >(); foreach (var item in cart.Items) { if (!groupedItems.ContainsKey(item.Category)) { groupedItems.Add(item.Category, new List <PromoModels.MarkedItem> { new PromoModels.MarkedItem { Item = item, MarkedBuys = new Dictionary <string, bool>(), MarkedGets = new Dictionary <string, float>() } }); } else { groupedItems[item.Category].Add(new PromoModels.MarkedItem { Item = item, MarkedBuys = new Dictionary <string, bool>(), MarkedGets = new Dictionary <string, float>() }); } } foreach (var promo in promos) { groupedItems = ApplyPromo(promo, groupedItems); } var items = new List <PromoModels.MarkedItem>(); foreach (var item in groupedItems) { items.AddRange(item.Value); } var prices = ComputePrices(items); var totalPrice = prices[0]; var totalOffPrice = prices[1]; PromoModels.PromofiedCart promofiedCart = new PromoModels.PromofiedCart { Items = items, TotalPrice = totalPrice, TotalOffPrice = totalOffPrice }; return(promofiedCart); }
public string Post([FromBody] CartModels.Cart cart) { return(_cartRepo.AddCart(cart)); }