Пример #1
0
        public string AddNewCategory(string catName)
        {
            // Declare id
            string id;

            using (CmsShoppingCartContext db = new CmsShoppingCartContext())
            {
                // Check that the category name is unique
                if (db.Categories.Any(x => x.Name == catName))
                {
                    return("titletaken");
                }

                // Init DTO
                Category dto = new Category();

                // Add to DTO
                dto.Name    = catName;
                dto.Slug    = catName.Replace(" ", "-").ToLower();
                dto.Sorting = 100;

                // Save DTO
                db.Categories.Add(dto);
                db.SaveChanges();

                // Get the id
                id = dto.Id.ToString();
            }

            // Return id
            return(id);
        }
Пример #2
0
        public ActionResult Login(LoginUserVM model)
        {
            //check model state
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            //check if the user is valid
            bool isValid = false;

            using (CmsShoppingCartContext db = new CmsShoppingCartContext())
            {
                if (db.Users.Any(x => x.UserName.Equals(model.Username) && x.Password.Equals(model.Password)))
                {
                    isValid = true;
                }

                if (!isValid) //if not valid
                {
                    ModelState.AddModelError("", "Invalid username or password");
                    return(View(model));
                }
                else
                {
                    //seting a cookie or session for a user
                    FormsAuthentication.SetAuthCookie(model.Username, model.RemberME);
                    return(Redirect(FormsAuthentication.GetRedirectUrl(model.Username, model.RemberME)));
                }
            }
            return(View());
        }
Пример #3
0
        //display /list products inluding pagination
        //Get: Admin/Shop/Products
        public ActionResult Products(int?page, int?catId)   //nullable int? of both page and catId (filter to products via catrgaoires)
        {
            //Declare a list of ProductVM
            List <ProductVM> listOfProductVM;

            //Set page number
            var pageNumber = page ?? 1;

            using (CmsShoppingCartContext db = new CmsShoppingCartContext())
            {
                //Init the list
                listOfProductVM = db.Products.ToArray()
                                  .Where(x => catId == null || catId == 0 || x.CategoryId == catId)
                                  .Select(x => new ProductVM(x))
                                  .ToList();
                //Populate categoires select list
                ViewBag.Categories = new SelectList(db.Categories.ToList(), "Id", "Name");
                //Set selected category
                ViewBag.SelectedCat = catId.ToString();
            }
            //Set pagination
            var onePageOfProducts = listOfProductVM.ToPagedList(pageNumber, 3); // will only contain 25 products max because of the pageSize

            ViewBag.OnePageOfProducts = onePageOfProducts;
            //Return view with list


            return(View(listOfProductVM));
        }
Пример #4
0
        public ActionResult EditProduct(int?id)
        {
            //declare productVM
            ProductVM model;

            using (CmsShoppingCartContext db = new CmsShoppingCartContext())
            {
                //Get ptoduct
                Product dto = db.Products.Find(id);

                //make  sure it exsists
                if (dto == null)
                {
                    return(Content("That product does not exists."));
                }

                //init model
                model = new ProductVM(dto);

                //make a select list
                model.Categories = new SelectList(db.Categories.ToList(), "Id", "Name");

                //Get gallery images
                model.GalleryImages = Directory.EnumerateFiles(Server.
                                                               MapPath("~/Images/Uploads/Products/" + id + "/Gallery/Thubs"))
                                      .Select(fileName => Path.GetFileName(fileName));
            }


            //Return view with model.

            return(View(model));
        }
Пример #5
0
        public string RenameCategory(string newCatName, int id)
        {
            using (CmsShoppingCartContext db = new CmsShoppingCartContext())
            {
                //check category name is unique
                if (db.Categories.Any(x => x.Name == newCatName))
                {
                    return("title taken");
                }

                //Get DTO
                Category dto = db.Categories.Find(id);

                //Edit DTO
                dto.Name = newCatName;
                dto.Slug = newCatName.Replace(" ", "-").ToLower();
            }


            //Save
            db.SaveChanges();


            //Return
            return("Ok");
        }
        public ActionResult EditPage(int id)
        {
            //declarre page vm
            PageVM model;

            using (CmsShoppingCartContext db = new CmsShoppingCartContext())
            {
                //get page
                Page dto = db.Pages.Find(id);

                //confirm page exists
                if (dto == null)
                {
                    return(Content("The page dose not exist.")); // returns a string
                }

                //ini page vm

                model = new PageVM(dto); //alternatively if we had no view modelPgage Ve would be:

                /* model = new PageVM()
                 *   {
                 *      Id = dto.Id,
                 *       Body = dto.Body, .......
                 *    }; */
            }
            //return view with model
            return(View(model));
        }
Пример #7
0
        protected void Application_AuthenticateRequest()
        {
            //check if user is logged in
            if (User == null)
            {
                return;
            }

            //Get username
            string username = Context.User.Identity.Name;

            //Declare roles
            string[] roles = null;

            using (CmsShoppingCartContext db = new CmsShoppingCartContext())
            {
                //Populate roles
                UserDTO dto = db.Users.FirstOrDefault(x => x.UserName == username);

                roles = db.UserRoles.Where(x => x.UserId == dto.Id).Select(x => x.Role.Name).ToArray();
            }

            //Build IPrincipal object
            IIdentity  userIdentity = new GenericIdentity(username);             //passing in user name
            IPrincipal newUserObj   = new GenericPrincipal(userIdentity, roles); //passing user Identity and roles

            //Update Context.User
            Context.User = newUserObj;
        }
Пример #8
0
        public ActionResult CreateAccount(UserVM model)
        {
            //check model state
            if (!ModelState.IsValid)
            {
                return(View("createAccount", model));
            }
            //check if passwords match
            if (!model.Password.Equals(model.ConfirmPassword))
            {
                ModelState.AddModelError("", "Passwords do not match.");
                return(View("CreateAccount", model));
            }

            using (CmsShoppingCartContext db = new CmsShoppingCartContext())

            {
                //make sure username is unique
                if (db.Users.Any(x => x.UserName.Equals(model.UserName))) //if theres a match its a problem
                {
                    ModelState.AddModelError("", "Username" + model.UserName + "is taken");
                    model.UserName = "";
                    return(View("CreateAccount", model));
                }

                //create user DTO
                UserDTO userDTO = new UserDTO()
                {
                    //initialise its fileds
                    FirstName    = model.FirstName,
                    LastName     = model.LastName,
                    EmailAddress = model.EmailAddress,
                    UserName     = model.UserName,
                    Password     = model.Password
                };

                //Add the DTO
                db.Users.Add(userDTO);
                //Save
                db.SaveChanges();

                //Add to userRolesDTO  >>>of 2 which is for user for anyone who signs in
                int id = userDTO.Id;

                UserRoleDTO userRolesDTO = new UserRoleDTO()
                {
                    UserId = id,
                    RoleId = 2
                };

                db.UserRoles.Add(userRolesDTO);
            }
            //Crate a TempData message
            TempData["SM"] = "You are now registered and can login";

            //Redirect
            return(Redirect("~/account/login"));
        }
        public ActionResult AddPage(PageVM model)
        {
            //check model state //first thing done after submiiting a form
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            using (CmsShoppingCartContext db = new CmsShoppingCartContext())
            {
                //Declare slug
                string slug;

                //initialise DTO
                Page dto = new Page();

                //DTO title
                dto.Title = model.Title;


                //check for set slug if need be
                if (string.IsNullOrWhiteSpace(model.Slug))
                {
                    slug = model.Title.Replace(" ", "-").ToLower(); //replaces input title any white spaces with -
                }                                                   // and converts to lowercase

                else
                {
                    slug = model.Slug.Replace(" ", "-").ToLower(); //replaces input title any white spaces with -
                }                                                  // and converts to lowercase

                //make syre title and slug are unique
                if (db.Pages.Any(x => x.Title == model.Title) || db.Pages.Any(x => x.Slug == slug))
                {
                    ModelState.AddModelError("", "Title or slug already exists.");
                    return(View(model));
                }

                //DTO the rest
                dto.Slug       = slug;
                dto.Body       = model.Body;
                dto.HasSidebar = model.HasSidebar;
                dto.Sorting    = 100; //when ever you add a page it will be the last page, since there wont be more than 100 items in the menu



                //Save DTO
                db.Pages.Add(dto);
                db.SaveChanges();
            }
            //set TempData message
            TempData["SM"] = "You have added a new page!"; //tempdata persists ulinke view bag

            //Redirect

            return(RedirectToAction("AddPage"));
        }
Пример #10
0
        public ActionResult UserProfile(UserProfileVM model)
        {
            //check model state
            if (!ModelState.IsValid)
            {
                return(View("UserProfile", model)); //being specific of paostback method
            }

            //check if passwords match if need be
            if (!string.IsNullOrWhiteSpace(model.Password))
            {
                if (!model.Password.Equals(model.ConfirmPassword))
                {
                    ModelState.AddModelError("", "Passwords do not match");
                    return(View("UserProfile", model));
                }
            }


            using (CmsShoppingCartContext db = new CmsShoppingCartContext())
            {
                //Get username
                string username = User.Identity.Name;
                //Make sure username is unique
                if (db.Users.Where(x => x.Id != model.Id).Any(x => x.UserName == username))
                {
                    //if theres a match we have a problem
                    ModelState.AddModelError("", "Username" + model.UserName + "already exists");
                    model.UserName = ""; //reset username
                    return(View("UserProfile", model));
                }

                //Edit DTO
                UserDTO dto = db.Users.Find(model.Id);

                dto.FirstName    = model.FirstName;
                dto.UserName     = model.LastName;
                dto.EmailAddress = model.EmailAddress;
                dto.UserName     = model.UserName;

                if (!string.IsNullOrWhiteSpace(model.Password)) //if passowrd is not temp edeit otherwiserse do nothing
                {
                    //if it is
                    dto.Password = model.Password;
                }

                //Save
                db.SaveChanges();
            }

            //Set TempData message
            TempData["SM"] = "You have edited your profile";

            //Redirect

            return(Redirect("~/account/user-profile")); /////
        }
Пример #11
0
        // GET: Admin/Pages
        public ActionResult Index()
        {
            // Declare list of PageVM
            List <PageVM> pagesList;

            using (CmsShoppingCartContext db = new CmsShoppingCartContext())
            {
                //Init the list
                pagesList = db.Pages.ToArray().OrderBy(x => x.Sorting).Select(x => new PageVM(x)).ToList();
            }
            //Return View with list
            return(View());
        }
        public ActionResult _PagesMenuPartial()
        {
            // Declare a list of PageVM
            List <PageVM> pageVMList;

            // Get all pages except home
            using (CmsShoppingCartContext db = new CmsShoppingCartContext())
            {
                pageVMList = db.Pages.ToArray().OrderBy(x => x.Sorting).Where(x => x.Slug != "home").Select(x => new PageVM(x)).ToList();
            }
            // Return partial view with list
            return(PartialView(pageVMList));
        }
Пример #13
0
        public ActionResult AddProduct()
        {
            //initilaise the model
            ProductVM model = new ProductVM();

            //Add select list of categories to model
            using (CmsShoppingCartContext db = new CmsShoppingCartContext())
            {
                model.Categories = new SelectList(db.Categories.ToList(), "Id", "Name");///get Id and name from Catagegories Tabel
            }

            //Return view with modoel

            return(View(model));
        }
        public ActionResult _SidebarPartial()
        {
            //Declare model
            SidebarVM model;

            //Init model
            using (CmsShoppingCartContext db = new CmsShoppingCartContext())
            {
                Sidebar dto = db.Sidebars.Find(1);

                model = new SidebarVM(dto);
            }


            //Return partial view with model
            return(PartialView(model));
        }
Пример #15
0
        public ActionResult UserProfile()
        {
            //Get username
            string username = User.Identity.Name;

            //Declare model
            UserProfileVM model;

            using (CmsShoppingCartContext db = new CmsShoppingCartContext())
            {
                //Get user
                UserDTO dto = db.Users.FirstOrDefault(x => x.UserName == username);

                //Build model
                model = new UserProfileVM(dto);
            }

            //Return view with model

            return(View("UserProfile", model)); //specify viewname with profile.
        }
Пример #16
0
        //Get: Admin/Shop/DeleteProducts

        public ActionResult DeleteProduct(int id)
        {
            //Delete product from DB
            using (CmsShoppingCartContext db = new CmsShoppingCartContext())
            {
                Product dto = db.Products.Find(id);
                db.Products.Remove(dto);
                db.SaveChanges();
            }

            //Delete product folder.

            var    originalDirectory = new DirectoryInfo(string.Format("{0}Images\\Uploads", Server.MapPath(@"")));
            string pathString        = Path.Combine(originalDirectory.ToString(), "Products\\" + id.ToString());

            if (Directory.Exists(pathString))
            {
                Directory.Delete(pathString, true); //to delete folders and sub- directories
            }
            //redirect to action
            return(RedirectToAction("Products"));
        }
 public CategoriesController(CmsShoppingCartContext context)
 {
     _context = context;
 }
 public ProductController(CmsShoppingCartContext context)
 {
     this.context = context;
 }
 public ProductsController(CmsShoppingCartContext context, IWebHostEnvironment hostEnviroment)
 {
     _context        = context;
     _hostEnviroment = hostEnviroment;
 }
Пример #20
0
 public PagesController(CmsShoppingCartContext context)
 {
     this.context = context;
 }
 public CategoriesController(CmsShoppingCartContext contex)
 {
     this.contex = contex;
 }
Пример #22
0
 public ProductsController(CmsShoppingCartContext contex, IWebHostEnvironment webHostEnvironment)
 {
     this.contex             = contex;
     this.webHostEnvironment = webHostEnvironment;
 }
Пример #23
0
 public ProductsController(CmsShoppingCartContext contex)
 {
     this.contex = contex;
 }
 public ProductsController(CmsShoppingCartContext context)
 {
     _context = context;
 }