/// <summary> /// Add new product to Cart /// </summary> /// <param name="product"></param> /// <returns></returns> public Guid AddProductInCart(CART product) { try { CART productcart=null; product.ISACTIVE = true; if (product == null) throw new ArgumentNullException("Product", "Product can not be null"); // check if all required fields are present if ((product.PRODUCTID == null || product.PRODUCTID == Guid.Empty) || product.QUANTITY == null || (product.BUYERID == null || product.BUYERID == Guid.Empty)) throw new ArgumentException("Some mandatory parameters required to add a new product are missing", "Product"); try { using (APIShopKaro.Models.apsteamCFHEntities db = new APIShopKaro.Models.apsteamCFHEntities()) { productcart = (from p in db.CARTs where p.BUYERID == product.BUYERID && p.PRODUCTID == product.PRODUCTID select p).Single(); } return productcart.ID; } catch (Exception e) { } if (product.ID==null || product.ID == Guid.Empty) product.ID = Guid.NewGuid(); using (APIShopKaro.Models.apsteamCFHEntities db = new APIShopKaro.Models.apsteamCFHEntities()) { try { db.CARTs.Add(product); db.SaveChanges(); } catch (System.Data.DataException e) { throw new Exception(e.InnerException.InnerException.Message); } } return product.ID; } catch (Exception e) { throw; } }
public HttpResponseMessage AddProductToCart(CART product) { try { var cartService = new CartService(); var id = cartService.AddProductInCart(product); var response = Request.CreateResponse(HttpStatusCode.OK, id); return response; } catch (Exception e) { var error = Request.CreateResponse(HttpStatusCode.InternalServerError, e.Message); return error; } }