public ActionResult EditSubmit(int?id, string name, decimal price, string about, List <HttpPostedFileBase> imgFiles)
        {
            var findProduct = db.products.Find(id);

            findProduct.Name  = name;
            findProduct.Price = price;
            findProduct.About = about;
            if (imgFiles[0] != null)
            {
                string SaveLocation = "";

                foreach (var file in imgFiles)
                {
                    string fileName = Path.GetFileName(file.FileName);
                    SaveLocation = Server.MapPath("~/Template/img/newImg/") + "\\" + fileName;
                    try
                    {
                        var newImgPath = new imageProduct();
                        newImgPath.ProductId   = findProduct.Id;
                        newImgPath.imgPath     = fileName;
                        newImgPath.CreatedDate = DateTime.Now;
                        db.imageProducts.Add(newImgPath);
                        file.SaveAs(SaveLocation);
                    }
                    catch (Exception ex)
                    {
                        Response.Write("Error: " + ex.Message);
                    }
                }
            }
            db.Entry(findProduct).State = EntityState.Modified;
            db.SaveChanges();

            return(RedirectToAction("newAddProduct", "Admin"));
        }
Example #2
0
        public ActionResult SubmitCreateNewItem(string name, string about, int producerVal, int colorVal,
                                                List <int> categoryVal, int?multiCategoryVal, decimal size, decimal price, decimal discountPrice, List <HttpPostedFileBase> imgFiles)
        {
            if (categoryVal == null && multiCategoryVal == null)
            {
                TempData["emptyCategory"] = "Məhsulun Kategoriyasını daxil edin!!";
                return(View("CreateNewItem"));
            }
            if (string.IsNullOrWhiteSpace(name))
            {
                TempData["emptyNameInput"] = "Məhsulun adını daxil edin";
                return(View("CreateNewItem"));
            }
            if (imgFiles[0] != null && (categoryVal != null || multiCategoryVal != 0))
            {
                bool    findUser       = false;
                Manager findSuperAdmin = new Manager();
                Manager findAdmin      = new Manager();
                var     newProduct     = new Product();
                newProduct.Name          = name;
                newProduct.About         = about;
                newProduct.ProducerId    = producerVal;
                newProduct.colorId       = colorVal;
                newProduct.SizeORCount   = size;
                newProduct.Price         = price;
                newProduct.DiscountPrice = discountPrice;
                if (multiCategoryVal != null)
                {
                    var findMultiCategory = db.SubCategories.Where(w => w.DeletedDate == null && w.Id == multiCategoryVal).FirstOrDefault();
                    newProduct.SubCategory = findMultiCategory.SubCategoryName;
                }
                newProduct.isWaitListForAdmin = true;
                var checkAdmin = db.Manager.Where(w => w.DeletedDate == null && w.ManagerStatus.DeletedDate == null && w.ManagerStatus.StatusName == "SuperAdmin").ToList();
                foreach (var item in checkAdmin)
                {
                    if (Session[SessionKey.Admin].ToString() == item.Email)
                    {
                        findSuperAdmin = item;
                        findUser       = true;
                    }
                }
                if (findUser == true)
                {
                    newProduct.CreatedId  = findSuperAdmin.Id;
                    newProduct.SharedId   = findSuperAdmin.Id;
                    newProduct.isConfirm  = true;
                    newProduct.sharedDate = DateTime.Now;
                    var subscribeUsers = db.Subscribes.Where(w => w.DeletedDate == null && w.isConfirm == true).ToList();
                    foreach (var user in subscribeUsers)
                    {
                        MailExtention.SendMail("Yeni Məhsul", newProduct.Name + " " + newProduct.Price + "AZN", user.Email);
                    }
                }
                else
                {
                    var checkSession    = Session[SessionKey.Admin];
                    var findActiveAdmin = db.Manager.Where(w => w.DeletedDate == null && w.Email.ToString() == checkSession.ToString()).FirstOrDefault();
                    newProduct.isConfirm = false;
                    newProduct.SharedId  = findActiveAdmin.Id;
                }
                newProduct.CreatedDate = DateTime.Now;
                newProduct.imgPath     = imgFiles.FirstOrDefault().FileName;
                db.products.Add(newProduct);
                foreach (var categoryItem in categoryVal)
                {
                    var newCategoryAndProduct = new CategoryAndProduct();
                    newCategoryAndProduct.CategoryId = categoryItem;
                    newCategoryAndProduct.ProductId  = newProduct.Id;
                    db.CategoryAndProduct.Add(newCategoryAndProduct);
                }

                string SaveLocation = "";

                foreach (var file in imgFiles)
                {
                    string fileName = Path.GetFileName(file.FileName);
                    SaveLocation = Server.MapPath("~/Template/img/newImg/") + "\\" + fileName;
                    try
                    {
                        var newImgPath = new imageProduct();
                        newImgPath.ProductId   = newProduct.Id;
                        newImgPath.imgPath     = fileName;
                        newImgPath.CreatedDate = DateTime.Now;
                        db.imageProducts.Add(newImgPath);
                        db.SaveChanges();
                        file.SaveAs(SaveLocation);
                    }
                    catch (Exception ex)
                    {
                        Response.Write("Error: " + ex.Message);
                    }
                }
                db.SaveChanges();
                var newProductOrWaitList = db.products.OrderByDescending(o => o.Id).Where(p => p.DeletedDate == null && (p.isWaitListForAdmin == true && p.isConfirm == false));
                return(PartialView("~/Areas/AdGetOn/Views/Admin/newAddProduct.cshtml", newProductOrWaitList));
            }
            else
            {
                TempData["imgIsEmpty"] = "Şəkil Seçin !!";
                return(View("CreateNewItem"));
            }
        }