コード例 #1
0
        public async Task TestAdd()
        {
            //ProuctManager _pManager1 = new ProuctManager();
            Product p = new Product
            {
                ImageAddress = "C:\\Img1.jpg",
                Price        = 10.00M,
                ProductName  = "P4"
            };
            var product = await _pManager.AddProductAsync(p);

            products.Add(product);
            Assert.IsNotNull(product.ProductId);
        }
コード例 #2
0
        public async Task <IHttpActionResult> PostProduct()
        {
            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }


            Product product = new Product
            {
                Price       = Convert.ToDecimal(HttpContext.Current.Request.Form["Price"]),
                ProductName = (string)HttpContext.Current.Request.Form["ProductName"]
            };

            if (string.IsNullOrEmpty(product.ProductName))
            {
                return(BadRequest("Product Name cannot be blank."));
            }
            if (product.Price < 0)
            {
                return(BadRequest("Product price cannot be less than 0."));
            }
            // db.Products.Add(product);
            try
            {
                var    image        = HttpContext.Current.Request.Files.Count > 0 ? HttpContext.Current.Request.Files[0] : null;
                string filePath     = null;
                string thumbnilPath = null;

                product.ImageAddress = filePath;
                if (image != null && image.ContentLength > 0)
                {
                    var imageValidityResult = CheckImageValidity(image);
                    if (imageValidityResult.Item1)
                    {
                        filePath     = imageDirectory + image.FileName;
                        thumbnilPath = thumbnilDirectory + image.FileName;

                        product.ImageAddress = filePath;
                    }
                    else
                    {
                        return(BadRequest(imageValidityResult.Item2));
                    }
                }

                product = await _productManager.AddProductAsync(product);

                if (filePath != null)
                {
                    image.SaveAs(filePath);
                    GetThumbnil(image).Save(thumbnilPath);
                }


                // await db.SaveChangesAsync();
            }
            catch (SqlException e)
            {
                return(BadRequest(e.GetBaseException().Message));
            }
            catch (CustomException.NotFoundException <Product> e)
            {
                return(NotFound());
            }
            catch (Exception e)
            {
                throw new CustomException.GeneralErrorMessage(e.GetBaseException().Message);
            }

            return(CreatedAtRoute("DefaultApi", new { id = product.ProductId }, product));
        }