Пример #1
0
 public HttpResponseMessage AddNewProduct(PRODUCT product)
 {
     try
     {
         var productsService = new ProductsService();
         var id = productsService.AddNewProduct(product);
         var response = Request.CreateResponse(HttpStatusCode.OK, id);
         return response;
     }
     catch (Exception e)
     {
         var error = Request.CreateResponse(HttpStatusCode.InternalServerError, e.Message);
         return error;
     }
 }
Пример #2
0
        // TODO : how to add image url
        /// <summary>
        /// Add new product
        /// </summary>
        /// <param name="product"></param>
        /// <returns></returns>
        public Guid AddNewProduct(PRODUCT product)
        {
            try
            {
                if (product == null)
                    throw new ArgumentNullException("Product", "Product can not be null");

                // check if all required fields are present
                if ((!product.CATEGORYID.HasValue || product.CATEGORYID.Value == Guid.Empty)|| product.NAME == null || 
                    !product.PRICE.HasValue || (!product.SELLERID.HasValue || product.SELLERID.Value == Guid.Empty))
                    throw new ArgumentException("Some mandatory parameters required to add a new product are missing", "Product");
                                
                if (!product.ID.HasValue || product.ID.Value == Guid.Empty)
                    product.ID = Guid.NewGuid();

                product.AVGRATING = 0;
                product.QUANTITYSOLD = 0;
               
                using (APIShopKaro.Models.apsteamCFHEntities db = new APIShopKaro.Models.apsteamCFHEntities())
                {
                    try
                    {
                        db.PRODUCTS.Add(product);
                        db.SaveChanges();
                    }
                    catch (System.Data.DataException e)
                    {
                        throw new Exception(e.InnerException.InnerException.Message);
                    }
                }

                return product.ID.Value;
            }
            catch (Exception e)
            {
                throw;
            }
        }