public IActionResult AddToCart(int itemId)
        {
            var product = _context.Products.Include(p => p.Item).SingleOrDefault(p => p.ItemId == itemId);

            if (product != null)
            {
                int userId = int.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier).ToString());
                var order  = _context.Orders.FirstOrDefault(o => o.UserId == userId && !o.IsFinaly);
                if (order != null)
                {
                    var orderDetail =
                        _context.OrderDetails.FirstOrDefault(d => d.OrderId == order.OrderId && d.ProductId == product.Id);
                    if (orderDetail != null)
                    {
                        orderDetail.Count += 1;
                    }
                    else
                    {
                        _context.OrderDetails.Add(new OrderDetail()
                        {
                            OrderId   = order.OrderId,
                            ProductId = product.Id,
                            Price     = product.Item.Price,
                            Count     = 1
                        });
                    }
                }
                else
                {
                    order = new Order()
                    {
                        IsFinaly   = false,
                        CreateDate = DateTime.Now,
                        UserId     = userId
                    };
                    _context.Orders.Add(order);
                    _context.SaveChanges();
                    _context.OrderDetails.Add(new OrderDetail()
                    {
                        OrderId   = order.OrderId,
                        ProductId = product.Id,
                        Price     = product.Item.Price,
                        Count     = 1
                    });
                }
                _context.SaveChanges();
            }
            return(RedirectToAction("ShowCart"));
        }
        public IActionResult OnPost()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var product = _context.Products.Find(Product.Id);
            var item    = _context.Items.First(p => p.Id == product.ItemId);

            product.Name         = Product.Name;
            product.Description  = Product.Description;
            item.Price           = Product.Price;
            item.QuantityInStock = Product.QuantityInStock;

            _context.SaveChanges();

            if (Product.Picture?.Length > 0)
            {
                string filePath = Path.Combine(Directory.GetCurrentDirectory(),
                                               "wwwroot",
                                               "images",
                                               product.Id + Path.GetExtension(Product.Picture.FileName));
                using (var stream = new FileStream(filePath, FileMode.Create))
                {
                    Product.Picture.CopyTo(stream);
                }
            }

            return(RedirectToPage("Index"));
        }
Example #3
0
        public IActionResult OnPost()
        {
            var product = _context.Products.Find(Product.Id);
            var item    = _context.Items.First(p => p.Id == product.ItemId);

            _context.Items.Remove(item);
            _context.Products.Remove(product);

            _context.SaveChanges();

            string filePath = Path.Combine(Directory.GetCurrentDirectory(),
                                           "wwwroot",
                                           "images",
                                           product.Id + ".jpg");

            if (System.IO.File.Exists(filePath))
            {
                System.IO.File.Delete(filePath);
            }
            return(RedirectToPage("Index"));
        }
Example #4
0
 public void AddUser(Users user)
 {
     _context.Add(user);
     _context.SaveChanges();
 }