public async Task<Order> CreateOrder() { AppUser user = await GetCurrentUser(); if (!user.AssertValidOrder()) { return null; } DateTime orderDate = new DateTime(); orderDate = DateTime.Now; Order order = new Order(user, orderDate); userRepository.InsertOrder(order); this.unitOfWork.Save(); ICollection<ShoppingCartEntry> cartEntries = user.ShoppingCartEntries; foreach (ShoppingCartEntry entry in cartEntries) { List<ProductKey> keys = listingRepository.GetProductKeys().Where(k => k.ListingID == entry.ListingID).Take(entry.Quantity).ToList(); int remainingQuantity = entry.Quantity - keys.Count; if (keys.Count > 0 && keys.Count < entry.Quantity && entry.Listing.ChildListings != null) { foreach (ProductKey productKey in keys) { listingRepository.DeleteProductKey(productKey.ProductKeyID); ClaimedProductKey claimedKey = new ClaimedProductKey(productKey, user, orderDate, "Purchase - Order #" + order.OrderID); user.AddClaimedProductKey(claimedKey); userRepository.InsertClaimedProductKey(claimedKey); unitOfWork.Save(); //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 (keys.Count < entry.Quantity && entry.Listing.ChildListings != null) { 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) { 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); 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); } } Listing listing = listingRepository.GetListingByID(entry.ListingID); listing.Quantity -= entry.Quantity; listing.UpdateParentQuantities(); listingRepository.UpdateListing(listing); unitOfWork.Save(); } await DeleteShoppingCart(); BalanceEntry balanceEntry = new BalanceEntry(user, "Purchase - Order #" + order.OrderID, 0 - order.TotalSalePrice() , orderDate); user.BalanceEntries.Add(balanceEntry); //userRepository.InsertBalanceEntry(balanceEntry); user.Balance -= order.TotalSalePrice(); user.AddOrder(order); await userRepository.UpdateAppUser(user); this.unitOfWork.Save(); return userRepository.GetOrderByID(order.OrderID); }