Example #1
0
        /// <summary>
        /// Creates a new BasketItem.
        /// </summary>
        /// <param name="productId">The Product's Id.</param>
        /// <param name="basketId">The Basket's Id.</param>
        /// <returns>Void</returns>
        public async Task AddBasketItem(int productId, int basketId)
        {
            BasketItem item = new BasketItem()
            {
                BasketID  = basketId,
                ProductID = productId,
                Quantity  = 1
            };

            await _context.BasketItems.AddAsync(item);

            await _context.SaveChangesAsync();
        }
Example #2
0
        /// <summary>
        /// Creates a new Order.
        /// </summary>
        /// <param name="userId">The user's Id.</param>
        /// <param name="basket">The user's basket.</param>
        /// <returns>The new order.</returns>
        public async Task <Order> CreateOrder(Basket basket, CheckoutViewModel cvm)
        {
            List <OrderItem> oItems = new List <OrderItem>();

            decimal total = 0;

            foreach (BasketItem item in basket.BasketItems)
            {
                total += item.Quantity * item.Product.Price;
            }

            Order order = new Order()
            {
                ApplicationUserID = basket.ApplicationUserID,
                CardNumber        = cvm.CardNumber,
                City            = cvm.City,
                TotalPrice      = total,
                ShippingAddress = cvm.ShippingAddress,
                Zip             = cvm.Zip
            };

            await _context.Orders.AddAsync(order);

            await _context.SaveChangesAsync();

            foreach (BasketItem bItem in basket.BasketItems)
            {
                oItems.Add(new OrderItem
                {
                    Quantity  = bItem.Quantity,
                    ProductID = bItem.ProductID,
                    Price     = bItem.Product.Price,
                    OrderID   = order.ID
                });
            }

            await _context.OrderItems.AddRangeAsync(oItems);

            await _context.SaveChangesAsync();

            order.OrderItems = oItems;

            return(order);
        }
Example #3
0
 /// <summary>
 /// Adds a new Product to the Database.
 /// </summary>
 /// <param name="product">New Product to add.</param>
 public async Task AddProduct(Product product)
 {
     _context.Products.Add(product);
     await _context.SaveChangesAsync();
 }