示例#1
0
        public IActionResult Put([FromBody] ProductViewModel model)
        {
            //return a generic HTTP Status 500 (Server Error)
            //if the client payload is invalid
            if (model == null)
            {
                return(new StatusCodeResult(500));
            }

            //map the ViewModel to the model
            var product = model.Adapt <Product>();

            //override those properties
            //that should be set from the server only
            product.Text        = model.Text;
            product.Notes       = model.Notes;
            product.ProductName = model.ProductName;
            product.ImagePath   = model.ImagePath;

            //set from server-side
            product.CreatedDate      = DateTime.Now;
            product.LastModifiedDate = product.CreatedDate;

            //add the product
            DbContext.Products.Add(product);
            //persist the changes into the database
            DbContext.SaveChanges();

            //return the newly-created Product to the client
            return(new JsonResult(product.Adapt <ProductViewModel>(), JsonSettings));
        }
示例#2
0
 public async Task <int> UpdateProduct(ProductViewModel product)
 {
     return(await _optionFactory.UpdateProduct(product.Adapt <ProductModel>()));
 }