Exemplo n.º 1
0
        public Customer Update(Customer customerUpdate)
        {
            _ctx.Attach(customerUpdate).State = EntityState.Modified;
            _ctx.Entry(customerUpdate).Collection(c => c.Carts).IsModified = true;
            var carts = _ctx.Cart.Where(o => o.Customer.Id == customerUpdate.Id &&
                                        !customerUpdate.Carts.Exists(co => co.Id == o.Id));

            foreach (var cart in carts)
            {
                cart.Customer = null;
                _ctx.Entry(cart).Reference(o => o.Customer)
                .IsModified = true;
            }
            _ctx.SaveChanges();
            return(customerUpdate);
        }
Exemplo n.º 2
0
        public Cart Update(Core.Entity.Cart cartUpdate)
        {
            int noStockProduct = IsStockAvailable(cartUpdate.CartLines);

            if (noStockProduct > 0)
            {
                throw new Exception($"{noStockProduct} idli ürün stokta yok!");
            }

            var newCartLines = new List <CartLine>(cartUpdate.CartLines);

            _ctx.Attach(cartUpdate).State = EntityState.Modified;
            _ctx.CartLines.RemoveRange(
                _ctx.CartLines.Where(ol => ol.CartId == cartUpdate.Id)
                );
            foreach (var ol in newCartLines)
            {
                _ctx.Entry(ol).State = EntityState.Added;
            }
            _ctx.Entry(cartUpdate).Reference(o => o.Customer).IsModified = true;
            _ctx.SaveChanges();
            return(cartUpdate);
        }