public ActionResult UpdateProduct(ProductModel update) { SIEBUEntities db = new SIEBUEntities(); Product target_product = db.Products.FirstOrDefault(p => p.p_id == update.id); Product_History history = new Product_History(); //if no product is found, create one bool add_new = false; if (target_product == null) { add_new = true; target_product = new Product(); target_product.dateadded = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")); } target_product.lastmodified = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")); history.datemodified = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")); bool change_made = false; if (target_product.name != update.name || target_product.sku != update.sku) { change_made = true; history.name = target_product.name; history.sku = target_product.sku; target_product.name = update.name; target_product.sku = update.sku; } if (!((target_product.cost + target_product.shipping_cost).Equals(update.cost + update.shipping_cost))) { change_made = true; history.cost = target_product.cost; history.shipping_cost = target_product.shipping_cost; target_product.cost = Convert.ToDecimal(update.cost, CultureInfo.GetCultureInfo("en")); target_product.shipping_cost = Convert.ToDecimal(update.shipping_cost, CultureInfo.GetCultureInfo("en")); } if (target_product.avail_inventory != update.avail_inventory) { change_made = true; history.avail_inventory = target_product.avail_inventory; target_product.avail_inventory = update.avail_inventory; } target_product.short_description = update.short_description; target_product.description = update.description; if (target_product.status != update.status) { change_made = true; history.status = target_product.status; target_product.status = update.status; } if (target_product.is_featured != update.is_featured) { change_made = true; history.is_featured = target_product.is_featured; target_product.is_featured = update.is_featured; if (update.is_featured) { target_product.featured_since = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")); } else { target_product.featured_since = null; } } if (add_new) { target_product.store_id = update.store_id; db.Products.Add(target_product); db.SaveChanges(); //changes must be saved so that product_id exists for image/tag/history foreign key relationships } foreach (ProductImageModel pim in update.images) { Product_Image target_image = db.Product_Image.FirstOrDefault(p => p.pi_id == pim.id); //only save up to 8 images per product if (db.Product_Image.Count(p => p.product_id == target_product.p_id) <= 8) { bool add_new_img = false; if (target_image == null) { add_new_img = true; target_image = new Product_Image(); } target_image.product_id = target_product.p_id; target_image.sort = pim.order; //check to make sure update does not match existing content if ((target_image.url == null || !(pim.url + "").Contains(target_image.url)) && pim.url != null) { //if different from original, determine whether value is blank (for purpose of removing images) if (pim.url != null) target_image.url = ImageController.UploadFile(pim.url, target_image.product_id + "" + target_image.pi_id + "" + target_product.lastmodified.Value.ToString("ffffff")); else target_image.url = null; } if (add_new_img) db.Product_Image.Add(target_image); } if (pim.delete) db.Product_Image.Remove(target_image); } foreach (ProductTagModel ptm in update.tags) { //create tag if non-existent Tag ref_tag = db.Tags.FirstOrDefault(t => t.caption == ptm.caption.Trim().ToLower()); if (ref_tag == null) { ref_tag = new Tag(); ref_tag.caption = ptm.caption.Trim().ToLower(); db.Tags.Add(ref_tag); db.SaveChanges(); } //tolower to compare captions Product_Tag target_tag = db.Product_Tag.FirstOrDefault(t => t.product_id == update.id && t.Tag.t_id == ref_tag.t_id); bool add_new_tag = false; if (target_tag == null) { add_new_tag = true; target_tag = new Product_Tag(); } target_tag.product_id = target_product.p_id; target_tag.tag_id = ref_tag.t_id; if (add_new_tag) db.Product_Tag.Add(target_tag); if (ptm.delete) db.Product_Tag.Remove(target_tag); } if (change_made) db.Product_History.Add(history); history.product_id = target_product.p_id; db.SaveChanges(); return Json(target_product.p_id, JsonRequestBehavior.AllowGet); }
/// <summary> /// ProductManagePartial loads the manage partial using the product ID /// </summary> /// <param name="id">product id</param> /// <returns></returns> public ActionResult ProductManagePartial(int id = 0) { ProductModel model = new ProductModel(); SIEBUEntities db = new SIEBUEntities(); Product cur_product = db.Products.FirstOrDefault(p => p.p_id == id); if (cur_product != null) model = new ProductModel(cur_product, db: db); return PartialView("_ProductManagePartial", model); }