Ejemplo n.º 1
0
        public static void AddProductData(BasketContext context)
        {
            List <ProductModel> products = new List <ProductModel>()
            {
                new ProductModel()
                {
                    SKU = "A111", Description = "Surface Phone", Price = 100, Quantity = 100
                },
                new ProductModel()
                {
                    SKU = "A112", Description = "Surface Pro 1", Price = 120, Quantity = 200
                },
                new ProductModel()
                {
                    SKU = "A113", Description = "Surface Pro 2", Price = 140, Quantity = 200
                },
                new ProductModel()
                {
                    SKU = "P111", Description = "Windows10 Pro", Price = 123, Quantity = 600
                },
            };

            context.Products.AddRange(products);
            context.SaveChanges();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Adds the or update basket item.
        /// </summary>
        /// <param name="basketItem">The basket item.</param>
        /// <returns>BasketError.</returns>
        public BasketError AddOrUpdateBasketItem(BasketRequest basketItem)
        {
            BasketError rtn = BasketError.ProductNotAvailable;

            if (po.IsAvailable(basketItem.SKU, basketItem.Quantity))
            {
                Tuple <bool, decimal> update = po.UpdateQuantity(basketItem.SKU, basketItem.Quantity);
                List <BasketRequest>  list   = _context.Baskets.Where(b => b.ClientId.Trim().ToLower() == basketItem.ClientId.Trim().ToLower() &&
                                                                      b.SKU.Trim().ToLower() == basketItem.SKU.Trim().ToLower()).ToList();
                if (list.Count() > 0)
                {
                    if (list.Count == 1)
                    {
                        list.SingleOrDefault().Quantity  += basketItem.Quantity;
                        list.SingleOrDefault().TotalPrice = list.SingleOrDefault().Quantity *update.Item2;
                        _context.SaveChanges();
                        rtn = BasketError.NoError;
                    }
                    else
                    {
                        rtn = BasketError.DuplicatedSKU;
                    }
                }
                else
                {
                    if (update.Item1)
                    {
                        basketItem.TotalPrice += basketItem.Quantity * update.Item2;
                        _context.Baskets.Add(basketItem);
                        _context.SaveChanges();

                        rtn = BasketError.NoError;
                    }
                }
            }

            return(rtn);
        }
        /// <summary>
        /// Updates the quantity.
        /// </summary>
        /// <param name="sku">The sku.</param>
        /// <param name="quantity">The quantity.</param>
        /// <returns>Tuple&lt;System.Boolean, System.Decimal&gt;.</returns>
        public Tuple <bool, decimal> UpdateQuantity(string sku, int quantity)
        {
            bool    rtn = false;
            decimal tot = -1;

            if (IsAvailable(sku, quantity))
            {
                ProductModel prod = _context.Products.Where(p => p.SKU.Trim() == sku.Trim()).FirstOrDefault();
                if (prod != null)
                {
                    prod.Quantity -= quantity;
                    _context.SaveChanges();

                    tot = quantity * prod.Price;
                    rtn = true;
                }
            }

            return(new Tuple <bool, decimal>(rtn, tot));
        }