Exemple #1
0
        public ActionResult DeletePages(int id)
        {
            using (EShoppingDb db = new EShoppingDb())
            {
                PageDTO dto = db.pages.Find(id);
                db.pages.Remove(dto);

                db.SaveChanges();
            }
            return(RedirectToAction("Index"));
        }
Exemple #2
0
        public ActionResult DeleteCategory(int id)
        {
            using (EShoppingDb db = new EShoppingDb())
            {
                CategoryDTO dto = db.Categories.Find(id);
                db.Categories.Remove(dto);

                db.SaveChanges();
            }
            return(RedirectToAction("Categories"));
        }
Exemple #3
0
 public string RenameCategory(string newCatName, int id)
 {
     using (EShoppingDb db = new EShoppingDb())
     {
         if (db.Categories.Any(c => c.Name == newCatName))
         {
             return("titletaken");
         }
         CategoryDTO dto = db.Categories.Find(id);
         dto.Name = newCatName;
         dto.Slug = newCatName.Replace(" ", "-").ToLower();
         db.SaveChanges();
     }
     return("ok");
 }
Exemple #4
0
        public void ReorderCategories(int[] id)
        {
            using (EShoppingDb db = new EShoppingDb())
            {
                int         count = 1;
                CategoryDTO dto;
                foreach (var catId in id)
                {
                    dto         = db.Categories.Find(catId);
                    dto.Sorting = count;

                    db.SaveChanges();
                    count++;
                }
            }
        }
Exemple #5
0
        public void ReorderPages(int [] id)
        {
            using (EShoppingDb db = new EShoppingDb())
            {
                int     count = 1;
                PageDTO dto;
                foreach (var pageId in id)
                {
                    dto         = db.pages.Find(pageId);
                    dto.Sorting = count;

                    db.SaveChanges();
                    count++;
                }
            }
        }
Exemple #6
0
        public ActionResult EditPage(PageVM model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }


            using (EShoppingDb db = new EShoppingDb())
            {
                int    id   = model.Id;
                string slug = "home";

                PageDTO dto = db.pages.Find(id);
                dto.Title = model.Title;

                if (model.Slug != "home")
                {
                    if (string.IsNullOrWhiteSpace(model.Slug))
                    {
                        slug = model.Title.Replace(" ", "-").ToLower();
                    }
                    else
                    {
                        slug = model.Slug.Replace(" ", "-").ToLower();
                    }
                }
                if (db.pages.Where(p => p.Id != model.Id).Any(p => p.Title == model.Title) || db.pages.Where(p => p.Id != model.Id).Any(p => p.Slug == model.Slug))
                {
                    ModelState.AddModelError("", "this page(title or slug) already exists");
                    return(View(model));
                }

                //dto the rest
                dto.Slug       = slug;
                dto.Body       = model.Body;
                dto.HasSidebar = model.HasSidebar;
                dto.Sorting    = model.Sorting;

                db.SaveChanges();

                TempData["Edited_message"] = "page edited successfully !";
            }

            return(RedirectToAction("EditPage"));
        }
Exemple #7
0
        public ActionResult EditSidebar(SidebarVM model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            using (EShoppingDb db = new EShoppingDb())
            {
                SidebarDTO dto = db.Sidebar.Find(1);

                dto.Body = model.Body;
                db.SaveChanges();
            }
            TempData["Edited_message"] = "Sidebar edited successfully !";
            return(RedirectToAction("EditSidebar"));
        }
Exemple #8
0
        public ActionResult AddPage(PageVM model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            using (EShoppingDb db = new EShoppingDb())
            {
                string slug;

                PageDTO dto = new PageDTO();
                dto.Title = model.Title;

                if (string.IsNullOrWhiteSpace(model.Slug))
                {
                    slug = model.Title.Replace(" ", "-").ToLower();
                }
                else
                {
                    slug = model.Slug.Replace(" ", "-").ToLower();
                }
                if (db.pages.Any(p => p.Title == model.Title) || db.pages.Any(p => p.Slug == model.Slug))
                {
                    ModelState.AddModelError("", "this page(title or slug) already exists");
                    return(View(model));
                }

                //dto the rest
                dto.Slug       = slug;
                dto.Body       = model.Body;
                dto.HasSidebar = model.HasSidebar;
                dto.Sorting    = model.Sorting;

                db.pages.Add(dto);
                db.SaveChanges();
            }

            TempData["Success_message"] = "you have addedd a new page ";
            return(RedirectToAction("AddPage"));
        }
Exemple #9
0
        public string AddNewCategory(string catName)
        {
            string id;

            using (EShoppingDb db = new EShoppingDb())
            {
                if (db.Categories.Any(c => c.Name == catName))
                {
                    return("titletaken");
                }

                CategoryDTO dto = new CategoryDTO();
                dto.Name    = catName;
                dto.Slug    = catName.Replace(" ", "-").ToLower();
                dto.Sorting = 100;
                db.Categories.Add(dto);
                db.SaveChanges();

                id = dto.Id.ToString();
            }
            return(id);
        }
Exemple #10
0
        public ActionResult AddProduct(ProductVM model, HttpPostedFileBase file)
        {
            //check model state
            if (!ModelState.IsValid)
            {
                using (EShoppingDb db = new EShoppingDb())
                {
                    model.Categories = new SelectList(db.Categories.ToList(), "id", "Name");
                    return(View(model));
                }
            }


            // check if the produc name is unique
            using (EShoppingDb db = new EShoppingDb())
            {
                if (db.Products.Any(p => p.Name == model.Name))
                {
                    model.Categories = new SelectList(db.Categories.ToList(), "id", "Name");
                    ModelState.AddModelError("", "the product name is taken !");
                    return(View(model));
                }
            }


            //declare prod id
            int id;

            using (EShoppingDb db = new EShoppingDb())
            {
                ProductDTO prod = new ProductDTO();
                prod.Name        = model.Name;
                prod.Slug        = model.Name.Replace(" ", "-").ToLower();
                prod.Description = model.Description;
                prod.Price       = model.Price;
                prod.CategoryId  = model.CategoryId;

                CategoryDTO cat = db.Categories.Find(Convert.ToInt32(prod.CategoryId));
                prod.CategoryName = cat.Name;

                db.Products.Add(prod);
                db.SaveChanges();

                //get id after put in DB
                id = prod.Id;
            }
            TempData["Success_message"] = "Product added successfully !";

            var originalDirectory = new DirectoryInfo(string.Format("{0}Images\\Uploads", Server.MapPath(@"\")));

            var pathString1 = Path.Combine(originalDirectory.ToString(), "Products");
            var pathString2 = Path.Combine(originalDirectory.ToString(), "Products\\" + id.ToString());
            var pathString3 = Path.Combine(originalDirectory.ToString(), "Products\\" + id.ToString() + "\\Thumbs");
            var pathString4 = Path.Combine(originalDirectory.ToString(), "Products\\" + id.ToString() + "\\Gallery");
            var pathString5 = Path.Combine(originalDirectory.ToString(), "Products\\" + id.ToString() + "\\Gallery\\Thumbs");

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

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

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

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

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

            //check the file is uploaded
            if (file != null && file.ContentLength > 0)
            {
                string extension = file.ContentType.ToLower();
                if (extension != "image/jpg" &&
                    extension != "image/jpeg" &&
                    extension != "image/pjpeg" &&
                    extension != "image/gif" &&
                    extension != "image/x-png" &&
                    extension != "image/png")
                {
                    using (EShoppingDb db = new EShoppingDb())
                    {
                        model.Categories = new SelectList(db.Categories.ToList(), "id", "Name");
                        ModelState.AddModelError("", "Image was not uploaded or wrong image formate !");
                        return(View(model));
                    }
                }
                string imageName = file.FileName;

                using (EShoppingDb db = new EShoppingDb())
                {
                    ProductDTO dto = db.Products.Find(id);
                    dto.ImageName = imageName;
                    db.SaveChanges();
                }
                //set original and thumb image paths
                var path  = string.Format("{0}\\{1}", pathString2, imageName);
                var path1 = string.Format("{0}\\{1}", pathString3, imageName);

                //save original
                file.SaveAs(path);

                //create and save thumb
                WebImage img = new WebImage(file.InputStream);
                img.Resize(200, 200);
                img.Save(path1);
            }

            return(RedirectToAction("AddProduct"));
        }