public void Create(ProductViewModel product)
        {
            var entity = new Product();

            entity.ProductName = product.ProductName;
            entity.UnitPrice = product.UnitPrice;
            entity.UnitsInStock = (short)product.UnitsInStock;
            entity.Discontinued = product.Discontinued;
            entity.CategoryID = product.CategoryID;

            if (entity.CategoryID == null)
            {
                entity.CategoryID = 1;
            }

            if (product.Category != null)
            {
                entity.CategoryID = product.Category.CategoryID;
            }

            entities.Products.Add(entity);
            entities.SaveChanges();

            product.ProductID = (int)entity.ProductID;
        }
 public static void Delete(ProductViewModel product)
 {
     ProductViewModel target = One(p => p.ProductID == product.ProductID);
     if (target != null)
     {
         All().Remove(target);
     }
 }
		public ActionResult EditingInline_Destroy([DataSourceRequest] DataSourceRequest request, ProductViewModel product)
        {            
            if (product != null)
            {                
                productService.Destroy(product);                
            }

            return Json(new[] { product }.ToDataSourceResult(request, ModelState));
        }
		public ActionResult EditingInline_Update([DataSourceRequest] DataSourceRequest request, ProductViewModel product)
        {
            if (product != null && ModelState.IsValid)
            {
                productService.Update(product);
            }            

            return Json(new[]{product}.ToDataSourceResult(request,ModelState));
        }
        public ActionResult ColumnSettings_Destroy([DataSourceRequest] DataSourceRequest request, ProductViewModel product)
        {            
            if (product != null)
            {                
                productService.Destroy(product);                
            }

            return Json(ModelState.ToDataSourceResult());
        }
        public ActionResult ColumnSettings_Update([DataSourceRequest] DataSourceRequest request, ProductViewModel product)
        {
            if (product != null && ModelState.IsValid)
            {
                productService.Update(product);
            }            

            return Json(ModelState.ToDataSourceResult());
        }
        public ActionResult Globalization_Create([DataSourceRequest] DataSourceRequest request, ProductViewModel product)
        {
            if (product != null && ModelState.IsValid)
            {
                productService.Create(product);
            }

            return Json(new[] { product }.ToDataSourceResult(request, ModelState));
        }
        public IActionResult Post(ProductViewModel product)
        {
			if (!ModelState.IsValid)
			{
				return HttpBadRequest(ModelState.Values.SelectMany(v => v.Errors).Select(error => error.ErrorMessage));
            }

			service.Create(product);

			return new ObjectResult(new DataSourceResult { Data = new[] { product }, Total = 1 });
		}
 public static void Update(ProductViewModel product)
 {
     ProductViewModel target = One(p => p.ProductID == product.ProductID);
     if (target != null)
     {
         target.ProductName = product.ProductName;
         target.UnitPrice = product.UnitPrice;
         target.UnitsInStock = product.UnitsInStock;
         target.Discontinued = product.Discontinued;
         target.LastSupply = product.LastSupply;
     }
 }
        public void Update(ProductViewModel product)
        {
            var entity = new Product();

            entity.ProductID = product.ProductID;
            entity.ProductName = product.ProductName;
            entity.UnitPrice = product.UnitPrice;
            entity.UnitsInStock = (short)product.UnitsInStock;
            entity.Discontinued = product.Discontinued;
            entity.CategoryID = product.CategoryID;

            if (product.Category != null)
            {
                entity.CategoryID = product.Category.CategoryID;
            }

            entities.Products.Attach(entity);
            entities.Entry(entity).State = EntityState.Modified;
            entities.SaveChanges();
        }
        public IActionResult Put(int id, ProductViewModel product)
        {
			if (ModelState.IsValid && id == product.ProductID)
			{
				try
				{
					service.Update(product);
				}
				catch (DbUpdateConcurrencyException)
				{
					return new HttpNotFoundResult();
				}

				return new HttpStatusCodeResult(200);
            }
			else
			{
				return HttpBadRequest(ModelState.Values.SelectMany(v => v.Errors).Select(error => error.ErrorMessage));
			}
		}
        public void Destroy(ProductViewModel product)
        {
            var entity = new Product();

            entity.ProductID = product.ProductID;
			
            entities.Products.Attach(entity);

            entities.Products.Remove(entity);

            var orderDetails = entities.OrderDetails.Where(pd => pd.ProductID == entity.ProductID);

            foreach (var orderDetail in orderDetails)
            {
                entities.OrderDetails.Remove(orderDetail);
            }

            entities.SaveChanges();
        }
        public static void Insert(ProductViewModel product)
        {
            product.ProductID = All().OrderByDescending(p => p.ProductID).First().ProductID + 1;

            All().Insert(0, product);
        }