Beispiel #1
0
        public async Task <IActionResult> Create([Bind("Id,Name,EmailAddress,Passowrd,City,Street")] User user)
        {
            if (ModelState.IsValid)
            {
                if (HttpContext.Session.GetString("userName") != null && HttpContext.Session.GetString("userName").Equals("admin"))
                {
                    _context.Add(user);
                    await _context.SaveChangesAsync();

                    return(View("../Products/Index", _context.Product));
                }
                var userResult = _context.User.Where(u => u.Name == user.Name);
                if (userResult.Count() > 0)
                {
                    ViewData["Error"] = "שם משתמש קיים במערכת, בחר שם אחר!";
                    return(View(user));
                }
                else
                {
                    _context.Add(user);
                    await _context.SaveChangesAsync();

                    HttpContext.Session.SetString("userId", user.Id.ToString());
                    HttpContext.Session.SetString("userName", user.Name);
                    return(View("../Products/Index", _context.Product));
                }
            }
            return(View(user));
        }
        public async Task <IActionResult> Create([Bind("Id,ProductId,Amount,FinalPrice")] ProductInCart productInCart)
        {
            productInCart.product = _context.Product.Where(p => p.Id == productInCart.ProductId).FirstOrDefault();
            if (HttpContext.Session.GetString("userId") == null)
            {
                return(View("../users/LogIn"));
            }
            if (ModelState.IsValid)
            {
                var myShoppingCartId = HttpContext.Session.GetString("MyShoppingCartId");
                if (myShoppingCartId == null)
                {
                    var myNewShoppingCart = new ShoppingCart()
                    {
                        DateCreate = DateTime.Now,
                        products   = new List <ProductInCart>(),
                        UserId     = int.Parse(HttpContext.Session.GetString("userId")),
                        user       = _context.User.Where(u => u.Id == int.Parse(HttpContext.Session.GetString("userId"))).FirstOrDefault()
                    };
                    _context.Add(myNewShoppingCart);
                    await _context.SaveChangesAsync();

                    myShoppingCartId = myNewShoppingCart.Id.ToString();
                    HttpContext.Session.SetString("MyShoppingCartId", myShoppingCartId);
                }

                var newProductInCart = new ProductInCart()
                {
                    Amount         = productInCart.Amount,
                    product        = _context.Product.Where(p => p.Id == productInCart.ProductId).FirstOrDefault(),
                    ProductId      = productInCart.ProductId,
                    FinalPrice     = (float)Math.Round(productInCart.Amount * productInCart.product.Price, 2),
                    ShoppingCart   = _context.ShoppingCart.Where(S => S.Id == int.Parse(myShoppingCartId)).FirstOrDefault(),
                    ShoppingCartId = int.Parse(myShoppingCartId)
                };
                var prodactExsits = _context.ProductInCart.Where(p => p.ProductId == productInCart.ProductId && p.ShoppingCartId == int.Parse(myShoppingCartId)).FirstOrDefault();
                if (prodactExsits != null)
                {
                    newProductInCart.Id = prodactExsits.Id;
                    return(View("../Products/Index", _context.Product));
                }


                _context.Add(newProductInCart);
                await _context.SaveChangesAsync();

                return(View("../Products/Index", _context.Product));
            }
            ViewData["ProductId"] = new SelectList(_context.Product, "Id", "Desc", productInCart.ProductId);
            return(View(productInCart));
        }
Beispiel #3
0
        public async Task <IActionResult> Create([Bind("Id,Name,Desc,Price,ImgURL")] Product product)
        {
            if (HttpContext.Session.GetString("userName") == null || !HttpContext.Session.GetString("userName").Equals("Admin"))
            {
                return(View("../Products/Index", _context.Product));
            }
            if (ModelState.IsValid)
            {
                _context.Add(product);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(product));
        }