public static bool addProduct(ModelView.ProductView item, HttpPostedFileBase picture) { try { db = new ShopOnlineEntities(); Product p = new Product { name = item.name, price = item.price, stock = item.stock, IDC = item.IDC, description = item.description, StatusProd = 1 }; db.Products.Add(p); db.SaveChanges(); ProImage img = new ProImage { Name = picture.FileName, IDP = p.id, StatusIMG = 1 }; db.ProImages.Add(img); db.SaveChanges(); } catch (Exception ex) { throw ex; } return(false); }
public static ModelView.ProductView getDataByID(int id) { db = new ShopOnlineEntities(); ModelView.ProductView rs = (from p in db.Products from i in db.ProImages where p.id == id && p.StatusProd == 1 && i.StatusIMG == 1 && i.IDP == p.id select new ModelView.ProductView { id = p.id, IDC = p.IDC ?? 0, CateName = p.Category.name, name = p.name, description = p.description, picture = i.Name, price = p.price, StatusProd = p.StatusProd, stock = p.stock }).FirstOrDefault(); return(rs); }
public static bool updateProduct(ModelView.ProductView item, HttpPostedFileBase picture) { db = new ShopOnlineEntities(); try { Product p = db.Products.Find(item.id) as Product; p.name = item.name; p.price = item.price; p.stock = item.stock; p.description = item.description; //p.StatusProd = item.StatusProd db.SaveChanges(); ProImage pi = db.ProImages.Where(pix => pix.IDP == p.id).FirstOrDefault() as ProImage; pi.Name = picture.FileName; db.SaveChanges(); return(true); } catch (Exception ex) { throw ex; } return(false); }