public ActionResult AdminPost(ProductCM model)
        {
            var product = model.Adapt <Product>();

            product.Status = (int)ProductStatus.available;
            foreach (var smell in model.SmellIds)
            {
                product.Smells.Add(new ProductSmell {
                    ProductId = product.Id,
                    SmellId   = smell
                });
            }
            _productService.CreateProduct(product);
            _productService.SaveChanges();
            return(StatusCode(201, new
            {
                Id = product.Id
            }));
        }
Example #2
0
        public IHttpActionResult Create(ProductCM cm)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            try
            {
                // Create product
                Product product = AutoMapper.Mapper.Map <ProductCM, Product>(cm);
                product.Picture = null;
                _productService.CreateProduct(product);
                _productService.SaveProductChanges();
                if (!String.IsNullOrEmpty(cm.Picture))
                {
                    // image stream
                    var bytes = Convert.FromBase64String(cm.Picture);
                    // physical server path
                    string filePath = System.Web.HttpContext.Current.Server.MapPath("~/Media/Product/");
                    Guid   guid     = Guid.NewGuid();
                    // SAVE IMAGE TO SERVER

                    Image image;
                    using (MemoryStream ms = new MemoryStream(bytes))
                    {
                        image = Image.FromStream(ms);
                        image.Save(filePath + guid + ".jpg");
                    }
                    // UPDATE IMAGE PATH
                    product.Picture = "/Media/Product/" + guid + ".jpg";
                    _productService.UpdateProduct(product);
                    _productService.SaveProductChanges();
                }
                // RESPONSE
                ProductVM productVM = AutoMapper.Mapper.Map <Product, ProductVM>(product);
                return(Ok(productVM));
            }
            catch (Exception e)
            {
                return(InternalServerError(e));
            }
        }