コード例 #1
0
        public void AddToCart(Medication album, int quantity)
        {
            var cartAlbum = _applicationDbContext.DrugCart.SingleOrDefault(s => s.Drug.DrugId == album.DrugId && s.Cart.CartId == CartId);// razlika
            var cart      = _applicationDbContext.Cart.FirstOrDefault(t => t.CartId == CartId);

            System.Diagnostics.Debug.WriteLine("kart" + CartId.ToString());
            if (cartAlbum == null)
            {
                cartAlbum = new DrugCart
                {
                    Cart     = this,
                    Drug     = album,
                    Quantity = 1
                };
                _applicationDbContext.DrugCart.Add(cartAlbum);
                System.Diagnostics.Debug.WriteLine("CARTALBUM" + cartAlbum.DrugCartId.ToString());
            }
            else
            {
                cartAlbum.Quantity++;
            }
            _applicationDbContext.SaveChanges();
            foreach (DrugCart dc in _applicationDbContext.DrugCart.ToList())
            {
                System.Diagnostics.Debug.WriteLine("DCS" + dc.DrugCartId.ToString());
            }
        }
コード例 #2
0
ファイル: ShoppingCart.cs プロジェクト: AmanRathee/DotNetCore
        public static ShoppingCart GetCart(IServiceProvider services)
        {
            Guid     CartId;
            ISession session   = services.GetRequiredService <IHttpContextAccessor>()?.HttpContext.Session;
            var      dbContext = services.GetService <AppDbContext>();

            //var cartIdFromSession = session.GetString("CartId");
            Guid cartIdFromSession;

            if (!string.IsNullOrEmpty(session.GetString("CartId")) && Guid.TryParse(session.GetString("CartId"), out cartIdFromSession))
            {
                CartId = cartIdFromSession;
            }
            else
            {
                CartId = Guid.NewGuid();
            }
            session.SetString("CartId", CartId.ToString());
            return(new ShoppingCart(dbContext)
            {
                ShoppingCartId = CartId
            });
        }
コード例 #3
0
 public override string ToString()
 {
     return($"{{{nameof(CartId)}={CartId.ToString()}, {nameof(ProductId)}={ProductId.ToString()}, {nameof(Quantity)}={Quantity.ToString()}}}");
 }