コード例 #1
0
        /// <summary>
        /// Adds an item to the user's cart
        /// </summary>
        /// <param name="productId">The product's id</param>
        /// <param name="username">The user's username</param>
        /// <returns>Shopping cart id</returns>
        public int AddItem(int productId, string username)
        {
            if (this.context.ShoppingCarts.FirstOrDefault(c => c.User.UserName == username) == null)
            {
                CreateCart(username);
            }

            var shoppingCartId = this.context.ShoppingCarts.FirstOrDefault(c => c.User.UserName == username).Id;

            CartItem item = this.context.CartItems.Where(i => i.ShoppingCartId == shoppingCartId).FirstOrDefault(i => i.ProductId == productId);

            if (item == null || item.Placed == true)
            {
                var newItem = new CartItem()
                {
                    ProductId      = productId,
                    ShoppingCart   = this.context.ShoppingCarts.FirstOrDefault(c => c.Id == shoppingCartId),
                    ShoppingCartId = shoppingCartId,
                    Quantity       = 1
                };
                this.context.CartItems.Add(newItem);
            }
            else
            {
                item.Quantity++;
            }

            context.SaveChanges();
            return(shoppingCartId);
        }
コード例 #2
0
        /// <summary>
        /// Creates a snowboard and adds it to the database
        /// </summary>
        /// <param name="name">Snowboard name</param>
        /// <param name="imagePath">Snowboard image path</param>
        /// <param name="price">Snowboard price</param>
        /// <param name="size">Snowboard size in cm</param>
        /// <param name="description">Snowboard description</param>
        /// <param name="brandId">Snowboard brand id</param>
        /// <param name="profile">Snowboard profile</param>
        /// <param name="flex">Snowboard flex rating</param>
        /// <returns>Snowboard id</returns>
        public int CreateSnowboard(string name, string imagePath, decimal price, float size, string description, int brandId, Profile profile, byte flex)
        {
            var snowboard = new Snowboard()
            {
                Name        = name,
                ImagePath   = imagePath,
                Price       = price,
                Size        = size,
                Description = description,
                BrandId     = brandId,
                Profile     = profile,
                Flex        = flex
            };

            context.Snowboards.Add(snowboard);
            context.SaveChanges();

            return(snowboard.Id);
        }