public ActionResult New()
        {
            if (User.IsInRole("Admin"))
            {
                var userid       = User.Identity.GetUserId();
                var cart         = _context.Carts.Where(c => c.UserId == userid).First();
                var products     = _context.Products.ToList();
                var Categories   = _context.Categories.ToList();
                var productscart = _context.CartsProducts.Include(cp => cp.Product).Where(cp => cp.CartId == cart.id).ToList();

                var Roles = _context.Roles.ToList();



                //
                var viewmodel = new ProductCartViewHome
                {
                    Cart         = cart,
                    CatrtProduct = productscart,
                    Categoris    = Categories,
                    Products     = products,
                    //Role=new IdentityRole()
                };
                return(View("RolesForm", viewmodel));
            }
            else
            {
                return(HttpNotFound());
            }
        }
        public ViewResult Index()
        {
            var viewmodel = new ProductCartViewHome();

            if (Request.IsAuthenticated)
            {
                var userid       = User.Identity.GetUserId();
                var cart         = _context.Carts.Where(c => c.UserId == userid).First();
                var products     = _context.Products.Include(p => p.Category).ToList();
                var Categories   = _context.Categories.ToList();
                var productscart = _context.CartsProducts.Include(cp => cp.Product).Where(cp => cp.CartId == cart.id).ToList();



                viewmodel.Cart         = cart;
                viewmodel.Products     = products;
                viewmodel.Categoris    = Categories;
                viewmodel.CatrtProduct = productscart;
            }
            else
            {
                var products   = _context.Products.ToList();
                var Categories = _context.Categories.ToList();
                viewmodel.Products = products;

                viewmodel.Categoris = Categories;
            }

            //var categories = _context.Categories.ToList();
            return(View(viewmodel));
        }
        public ActionResult Show(int id)
        {
            if (User.IsInRole("Admin"))
            {
                var userid = User.Identity.GetUserId();

                var cart = _context.Carts.Where(c => c.UserId == userid).First();

                var order    = _context.Orders.Single(or => or.id == id);
                var products = _context.OrderProducts.Include(cp => cp.Product).Where(cp => cp.OrderId == order.id).ToList();
                //  var product = _context.Carts.ToList();
                //var prod = _context.Products.ToList().Select(_context.CartsProducts);
                var cartproduct = _context.CartsProducts.Include(cp => cp.Product).Where(cp => cp.CartId == cart.id);
                var category    = _context.Categories.ToList();
                var viewmode    = new ProductCartViewHome
                {
                    Cart         = cart,
                    OrderProduct = products,
                    Categoris    = category,
                    CatrtProduct = cartproduct
                };


                return(View(viewmode));
            }
            return(HttpNotFound());
        }
        public ActionResult Edit(int id)
        {/////
            var viewmodel = new ProductCartViewHome();


            var userid = User.Identity.GetUserId();
            var cart   = _context.Carts.Where(c => c.UserId == userid).First();


            var Categories   = _context.Categories.ToList();
            var productscart = _context.CartsProducts.Include(cp => cp.Product).Where(cp => cp.CartId == cart.id).ToList();

            var products = _context.Products.Where(p => p.CategoryId == id).ToList();

            viewmodel.Cart = cart;
            var category = _context.Categories.SingleOrDefault(c => c.id == id);

            viewmodel.Categoris    = Categories;
            viewmodel.CatrtProduct = productscart;
            viewmodel.Products     = products;
            viewmodel.Category     = category;

            if (category == null)
            {
                return(HttpNotFound());
            }

            ///////



            return(View("CategoryForm", viewmodel));
            //  return View();
        }
Ejemplo n.º 5
0
        public ActionResult CheckOut(int cartid)
        {
            var userid = User.Identity.GetUserId();
            var cart   = _context.Carts.Where(c => c.id == cartid).First();


            var products = _context.CartsProducts.Include(cp => cp.Product).Where(cp => cp.CartId == cart.id).ToList();
            //  var product = _context.Carts.ToList();
            //var prod = _context.Products.ToList().Select(_context.CartsProducts);
            var category = _context.Categories.ToList();
            var user     = _context.Users.SingleOrDefault(u => u.Id == userid);

            var viewmode = new ProductCartViewHome
            {
                Cart         = cart,
                CatrtProduct = products,
                Categoris    = category,
                Users        = user
            };


            return(View(viewmode));

            //return HttpNotFound();
            //return View();
        }
        public ActionResult Index()
        {
            if (User.IsInRole("Admin"))
            {
                var product  = _context.Products.Include(p => p.Category).OrderByDescending(p => p.id).ToList();
                var category = _context.Categories.ToList();

                //
                var userid       = User.Identity.GetUserId();
                var cart         = _context.Carts.Where(c => c.UserId == userid).First();
                var orders       = _context.Orders.Include(or => or.User).ToList();
                var Categories   = _context.Categories.ToList();
                var productscart = _context.CartsProducts.Include(cp => cp.Product).Where(cp => cp.CartId == cart.id).ToList();


                //
                var viewmodel = new ProductCartViewHome
                {
                    Cart         = cart,
                    CatrtProduct = productscart,
                    Categoris    = Categories,
                    Orders       = orders,
                };
                return(View(viewmodel));
            }
            else
            {
                return(HttpNotFound());
            }
        }
        public ActionResult Details(string id)
        {
            if (User.IsInRole("Admin"))
            {
                var viewmodel = new ProductCartViewHome();


                var userid       = User.Identity.GetUserId();
                var cart         = _context.Carts.Where(c => c.UserId == userid).First();
                var user         = _context.Users.SingleOrDefault(p => p.Id == id);
                var Categories   = _context.Categories.ToList();
                var productscart = _context.CartsProducts.Include(cp => cp.Product).Where(cp => cp.CartId == cart.id).ToList();
                var orders       = _context.Orders.Where(or => or.userid == id).ToList();



                viewmodel.Cart         = cart;
                viewmodel.Users        = user;
                viewmodel.Categoris    = Categories;
                viewmodel.CatrtProduct = productscart;
                viewmodel.Orders       = orders;


                return(View(viewmodel));
            }
            else
            {
                return(HttpNotFound());
            }
        }
        public ActionResult Cart(string id)
        {
            var userid = User.Identity.GetUserId();

            if (userid == id)
            {
                var cart     = _context.Carts.Where(c => c.UserId == id).First();
                var products = _context.CartsProducts.Include(cp => cp.Product).Where(cp => cp.CartId == cart.id).ToList();
                // var product = _context.Carts.ToList();
                //var prod = _context.Products.ToList().Select(_context.CartsProducts);
                var category = _context.Categories.ToList();
                var viewmode = new ProductCartViewHome
                {
                    Cart         = cart,
                    CatrtProduct = products,
                    Categoris    = category
                };


                return(View(viewmode));
            }
            else
            {
                return(HttpNotFound());
            }
        }
        //
        // GET: /Manage/Index
        public async Task <ActionResult> Index(ManageMessageId?message)
        {
            ViewBag.StatusMessage =
                message == ManageMessageId.ChangePasswordSuccess ? "Your password has been changed."
                : message == ManageMessageId.SetPasswordSuccess ? "Your password has been set."
                : message == ManageMessageId.SetTwoFactorSuccess ? "Your two-factor authentication provider has been set."
                : message == ManageMessageId.Error ? "An error has occurred."
                : message == ManageMessageId.AddPhoneSuccess ? "Your phone number was added."
                : message == ManageMessageId.RemovePhoneSuccess ? "Your phone number was removed."
                : "";
            var products   = _context.Products.ToList();
            var categories = _context.Categories.ToList();
            var userId     = User.Identity.GetUserId();
            var cart       = _context.Carts.Single(c => c.UserId == userId);
            var model      = new ProductCartViewHome
            {
                Products          = products,
                Categoris         = categories,
                Cart              = cart,
                HasPassword       = HasPassword(),
                PhoneNumber       = await UserManager.GetPhoneNumberAsync(userId),
                TwoFactor         = await UserManager.GetTwoFactorEnabledAsync(userId),
                Logins            = await UserManager.GetLoginsAsync(userId),
                BrowserRemembered = await AuthenticationManager.TwoFactorBrowserRememberedAsync(userId)
            };

            return(View(model));
        }
        public ActionResult Edit(string id)
        {
            if (User.IsInRole("Admin"))
            {
                /////////////////
                var viewmodel = new ProductCartViewHome();


                var userid = User.Identity.GetUserId();
                // var user = _context.Users.SingleOrDefault(c => c.Id ==userid);
                var cart     = _context.Carts.Where(c => c.UserId == userid).First();
                var products = _context.Products.Include(p => p.Category).ToList();
                // var product = _context.Products.SingleOrDefault(c => c.id == id);
                var Categories   = _context.Categories.ToList();
                var productscart = _context.CartsProducts.Include(cp => cp.Product).Where(cp => cp.CartId == cart.id).ToList();
                var user         = _context.Users.Single(u => u.Id == id);
                var Roles        = _context.Roles.ToList();
                viewmodel.Cart = cart;
                // viewmodel.Product = product;
                viewmodel.Categoris    = Categories;
                viewmodel.CatrtProduct = productscart;
                viewmodel.Users        = user;
                viewmodel.Roles        = Roles;


                // viewmodel.users = usersin;



                ///
                //  var product = _context.Products.SingleOrDefault(c => c.id == id);
                if (user == null)
                {
                    return(HttpNotFound());
                }
                //var viewmodel = new ProductCategoryModelForm
                //{
                //    Product = product,
                //    Categories = _context.Categories.ToList()
                //};
                return(View("UserForm", viewmodel));
            }
            else
            {
                return(HttpNotFound());
            }
        }
Ejemplo n.º 11
0
        public ActionResult New()
        {
            if (User.IsInRole("Admin"))
            {
                ///////////////
                var viewmodel = new ProductCartViewHome();


                var userid   = User.Identity.GetUserId();
                var cart     = _context.Carts.Where(c => c.UserId == userid).First();
                var products = _context.Products.Include(p => p.Category).ToList();

                var Categories   = _context.Categories.ToList();
                var productscart = _context.CartsProducts.Include(cp => cp.Product).Where(cp => cp.CartId == cart.id).ToList();


                viewmodel.Cart         = cart;
                viewmodel.Product      = new Product();
                viewmodel.Categoris    = Categories;
                viewmodel.CatrtProduct = productscart;



                ///////
                var categories = _context.Categories.ToList();
                var viewmodel1 = new ProductCategoryModelForm
                {
                    //to  give id=0 value
                    Product    = new Product(),
                    Categories = categories
                };
                return(View("ProductForm", viewmodel));
            }
            else
            {
                return(HttpNotFound());
            }
        }
        public ActionResult Save(Category category)
        {
            if (!ModelState.IsValid)
            {
                var viewmodel = new ProductCartViewHome();


                var userid   = User.Identity.GetUserId();
                var cart     = _context.Carts.Where(c => c.UserId == userid).First();
                var products = _context.Products.Include(p => p.Category).ToList();

                var Categories   = _context.Categories.ToList();
                var productscart = _context.CartsProducts.Include(cp => cp.Product).Where(cp => cp.CartId == cart.id).ToList();
                viewmodel.Category = category;

                viewmodel.Cart = cart;
                //    viewmodel.Product = new Product();
                viewmodel.Categoris    = Categories;
                viewmodel.CatrtProduct = productscart;

                return(View("CategoryForm", viewmodel));
            }

            if (category.id == 0)
            {
                _context.Categories.Add(category);
            }
            else
            {
                var categoryinDB = _context.Categories.Single(c => c.id == category.id);
                categoryinDB.Name = category.Name;
            }

            _context.SaveChanges();
            return(RedirectToAction("Index", "Categories"));
        }
        public async Task <ActionResult> Save(string Id, string Roles)
        {
            var viewmodel = new ProductCartViewHome();
            var users     = _context.Users.ToList();

            //   var Roles = _context.Roles.ToList();
            viewmodel.users = users;
            //    viewmodel.Roles = Roles;
            var role = _context.Roles.Where(r => r.Name == Roles);
            var user = _context.Users.Where(u => u.Id == Id);
            // var users = _context.Users.ToList();

            var roles = _context.Roles.ToList();

            if (user != null)
            {
                await UserManager.AddToRoleAsync(Id, Roles);

                return(RedirectToAction("Index"));
            }
            else
            {
                return(HttpNotFound());
            }


            //  ApplicationUser user = _context.Users.Where(u => u.UserName.Equals(UserName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();

            // var user = UserManager.FindByName(UserName);
            ////  var user = _context.Users.Single(u => u.Id ==id.Id);
            //var userid = User.Identity.GetUserId();
            //var cart = _context.Carts.Where(c => c.UserId == userid).First();
            //var products = _context.Products.ToList();
            //var Categories = _context.Categories.ToList();
            //var productscart = _context.CartsProducts.Include(cp => cp.Product).Where(cp => cp.CartId == cart.id).ToList();
            ////var role = _context.Roles.Where(r => r.Id == Role.Id).First();
            //var Roles = _context.Roles.ToList();
            //var viewmodel = new ProductCartViewHome
            //{
            //    Cart = cart,
            //    CatrtProduct = productscart,
            //    Categoris = Categories,
            //    Products = products,
            //    Roles = Roles,
            //  //  Users = user
            //    //  Role=role
            //};



            //var UserManager = new UserManager<ApplicationUser>
            //    (new UserStore<ApplicationUser>(_context));

            //try
            //{
            //    var user = UserManager.FindByName(UserName);
            //    UserManager.AddToRole(user.Id, Role.Name);
            //    _context.SaveChanges();
            //}catch
            //{ }
            ////var account = new AccountController();//< --NULL REFERENCE EXCEPTION OBJECT NOT SET TO INSTANCE
            ////account.UserManager.AddToRole(user.Id, Role.Name);

            ////////////////

            //// var roleStore = new RoleStore<IdentityRole>(new ApplicationDbContext());



            //// var roleManager = new RoleManager<IdentityRole>(roleStore);
            ////await roleManager.CreateAsync(new IdentityRole());
            /////    await UserManager.AddToRoleAsync(userid,role.Name);

            ///////////////

            ////var roleStore = new RoleStore<IdentityRole>(_context);
            ////var roleManager = new RoleManager<IdentityRole>(roleStore);

            ////var userStore = new UserStore<ApplicationUser>(_context);
            ////var userManager = new UserManager<ApplicationUser>(userStore);
            ////userManager.AddToRole(user.Id, Role.Id);
            ////
            //// var userinDB = _context.Users.Single(c => c.Id == id.Id);


            ////if (user != null)
            ////{
            ////    var result = UserManager.AddToRole(user.Id, Role.Name);
            ////    if (result.Succeeded)
            ////    {
            ////        _context.SaveChanges();
            ////        return RedirectToAction("Index");
            ////    }
            ////    else
            ////        return HttpNotFound();
            ////    //   await UserManager.AddToRoleAsync(userid, viewmodel.Role.Name);

            ////}
            ////else
            ////{
            ////    return HttpNotFound();
            ////}



            ////  ViewBag.ResultMessage = "Role created successfully !";

            //// prepopulat roles for the view dropdown


            ////return View("ManageUserRoles");
        }
Ejemplo n.º 14
0
        public ActionResult Save(Product product, HttpPostedFileBase file)
        {
            //very important note//The name of the HttpPostedFileBase parameter and the name of HTML FileUpload element must be exact same,
            //otherwise the HttpPostedFileBase parameter will be NULL

            if (ModelState.IsValid)
            {
                if (file != null)
                {
                    string pic  = Path.GetFileName(file.FileName);
                    string path = Path.Combine(
                        Server.MapPath("~/resources/"), pic);
                    // file is uploaded
                    file.SaveAs(path);


                    //file.SaveAs(HttpContext.Server.MapPath("~/resources/")
                    //                                      + file.FileName);
                    product.img = file.FileName;
                }
                if (product.id == 0)
                {
                    _context.Products.Add(product);
                }

                else
                {
                    var productInDB = _context.Products.Single(c => c.id == product.id);

                    //file.SaveAs(HttpContext.Server.MapPath("~/resources/")
                    //                                     + file.FileName);
                    //string pic = System.IO.Path.GetFileName(file.FileName);
                    productInDB.img        = file.FileName;
                    productInDB.Name       = product.Name;
                    productInDB.price      = product.price;
                    productInDB.qnt        = product.qnt;
                    productInDB.CategoryId = product.CategoryId;
                    //    TryUpdateModel(productInDB);
                }
                _context.SaveChanges();
                return(RedirectToAction("Index", "Products"));
            }
            else
            {
                var viewmodel = new ProductCartViewHome();


                var userid   = User.Identity.GetUserId();
                var cart     = _context.Carts.Where(c => c.UserId == userid).First();
                var products = _context.Products.Include(p => p.Category).ToList();

                var Categories   = _context.Categories.ToList();
                var productscart = _context.CartsProducts.Include(cp => cp.Product).Where(cp => cp.CartId == cart.id).ToList();


                viewmodel.Cart         = cart;
                viewmodel.Product      = new Product();
                viewmodel.Categoris    = Categories;
                viewmodel.CatrtProduct = productscart;

                return(View("ProductForm", viewmodel));
            }
        }