Esempio n. 1
0
        public IActionResult Index()
        {
            string User = _read.ReadSession("User");

            if (User != null)
            {
                return(View());
            }
            return(RedirectToAction("Index", "Login"));
        }
        public IActionResult Index(string sortOrder)
        {
            string user = _read.ReadCookie("User");

            if (user != null)
            {
                //if user exist
                _write.WriteToSession("User", user);
                User CurrectUser = JsonConvert.DeserializeObject <User>(user);
                ViewData["Name"] = CurrectUser.FirstName;
            }

            else
            {
                string UserPP = _read.ReadSession("User");
                if (UserPP != null)
                {
                    User CurrectUser = JsonConvert.DeserializeObject <User>(UserPP);
                    ViewData["Name"] = CurrectUser.FirstName;
                }
            }

            var prods = _repositoryProducts.GetAllProducts();

            switch (sortOrder)
            {
            case "name":
                prods = prods.OrderByDescending(p => p.Title).ToList();
                break;

            case "date":
                prods = prods.OrderBy(p => p.Date).ToList();
                break;

            case "price":
                prods = prods.OrderBy(p => p.Price).ToList();
                break;

            default:
                prods = prods.OrderBy(p => p.Title).ToList();
                break;
            }
            return(View(prods));
        }
        public IActionResult Index()
        {
            string   User     = _read.ReadSession("User");
            User     userData = null;
            Register reg      = null;

            if (User != null)
            {
                userData = JsonConvert.DeserializeObject <User>(User);
                reg      = new Register {
                    FirstName = userData.FirstName,
                    BirthDate = userData.BirthDate,
                    Email     = userData.Email,
                    LastName  = userData.LastName,
                    UserName  = userData.UserName,
                };
            }
            if (reg != null)
            {
                return(View(reg));
            }
            return(View());
        }
Esempio n. 4
0
        public IActionResult AddToCart(long productID)
        {
            string       cart        = _read.ReadCookie("Cart");
            string       currentUser = _read.ReadSession("User");
            ShoppingCart userCart    = null;

            if (currentUser == null) // No user login

            {
                if (cart == null) // No Cart Exist
                {
                    //Create New Cart with product ID
                    userCart = _cartManager.AddProduct(productID, null);
                    _write.WriteCookies("Cart", JsonConvert.SerializeObject(userCart));
                    SaveInCart(productID);
                    return(RedirectToAction("Index"));
                }
                else // Cart exist, add product only
                {
                    userCart = JsonConvert.DeserializeObject <ShoppingCart>(cart);
                    userCart = _cartManager.AddProduct(productID, userCart);
                    _write.WriteCookies("Cart", JsonConvert.SerializeObject(userCart));
                    SaveInCart(productID);
                    return(RedirectToAction("Index"));
                }
            }
            else // there is a User Login in the web store
            {
                if (cart == null)// empty cart to the user login
                {
                    long userID = JsonConvert.DeserializeObject <User>(currentUser).UserId;
                    userCart = _cartManager.AddProduct(productID, null, userID);
                    _write.WriteCookies("Cart", JsonConvert.SerializeObject(userCart));
                    SaveInCart(productID);
                    return(RedirectToAction("Index"));
                }
                else // cart with product already
                {
                    userCart = JsonConvert.DeserializeObject <ShoppingCart>(cart);
                    userCart = _cartManager.AddProduct(productID, userCart);
                    _write.WriteCookies("Cart", JsonConvert.SerializeObject(userCart));
                    SaveInCart(productID);
                    return(RedirectToAction("Index"));
                }
            }
        }