/// <summary>
        /// Sends and process an order to the database.
        /// </summary>
        /// <param name="order">The order to process</param>
        public async Task SendOrderTransaction(IOrder order)
        {
            using var context = new DigitalStoreContext(Options);

            if (order.ShoppingCartQuantity.Count == 0)
            {
                throw new OrderException("Cannot submit order with no products in cart.");
            }

            var productIds    = order.ShoppingCartQuantity.Keys.Select(s => s.Id).ToList();
            var foundProducts = await context.Products.Where(p => productIds.Contains(p.Id)).ToListAsync();

            if (productIds.Count != foundProducts.Count)
            {
                throw new OrderException("One or more Products in cart did not exist in the database.");
            }

            PurchaseOrder purchaseOrder = new PurchaseOrder()
            {
                // TODO: Replace with proper authentication and unique identification using a Login System
                CustomerId      = order.Customer.Id,
                DateProcessed   = DateTime.Now,
                OrderLines      = new List <OrderLine>(),
                StoreLocationId = order.StoreLocation.Id
            };

            await AddProductsToOrder(order, context, foundProducts, purchaseOrder);

            await context.PurchaseOrders.AddAsync(purchaseOrder);

            await context.SaveChangesAsync();
        }
Beispiel #2
0
        /// <summary>
        /// Creates a customer and adds it into the database. It does not check for duplicates before creating it.
        /// </summary>
        /// <param name="firstName">The first name of the customer.</param>
        /// <param name="lastName">The last name of the customer.</param>
        public async Task CreateCustomerAsync(Library.Model.INewCustomer customer)
        {
            using var context = new DigitalStoreContext(Options);

            bool lastNameIsEmpty = string.IsNullOrWhiteSpace(customer.LastName);

            await context.Customers.AddAsync(new Customer()
            {
                FirstName = customer.FirstName,
                LastName  = lastNameIsEmpty ? null : customer.LastName
            });

            await context.SaveChangesAsync();
        }