Exemple #1
0
        public IActionResult AddToCart(int itemId)
        {
            var product = _context.products /*Include(p => p.item)*/.SingleOrDefault(p => p.ItemId == itemId);

            if (product != null)
            {
                //var userId = /*int.Parse*/(User.FindFirstValue(ClaimTypes.NameIdentifier).ToString());
                //var userId = _context.Users.Where(p=>p.Id==);



                var order = _context.orders.FirstOrDefault(o => o.username == User.Identity.Name && !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.Price,
                            Count     = 1
                        });
                    }
                }
                else
                {
                    order = new Order()
                    {
                        IsFinaly = false,
                        dateTime = DateTime.Now,
                        username = User.Identity.Name
                    };
                    _context.orders.Add(order);
                    _context.SaveChanges();
                    _context.orderDetails.Add(new OrderDetail()
                    {
                        OrderId   = order.OrderId,
                        ProductId = product.Id,
                        Price     = product.Price,
                        Count     = 1
                    });
                }

                _context.SaveChanges();
            }


            return(RedirectToAction("ShowCart"));
        }
Exemple #2
0
 public void GetAll(Filess Filee)
 {
     _context.Filess.Add(Filee);
     _context.SaveChanges();
 }