Exemple #1
0
        // To protect from overposting attacks, enable the specific properties you want to bind to, for
        // more details, see https://aka.ms/RazorPagesCRUD.
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            _context.Attach(Cart).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CartExists(Cart.ItemId))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(RedirectToPage("./Index"));
        }
Exemple #2
0
        // To protect from overposting attacks, enable the specific properties you want to bind to, for
        // more details, see https://aka.ms/RazorPagesCRUD.
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }
            string UserID = User.FindFirst(ClaimTypes.NameIdentifier).Value;

            Cart.UserId      = UserID;
            Cart.DateCreated = DateTime.Now;

            SQLmessage  = "Select * From ShoppingCartItems Where UserId = '" + Cart.UserId + "'";
            CurrentCart = await _context.ShoppingCartItems.FromSqlRaw(SQLmessage).ToListAsync();

            if (CurrentCart.Count == 0)
            {
                SQLmessage = "Select * From ShoppingCartItems";
                IList <Cart> AllCart = await _context.ShoppingCartItems.FromSqlRaw(SQLmessage).ToListAsync();

                if (AllCart.Count == 0)
                {
                    Cart.CartId = 1;
                }
                else
                {
                    Cart.CartId = AllCart[AllCart.Count - 1].CartId + 1;
                }
            }
            else if (CurrentCart[CurrentCart.Count - 1].Paid)
            {
                SQLmessage = "Select * From ShoppingCartItems";
                IList <Cart> AllCart = await _context.ShoppingCartItems.FromSqlRaw(SQLmessage).ToListAsync();

                Cart.CartId = AllCart[AllCart.Count - 1].CartId + 1;
            }
            else
            {
                Cart.CartId = CurrentCart[CurrentCart.Count - 1].CartId;
            }

            SQLmessage = "Select * From Product Where ID = '" + Cart.ProductId + "'";
            Products   = await _context.Product.FromSqlRaw(SQLmessage).ToListAsync();

            Cart.Cost = Products[0].Price;

            _context.ShoppingCartItems.Add(Cart);
            await _context.SaveChangesAsync();

            return(RedirectToPage("./Index"));
        }
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Cart = await _context.ShoppingCartItems.FindAsync(id);

            if (Cart != null)
            {
                _context.ShoppingCartItems.Remove(Cart);
                await _context.SaveChangesAsync();
            }

            return(RedirectToPage("./Index"));
        }
        // To protect from overposting attacks, enable the specific properties you want to bind to, for
        // more details, see https://aka.ms/RazorPagesCRUD.
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }
            Order.UserID = User.FindFirst(ClaimTypes.NameIdentifier).Value;
            SQLmessage   = "Select * From ShoppingCartItems Where UserId = '" + Order.UserID + "' AND Paid = 'False'";
            AllCart      = await _context.ShoppingCartItems.FromSqlRaw(SQLmessage).ToListAsync();

            Order.CartID = AllCart[AllCart.Count - 1].CartId;
            foreach (Cart item in AllCart)
            {
                Order.TotalPrice += (item.Cost * item.Quantity);
            }
            Order.CreatedTime = DateTime.Now;
            _context.OrderList.Add(Order);
            await _context.SaveChangesAsync();

            SQLmessage = "Update ShoppingCartItems SET Paid = 'True' Where UserId = '" + Order.UserID + "' AND Paid = 'False'";
            _          = _context.ShoppingCartItems.FromSqlRaw(SQLmessage).ToListAsync();
            return(RedirectToPage("./Index"));
        }