public void AddProductToCart(int userId, CartItem cartItem) {// ovde moras provjeriti da li se dodaje postojeci item, ako dodaje povecaj mu samo quantity using (EF.APShopContext context = new EF.APShopContext()) { UnitOfWork uow = new UnitOfWork(context); List <CartItem> userCartItems = GetCartItems(userId).ToList(); foreach (CartItem item in userCartItems) { if (item.Code == cartItem.Code) { EF.CartProduct dboCartProduct1 = new EF.CartProduct(); dboCartProduct1 = _mapper.Map <EF.CartProduct>(item); dboCartProduct1.Quantity += cartItem.Quantity; context.CartProduct.Update(dboCartProduct1); context.SaveChanges(); return; } } // EF.Cart cart = context.Cart.Where(c => c.UserId == userId).SingleOrDefault(); EF.CartProduct dboCartProduct = new EF.CartProduct(); //dboCartProduct.CartId = cart.Id; dboCartProduct.Code = cartItem.Code; dboCartProduct.Quantity = cartItem.Quantity; uow.Carts.GetByUserId(userId).CartProduct.Add(dboCartProduct); //uow.Users.addCartProduct(dboCartProduct); uow.Commit(); } }
public int updateCartItem(int userId, string cartItemCode, int quantity) { using (EF.APShopContext context = new EF.APShopContext()) { UnitOfWork uow = new UnitOfWork(context); EF.CartProduct cartItem = context.CartProduct.Where(p => p.Cart.UserId == userId && p.Code == cartItemCode).SingleOrDefault(); cartItem.Quantity = quantity; uow.Users.updateCartItem(cartItem); uow.Commit(); return(cartItem.Quantity); } }
public List <CartItem> deleteProductFromCart(int userId, string code, DateTime dateTime) { using (EF.APShopContext context = new EF.APShopContext()) { UnitOfWork uow = new UnitOfWork(context); EF.CartProduct productToDelete = context.CartProduct.Include(c => c.Cart). Where(p => p.Cart.UserId == userId && p.Code == code).SingleOrDefault(); // EF.CartProduct productToDelete = uow.Users.GetById(userId).Cart.SingleOrDefault().CartProduct.Where(p => p.Code == code && p.Cart.UserId ==userId).SingleOrDefault(); if (productToDelete == null) { throw new ArgumentNullException("Couldn't find requested resource"); } productToDelete.Cart.DateLastUpdated = dateTime; uow.Users.deleteCartProduct(productToDelete); uow.Commit(); return(GetCartItems(userId).ToList()); } }