public int EditProduct(EditProductModels editProductModels)
        {
            try
            {
                //LoginProductModels loginProductModels = Session["Login"] as LoginProductModels;
                Product product = con.Products.Find(editProductModels.ID);

                if (product != null)
                {
                    product.Name                = editProductModels.Name;
                    product.MetaTitle           = editProductModels.MetaTitle;
                    product.Description         = editProductModels.Description;
                    product.Image               = editProductModels.Image;
                    product.AltImage            = editProductModels.AltImage;
                    product.MoreImages          = editProductModels.MoreImages;
                    product.Price               = editProductModels.Price;
                    product.Percent             = editProductModels.Percent;
                    product.PromotionPrice      = editProductModels.PromotionPrice;
                    product.Quantity            = editProductModels.Quantity;
                    product.CategoryID          = editProductModels.CategoryID;
                    product.DescriptionIdDetail = editProductModels.DescriptionIdDetail;
                    product.Warranty            = editProductModels.Warranty;
                    product.CreatedDate         = editProductModels.CreatedDate;
                    product.CreatedBy           = editProductModels.CreatedBy;
                    product.ModifiedDate        = editProductModels.ModifiedDate;
                    product.ModifiedBy          = editProductModels.ModifiedBy;
                    product.MetaKeywords        = editProductModels.MetaKeywords;
                    product.MetaDescriptions    = editProductModels.MetaDescriptions;
                    product.Status              = editProductModels.Status;
                    product.TopHot              = editProductModels.TopHot;
                    product.Sale                = editProductModels.Sale;
                    product.ViewCount           = editProductModels.ViewCount;
                    product.IsActive            = editProductModels.IsActive;

                    con.SaveChanges();
                    return(Constants.RETURN_TRUE);
                }
                else
                {
                    return(Constants.RETURN_FALSE);
                }
            }
            catch
            {
                return(Constants.RETURN_FALSE);
            }
        }
        public ActionResult DeleteProduct(int id)
        {
            AppDbContext   dbContext = new AppDbContext();
            ProductModels  product   = dbContext.getProducts.SingleOrDefault(p => p.Id == id);
            ModelModels    model     = dbContext.getModels.SingleOrDefault(m => m.Id == product.ModelId);
            BrandModels    brand     = dbContext.getBrands.SingleOrDefault(b => b.BrandId == model.BrandId);
            CategoryModels category  = dbContext.getCategories.SingleOrDefault(c => c.CategoryId == model.CategoryId);

            EditProductModels toBeDeleted = new EditProductModels
            {
                Id         = Guid.NewGuid(),
                Model      = model.Name,
                Brand      = brand.Brand,
                Category   = category.Category,
                Price      = product.Price,
                Color      = product.Color,
                Storage    = product.Storage,
                Processor  = product.Processor,
                Memory     = product.Memory,
                Display    = product.Display,
                Details    = product.Details,
                EditedBy   = User.Identity.Name,
                EditedDate = DateTime.Now,
                ProductId  = product.Id
            };

            dbContext.addDeletedProduct(toBeDeleted);

            List <ProductPhoto> photos = dbContext.getProductPhotos(id).ToList();

            if (photos.Count > 0)
            {
                foreach (var photo in photos)
                {
                    System.IO.File.Delete(photo.Src);
                    dbContext.deleteProductPhoto(photo.Id);
                }
            }

            dbContext.deleteProduct(id);

            return(RedirectToAction("Index"));
        }
        public ActionResult EditProduct(List <HttpPostedFileBase> Photo, EditProductViewModel product)
        {
            if (ModelState.IsValid)
            {
                AppDbContext dbContext = new AppDbContext();

                ModelModels model = dbContext.getModels.SingleOrDefault(m => m.Id == product.ModelId);

                ProductModels oldProduct = dbContext.getProducts.SingleOrDefault(p => p.Id == product.Id);

                BrandModels brand = dbContext.getBrands.SingleOrDefault(b => b.BrandId == model.BrandId);

                CategoryModels category = dbContext.getCategories.SingleOrDefault(c => c.CategoryId == model.CategoryId);

                EditProductModels editProduct = new EditProductModels
                {
                    Id         = Guid.NewGuid(),
                    Model      = model.Name,
                    Brand      = brand.Brand,
                    Category   = category.Category,
                    Price      = oldProduct.Price,
                    Color      = oldProduct.Color,
                    Storage    = oldProduct.Storage,
                    Processor  = oldProduct.Processor,
                    Memory     = oldProduct.Memory,
                    Display    = oldProduct.Display,
                    Details    = oldProduct.Details,
                    EditedBy   = User.Identity.Name,
                    EditedDate = DateTime.Now,
                    ProductId  = product.Id
                };

                dbContext.addEditedProduct(editProduct);


                ProductModels productModels = new ProductModels();
                productModels.Id        = product.Id;
                productModels.Price     = product.Price;
                productModels.Color     = product.Color;
                productModels.Storage   = product.Storage;
                productModels.Processor = product.Processor;
                productModels.Memory    = product.Memory;
                productModels.Display   = product.Display;
                productModels.Details   = product.Details;
                productModels.ModelId   = product.ModelId;

                productModels.Photos = new List <ProductPhoto>();



                List <ProductPhoto> photos = new List <ProductPhoto>();

                foreach (var photo in Photo)
                {
                    if (photo != null)
                    {
                        List <ProductPhoto> Photos = dbContext.getProductPhotos(product.Id).ToList();

                        if (Photos.Count > 0)
                        {
                            foreach (var item in Photos)
                            {
                                System.IO.File.Delete(item.Src);
                                dbContext.deleteProductPhoto(item.Id);
                            }
                        }


                        var fileName   = Path.GetFileName(photo.FileName);
                        var path       = "/Public/Products/" + model.Name;
                        var photoUrl   = Server.MapPath(path);
                        var photoTitle = Path.GetFileNameWithoutExtension(photo.FileName);
                        var uniqName   = Guid.NewGuid().ToString() + "_" + fileName;

                        if (!Directory.Exists(photoUrl))
                        {
                            Directory.CreateDirectory(photoUrl);
                        }

                        var          photoPath = Path.Combine(photoUrl, uniqName);
                        ProductPhoto newPhoto  = new ProductPhoto
                        {
                            Path      = path,
                            Src       = photoPath,
                            Title     = uniqName,
                            ProductId = product.Id
                        };
                        photos.Add(newPhoto);

                        photo.SaveAs(photoPath);
                    }
                }
                dbContext.addProductPhoto(photos);
                dbContext.updateProduct(productModels);


                return(RedirectToAction("Index"));
            }

            return(View(product));
        }