Example #1
0
        public List <NavGrouping> CreateCartControllerNavList(AppUser user)
        {
            List <NavGrouping> grouping = new List <NavGrouping>();

            NavGrouping actions = new NavGrouping();

            actions.GroupingHeader = "Actions";
            actions.NavItems       = new List <NavItem>();

            NavItem continueShopping = new NavItem();

            continueShopping.Destination     = "/Store";
            continueShopping.DestinationName = "Continue Shopping";

            NavItem clearCart = new NavItem();

            clearCart.Destination     = "/Cart/EmptyCart";
            clearCart.DestinationName = "Empty Cart";

            actions.NavItems.Add(continueShopping);
            actions.NavItems.Add(clearCart);

            if (user.AssertValidOrder())
            {
                NavItem purchase = new NavItem();
                purchase.Destination     = "/Cart/Purchase";
                purchase.DestinationName = "Purchase";

                actions.NavItems.Add(purchase);
            }

            grouping.Add(actions);

            return(grouping);
        }
Example #2
0
        public async Task <bool> ValidateOrder()
        {
            AppUser user = await GetCurrentUser();

            return(user.AssertValidOrder());
        }
Example #3
0
        public async Task <Order> CreateOrder()
        {
            AppUser user = await GetCurrentUser();

            ICollection <ShoppingCartEntry> cartEntries = user.ShoppingCartEntries;

            if (user == null || cartEntries == null || cartEntries.Count == 0 || !user.AssertValidOrder())
            {
                return(null);
            }

            DateTime orderDate = DateTime.Now;
            Order    order     = new Order(user, orderDate);

            userRepository.InsertOrder(order);
            unitOfWork.Save();


            foreach (ShoppingCartEntry entry in cartEntries)
            {
                IEnumerable <ProductKey> keys = entry.Listing.ProductKeys.Take(entry.Quantity);
                // part of removed code below
                //int remainingQuantity = entry.Quantity - keys.Count;

                foreach (ProductKey productKey in keys)
                {
                    productKey.Listing.Quantity--;

                    listingRepository.UpdateListingSimple(productKey.Listing);
                    listingRepository.DeleteProductKey(productKey.ProductKeyID);

                    ClaimedProductKey claimedKey = new ClaimedProductKey(productKey, user, orderDate, "Purchase - Order #" + order.OrderID);
                    //user.AddClaimedProductKey(claimedKey);
                    userRepository.InsertClaimedProductKey(claimedKey);

                    ProductOrderEntry orderEntry = new ProductOrderEntry(order, entry);
                    orderEntry.AddClaimedProductKey(claimedKey);

                    order.AddProductOrderEntry(orderEntry);
                }
                #region removed
                //                // the case where there are some product keys, but not enough to fulfill the order (the remainining are keys of child listings bundled together)
                //                // rarely triggered (if ever)
                //                if (entry.Listing.ChildListings != null && entry.Listing.ChildListings.Count > 0 && keys != null && keys.Count > 0 && keys.Count < entry.Quantity)
                //                {
                //                    foreach (ProductKey productKey in keys)
                //                    {
                //                        productKey.Listing.Quantity--;
                //                        productKey.Listing.UpdateParentQuantities();

                //                        listingRepository.UpdateListing(productKey.Listing);
                //                        listingRepository.DeleteProductKey(productKey.ProductKeyID);

                //                        ClaimedProductKey claimedKey = new ClaimedProductKey(productKey, user, orderDate, "Purchase - Order #" + order.OrderID);
                //                        user.AddClaimedProductKey(claimedKey);
                //                        userRepository.InsertClaimedProductKey(claimedKey);
                //                        //unitOfWork.Save();

                //                        ProductOrderEntry orderEntry = new ProductOrderEntry(order, entry);
                //                        order.AddProductOrderEntry(orderEntry);
                //                        userRepository.InsertProductOrderEntry(orderEntry);
                //                        //unitOfWork.Save();
                //                        orderEntry.AddClaimedProductKey(claimedKey);
                //                    }

                //                    keys = new List<ProductKey>();

                //                    foreach (Listing childListing in entry.Listing.ChildListings)
                //                    {
                //                        keys.AddRange(listingRepository.GetProductKeys().Where(k => k.ListingID == childListing.ListingID).Take(remainingQuantity));
                //                    }
                //                }
                //                else if (entry.Listing.ChildListings != null && entry.Listing.ChildListings.Count > 0 && keys.Count < entry.Quantity)
                //                {
                //                    foreach (Listing childListing in entry.Listing.ChildListings)
                //                    {
                //                        keys.AddRange(listingRepository.GetProductKeys().Where(k => k.ListingID == childListing.ListingID).Take(entry.Quantity));
                //                    }
                //                }

                //                if (entry.Listing.ChildListings == null || entry.Listing.ChildListings.Count == 0 || keys.Count == entry.Quantity)
                //                {
                //                    foreach (ProductKey productKey in keys)
                //                    {
                //                        productKey.Listing.Quantity--;
                //                        productKey.Listing.UpdateParentQuantities();

                //                        listingRepository.UpdateListing(productKey.Listing);
                //                        listingRepository.DeleteProductKey(productKey.ProductKeyID);

                //                        ClaimedProductKey claimedKey = new ClaimedProductKey(productKey, user, orderDate, "Purchase - Order #" + order.OrderID);
                //                        user.AddClaimedProductKey(claimedKey);
                //                        userRepository.InsertClaimedProductKey(claimedKey);
                //                        //unitOfWork.Save();

                //                        ProductOrderEntry orderEntry = new ProductOrderEntry(order, entry);
                //                        userRepository.InsertProductOrderEntry(orderEntry);
                //                       // unitOfWork.Save();
                //                        orderEntry.AddClaimedProductKey(claimedKey);

                //                        order.AddProductOrderEntry(orderEntry);
                //                    }
                //                }
                //                else
                //                {
                //                    for (int i = 0; i < remainingQuantity; i++)
                //                    {
                //                        ProductOrderEntry orderEntry = new ProductOrderEntry(order, entry);
                //                        order.AddProductOrderEntry(orderEntry);
                //                        userRepository.InsertProductOrderEntry(orderEntry);
                //                        //unitOfWork.Save();

                //                        foreach (Listing childListing in entry.Listing.ChildListings)
                //                        {
                //                            ProductKey productKey = keys.Where(k => k.Listing.ListingID == childListing.ListingID).First();
                //                            keys.Remove(productKey);

                //                            productKey.Listing.Quantity--;
                //                           // productKey.Listing.UpdateParentQuantities();

                //                            listingRepository.UpdateListing(productKey.Listing);
                //                            listingRepository.DeleteProductKey(productKey.ProductKeyID);

                //                            ClaimedProductKey claimedKey = new ClaimedProductKey(productKey, user, orderDate, "Purchase - Order #" + order.OrderID);
                //                            userRepository.InsertClaimedProductKey(claimedKey);
                //                            //unitOfWork.Save();
                //                            orderEntry.AddClaimedProductKey(claimedKey);
                //                            user.AddClaimedProductKey(claimedKey);
                //                        }

                //                        order.AddProductOrderEntry(orderEntry);
                //                    }
                //                }

                //                //unitOfWork.Save();
                #endregion
            }

            DeleteShoppingCart(user);

            int totalSalePrice = order.TotalSalePrice();

            BalanceEntry balanceEntry = new BalanceEntry(user, "Purchase - Order #" + order.OrderID, 0 - totalSalePrice, orderDate);
            //user.BalanceEntries.Add(balanceEntry);
            userRepository.InsertBalanceEntry(balanceEntry);

            user.Balance -= totalSalePrice;
            //user.AddOrder(order);
            userRepository.UpdateOrder(order);
            await userRepository.UpdateAppUserSimple(user);

            this.unitOfWork.Save();

            return(userRepository.GetOrderByID(order.OrderID));
        }