public ActionResult AddProductToMyCart() { int quantiteChoisie = Int16.Parse(Request["quantite"].ToString()); int productId = Int16.Parse(Request["productId"].ToString()); try { Cart cartForTest = serviceCart.GetCartByUserId(user.Id); Product product = serviceProd.GetById(productId); if (cartForTest == null) { Cart newCart = db.Carts.Create(); newCart.status = true; newCart.userId = user.Id; newCart.dateAchat = DateTime.UtcNow; serviceCart.Add(newCart); serviceCart.Commit(); CartLine cartLine = db.CartLines.Create(); cartLine.dateAjout = DateTime.UtcNow; cartLine.CartId = newCart.id; cartLine.productId = productId; cartLine.quantiteChoisie = quantiteChoisie; cartLine.prixTotal = product.prix * quantiteChoisie; cartLine.prixDuProduit = product.prix; serviceCartLine.Add(cartLine); serviceCartLine.Commit(); //newCart.prixTotal += cartLine.prixTotal; //serviceCart.Commit(); } else { CartLine cartLine = db.CartLines.Create(); cartLine.dateAjout = DateTime.UtcNow; cartLine.CartId = cartForTest.id; cartLine.productId = productId; cartLine.quantiteChoisie = quantiteChoisie; cartLine.prixTotal = product.prix * quantiteChoisie; cartLine.prixDuProduit = product.prix; serviceCartLine.Add(cartLine); serviceCartLine.Commit(); //cartForTest.prixTotal += cartLine.prixTotal ; //serviceCart.Commit(); } } catch (IOException e) { throw e; } return(RedirectToAction("Index")); }
public IHttpActionResult AddProductToMyCart(int idp, ProductModel p) { Cart cartForTest = serviceCart.GetCartByUserId(1); Product product = serviceProduct.GetById(idp); if (cartForTest == null) { Cart newCart = db.Carts.Create(); newCart.status = true; newCart.userId = 1; newCart.dateAchat = DateTime.UtcNow; serviceCart.Add(newCart); serviceCart.Commit(); CartLine cartLine = db.CartLines.Create(); cartLine.dateAjout = DateTime.UtcNow; cartLine.CartId = newCart.id; cartLine.productId = idp; cartLine.quantiteChoisie = p.quantite; cartLine.prixTotal = product.prix * p.quantite; cartLine.prixDuProduit = product.prix; serviceCartLine.Add(cartLine); serviceCartLine.Commit(); } else { CartLine cartLine = db.CartLines.Create(); cartLine.dateAjout = DateTime.UtcNow; cartLine.CartId = cartForTest.id; cartLine.productId = idp; cartLine.quantiteChoisie = p.quantite; cartLine.prixTotal = product.prix * p.quantite; cartLine.prixDuProduit = product.prix; serviceCartLine.Add(cartLine); serviceCartLine.Commit(); //cartForTest.prixTotal += cartLine.prixTotal ; //serviceCart.Commit(); } return(Ok()); }