/// <summary>
        /// Update the stock of a product by its id
        /// </summary>
        public void UpdateProductStocks(int id, int quantityToRemove)
        {
            Product product = _context.Product.First(p => p.Id == id);

            product.Quantity = product.Quantity - quantityToRemove;

            if (product.Quantity == 0)
            {
                _context.Product.Remove(product);
            }
            else
            {
                _context.Product.Update(product);
                _context.SaveChanges();
            }
        }
Пример #2
0
 public void Save(Order order)
 {
     if (order != null && OrderValid(order))
     {
         _context.Order.Add(order);
         _context.SaveChanges();
     }
 }
Пример #3
0
 public void Save(Order order)
 {
     //Bug fix: Add a condition to check for null as we dont want to save null order
     if (order != null)
     {
         _context.Order.Add(order);
         _context.SaveChanges();
     }
 }
        private void SeedTestDb(DbContextOptions <P3Referential> options)
        {
            using (var context = new P3Referential(options))
            {
                //seed products
                foreach (var p in _testProductsList)
                {
                    context.Product.Add(p);
                }
                context.SaveChanges();

                //seed orders
                foreach (var o in _testOrdersList)
                {
                    context.Order.Add(o);
                }
                context.SaveChanges();
            }
        }
        /// <summary>
        /// Update the stock of a product by its id
        /// </summary>
        public void UpdateProductStocks(int id, int quantityToRemove)
        {
            Product product = _context.Product.First(p => p.Id == id);

            product.Quantity = product.Quantity - quantityToRemove;

            /*Bug Fixed: This condition should be <= 0 to cater for the scenario where
             * Product stock is already 0 and this method gets called with quantityToRemove as greater than zero.
             */
            if (product.Quantity <= 0)
            {
                _context.Product.Remove(product);
            }
            else
            {
                _context.Product.Update(product);
                _context.SaveChanges();
            }
        }
Пример #6
0
 private void SeedTestDb(DbContextOptions <P3Referential> options)
 {
     using (var context = new P3Referential(options))
     {
         foreach (var p in _testProductsList)
         {
             context.Product.Add(p);
         }
         context.SaveChanges();
     }
 }
Пример #7
0
        /// <summary>
        /// Update the stock of a product by its id
        /// </summary>
        public void UpdateProductStocks(int id, int quantityToRemove)
        {
            Product product = _context.Product.FirstOrDefault(p => p.Id == id);

            if (product != null)
            {
                if (quantityToRemove > product.Quantity)
                {
                    throw new ArgumentException("quantityToRemove bigger than product quantity");
                }

                product.Quantity = product.Quantity - quantityToRemove;

                if (product.Quantity == 0)
                {
                    _context.Product.Remove(product);
                }
                else
                {
                    _context.Product.Update(product);
                    _context.SaveChanges();
                }
            }
        }
Пример #8
0
 public void Save(Order order)
 {
     _context.Order.Add(order);
     _context.SaveChanges();
 }