public IHttpActionResult DeleteFromCart(short cartId, string customerId) { designEntity = new online_tshirt_designingEntities(); var matches = from p in designEntity.product_cart where p.ProductCartId == cartId && p.CustId == customerId select p; //Execute the Query & return the Obj product_cart pCart = matches.Single(); //Delete the Record from the Database designEntity.product_cart.Remove(pCart); designEntity.SaveChanges(); return(Ok()); }
[HttpPut] //(productId), (customerId), (quantity) public IHttpActionResult IncreaseQuantity([FromBody] CartModel theCart) { designEntity = new online_tshirt_designingEntities(); CartModel cartModel = new CartModel(); ProductCartModel pc = new ProductCartModel(); int updatedRecord = 0; var matchesProduct = designEntity.products //from statement .Join(designEntity.product_cart, //Source table of inner join prod => prod.ProductId, //Select the primarykey (the first part of the "on" clause in an sql "join" statement) cart => cart.ProductId, //Select the foreign key i.e on clause (prod, cart) => new { Prod = prod, Cart = cart } //Selection ) .Where(cart => (cart.Cart.ProductCartId == theCart.ProductCartId) && (cart.Cart.CustId == theCart.CustId))//Where statement .FirstOrDefault(); //Sets the quantity cartModel.ProductQuantity = theCart.ProductQuantity; //Sets the price for selected individual product to be calculated cartModel.ProductQuantityPrice = (int)matchesProduct.Prod.ProductPrice; //Change the entity object product_cart newProductCartEntry = matchesProduct.Cart; newProductCartEntry.ProductQuantity = cartModel.ProductQuantity; newProductCartEntry.ProductQuantityPrice = cartModel.ProductQuantityPrice; try { //Commits the changes and inserts the record //In Entity database updatedRecord = designEntity.SaveChanges(); } catch (Exception error) { System.Diagnostics.Debug.WriteLine("Error in Linq", error); } if (updatedRecord > 0) { //Return the product added in the cart var cart = from entProd in designEntity.products join entCart in designEntity.product_cart on entProd.ProductId equals entCart.ProductId where entCart.CustId == theCart.CustId select new { //Send entire product Obj entProd.ProductId, entProd.ProductCode, entProd.ProductCat, entProd.ProductName, entProd.ProductStyle, entProd.ProductColor, entProd.ProductImg, entProd.ProductDisc, entProd.ProductPrice, entProd.ProductSizeQuantM, entProd.ProductSizeQuantXL, entProd.ProductSizeQuantXXL, entCart.ProductCartId, entCart.ProductQuantity, entCart.ProductQuantityPrice, entCart.ProductSize }; //Calculates the price foreach (var item in cart) { //// Sets the quantity //cartModel.ProductQuantity = (short)item.ProductQuantity; ////Sets the quantity price //cartModel.ProductQuantityPrice = (int)item.ProductQuantityPrice; //Calcualtes the amount cartModel.TotalProductPrice += (int)item.ProductQuantityPrice; } return(Ok(new Tuple <IEnumerable <object>, double>(cart, cartModel.TotalProductPrice))); } return(NotFound()); }
public IHttpActionResult AddToCart([FromBody] CartModel theCart) { designEntity = new online_tshirt_designingEntities(); int insertedRecord = 0; //1st checks the Product is already in cart or not var matchesCart = designEntity.products //from statement .Join(designEntity.product_cart, //Source table of inner join prod => prod.ProductId, //Select the primarykey (the first part of the "on" clause in an sql "join" statement) cart => cart.ProductId, //Select the foreign key i.e on clause (prod, cart) => new { Prod = prod, Cart = cart } //Selection ) .Where(cart => (cart.Cart.ProductId == theCart.ProductId) && (cart.Cart.CustId == theCart.CustId) && (cart.Cart.ProductSize == theCart.ProductSize))//Where statement .FirstOrDefault(); //designEntity.product_cart.FirstOrDefault((c) => c.ProductId == theCart.ProductId && c.CustId == theCart.CustId) ; int cartLength = designEntity.product_cart.Select((c) => c).Where(c => c.CustId == theCart.CustId).Count(); //It states the product is in the cart if (matchesCart != null) { return(NotFound()); } //Inserts in the Entity database, if no product is added product_cart newCartEntry = new product_cart { ProductQuantity = theCart.ProductQuantity, ProductQuantityPrice = theCart.ProductQuantityPrice, ProductSize = theCart.ProductSize, ProductId = theCart.ProductId, CustId = theCart.CustId }; try { //Commits the changes and inserts the record //In Entity database designEntity.product_cart.Add(newCartEntry); insertedRecord = designEntity.SaveChanges(); } catch (Exception error) { System.Diagnostics.Debug.WriteLine("Error in Linq", error); } if (insertedRecord > 0) { //Return the product added in the cart var cart = from entProd in designEntity.products join entCart in designEntity.product_cart on entProd.ProductId equals entCart.ProductId where entCart.CustId == theCart.CustId select new { //Send entire product Obj entProd.ProductId, entProd.ProductCode, entProd.ProductCat, entProd.ProductName, entProd.ProductStyle, entProd.ProductColor, entProd.ProductImg, entProd.ProductDisc, entProd.ProductPrice, entProd.ProductSizeQuantM, entProd.ProductSizeQuantXL, entProd.ProductSizeQuantXXL, entCart.ProductCartId, entCart.ProductQuantity, entCart.ProductQuantityPrice, entCart.ProductSize }; return(Ok(cart)); } return(NotFound()); }