public Carts addItem(Carts cart, int productId) { decimal productPrice = _productDAL.Get(x => x.Id == productId).Price; int lastCartItemId = 0; int isExist = _cartDAL.GetList().Where(x => x.Id == cart.Id).Count(); Carts cartItem = new Carts(); if (isExist == 0) { cartItem.Amount = cart.Amount; cartItem.TotalPrice = productPrice * cart.Amount; _cartDAL.Add(cartItem); lastCartItemId = _cartDAL.GetList().OrderByDescending(x => x.Id).ToList().FirstOrDefault().Id; } else { var thisCartItem = _cartDAL.GetList().OrderByDescending(x => x.Id).ToList().FirstOrDefault(); cartItem.Id = cart.Id; cartItem.Amount = thisCartItem.Amount + cart.Amount; cartItem.TotalPrice = (productPrice * (thisCartItem.Amount + cart.Amount)); _cartDAL.Update(cartItem); lastCartItemId = cart.Id; } if (_cartProductDAL.GetList(x => x.CartId == lastCartItemId && x.ProductId == productId).Count() < 1) { CartProducts cartProduct = new CartProducts() { CartId = lastCartItemId, ProductId = productId }; _cartProductDAL.Add(cartProduct); } else { int lastCartId = _cartDAL.GetList().Where(x => x.Id == lastCartItemId).FirstOrDefault().Id; CartProducts cartProduct = new CartProducts() { Id = lastCartItemId, CartId = lastCartItemId, ProductId = productId }; _cartProductDAL.Update(cartProduct); } return(cartItem); }
public void createCartProducts(CartProducts cartProducts) { _cartProductDAL.Add(cartProducts); }