public async Task <ActionResult> Create(int ClaimedProductKeyID = 0) { GiveawayViewModel model = new GiveawayViewModel(); model.LoggedInUser = await userService.GetCurrentUser(); if (ClaimedProductKeyID != 0) { ClaimedProductKey key = model.LoggedInUser.ClaimedProductKeys.SingleOrDefault(x => x.ClaimedProductKeyID == ClaimedProductKeyID); if (key != null) { model.Giveaway.Listing = key.Listing; model.Giveaway.ListingID = key.ListingID; model.Giveaway.Prize = key.Key; } } /* ADD KEY TO GIVEAWAY, REMOVE USELESS FIELDS */ List <String> destNames = new List <String>(); destNames.Add(createGivesText); model.FullNavList = CreateGiveawayNavList(destNames); return(View(model)); }
public async Task CreateOrder(Order order, bool alreadyCharged, bool useDBKey = false) { // New orders should only have one product order entry ProductOrderEntry entry = order.ProductOrderEntries.FirstOrDefault(); if (entry.ListingID != 0) { entry.Listing = listingRepository.GetListingByID(entry.ListingID); } DateTime date = DateTime.Now; order.SaleDate = date; String note = "Admin-created order"; ClaimedProductKey newKey = new ClaimedProductKey(); int priceToCharge = 0; if (order.ProductOrderEntries.Count() > 0) { priceToCharge = order.ProductOrderEntries.First().SalePrice; } if (useDBKey) { ProductKey key = GetProductKey(entry.ListingID); newKey = new ClaimedProductKey(key, order.AppUser, date, note); listingRepository.DeleteProductKey(key.ProductKeyID); unitOfWork.Save(); priceToCharge = newKey.Listing.SaleOrDefaultPrice(); entry.AddClaimedProductKey(newKey);//userRepository.InsertClaimedProductKey(newKey); } if (alreadyCharged == false) { BalanceEntry balanceEntry = new BalanceEntry(); balanceEntry.Date = date; balanceEntry.AppUser = order.AppUser; balanceEntry.Notes = note; balanceEntry.PointsAdjusted = priceToCharge; order.AppUser.Balance -= priceToCharge; //userRepository.UpdateAppUser(order.AppUser); //userRepository.InsertBalanceEntry(balanceEntry); order.AppUser.AddBalanceEntry(balanceEntry); } entry.SalePrice = priceToCharge; order.AppUser.AddOrder(order); //userRepository.InsertOrder(order); await userRepository.UpdateAppUser(order.AppUser); unitOfWork.Save(); }
public string RevealKey(int keyId) { ClaimedProductKey key = userRepository.GetClaimedProductKeyByID(keyId); key.IsRevealed = true; userRepository.UpdateClaimedProductKey(key); this.unitOfWork.Save(); return(key.Key); }
public bool MarkKeyUsed(int keyId) { ClaimedProductKey key = userRepository.GetClaimedProductKeyByID(keyId); key.IsUsed = !key.IsUsed; userRepository.UpdateClaimedProductKey(key); this.unitOfWork.Save(); return(key.IsUsed); }
public void EditClaimedProductKey(ClaimedProductKey key) { ClaimedProductKey updatedKey = userRepository.GetClaimedProductKeyByID(key.ClaimedProductKeyID); updatedKey.AcquisitionTitle = key.AcquisitionTitle; updatedKey.ListingID = key.ListingID; updatedKey.IsGift = key.IsGift; updatedKey.Key = key.Key; userRepository.UpdateClaimedProductKey(updatedKey); unitOfWork.Save(); }
public void CreateClaimedProductKey(ClaimedProductKey key) { if (key.ListingID != 0) { key.Listing = listingRepository.GetListingByID(key.ListingID); } key.Date = DateTime.Now; userRepository.InsertClaimedProductKey(key); unitOfWork.Save(); }
public void UpdateClaimedProductKey(ClaimedProductKey claimedProductKey) { ClaimedProductKey targetClaimedProductKey = context.ClaimedProductKeys.Find(claimedProductKey.ClaimedProductKeyID); if (targetClaimedProductKey != null) { targetClaimedProductKey.AcquisitionTitle = claimedProductKey.AcquisitionTitle; targetClaimedProductKey.Date = claimedProductKey.Date; targetClaimedProductKey.IsRevealed = claimedProductKey.IsRevealed; targetClaimedProductKey.IsUsed = claimedProductKey.IsUsed; targetClaimedProductKey.Key = claimedProductKey.Key; targetClaimedProductKey.ListingID = claimedProductKey.ListingID; targetClaimedProductKey.UserID = claimedProductKey.UserID; targetClaimedProductKey.IsGift = claimedProductKey.IsGift; } }
public void CreateClaimedProductKey(ClaimedProductKey key, string nickname) { if (key.ListingID != 0) { key.Listing = listingRepository.GetListingByID(key.ListingID); } if (String.IsNullOrEmpty(nickname) == false) { key.AppUser = userRepository.GetAppUsers().Where(a => object.Equals(nickname.ToUpper(), a.UserName.ToUpper())).SingleOrDefault(); } if (key.AppUser == null) { return; } key.Date = DateTime.Now; userRepository.InsertClaimedProductKey(key); unitOfWork.Save(); }
public Order BuyAndRevealListing(AppUser user, int listingId, int price) { Order order = null; if (user.Balance < price) { return(order); } Listing targetListing = listingRepository.GetListingByID(listingId); if (targetListing == null) { return(order); } DateTime orderDate = new DateTime(); orderDate = DateTime.Now; List <ProductKey> keys; ProductKey key = listingRepository.GetProductKeys().Where(k => k.ListingID == listingId).FirstOrDefault(); if (key == null && targetListing.ChildListings != null) { keys = new List <ProductKey>(); foreach (Listing childListing in targetListing.ChildListings) { ProductKey temp = listingRepository.GetProductKeys().Where(k => k.ListingID == childListing.ListingID).SingleOrDefault(); if (temp != null) { keys.Add(temp); } else { return(order); } } } else { keys = new List <ProductKey>() { key }; } order = new Order(user, orderDate); userRepository.InsertOrder(order); unitOfWork.Save(); ProductOrderEntry orderEntry = new ProductOrderEntry(order, targetListing); order.AddProductOrderEntry(orderEntry); //if (targetListing.ChildListings == null || targetListing.ChildListings.Count == 0) //{ foreach (ProductKey productKey in keys) { listingRepository.DeleteProductKey(productKey.ProductKeyID); productKey.Listing.Quantity--; //productKey.Listing.UpdateParentQuantities(); listingRepository.UpdateListing(productKey.Listing); ClaimedProductKey claimedKey = new ClaimedProductKey(productKey, user, orderDate, "Purchase - Order #" + order.OrderID); user.AddClaimedProductKey(claimedKey); claimedKey.IsRevealed = true; userRepository.InsertClaimedProductKey(claimedKey); unitOfWork.Save(); orderEntry.AddClaimedProductKey(claimedKey); } userRepository.InsertProductOrderEntry(orderEntry); unitOfWork.Save(); return(order); }
public void DrawAuctionWinners(int id) { Auction auction = GetAuctionByID(id); if (auction == null) { return; } if (auction.Winners?.Count > 0 || auction.IsOpen()) { return; } List <AuctionBid> winningBids = auction.WinningBids().ToList(); if (winningBids == null) { return; } auction.Winners = new HashSet <AppUser>(); List <string> keys = auction.GetKeys().ToList(); foreach (AuctionBid bid in winningBids) { auction.Winners.Add(bid.AppUser); if (bid.AppUser.WonAuctions == null) { bid.AppUser.WonAuctions = new HashSet <Auction>(); } bid.AppUser.WonAuctions.Add(auction); string key = String.Empty; if (keys.Count > 0) { key = keys.First(); keys.Remove(key); } ClaimedProductKey userKey; BalanceEntry balanceEntry; ProductKey pKey; if (String.IsNullOrEmpty(key) == false) { pKey = new ProductKey(auction.ListingID, key); } else { pKey = new ProductKey(auction.ListingID, true); } userKey = new ClaimedProductKey(pKey, bid.AppUser, DateTime.Now, auction.Prize() + " Auction"); int cost = bid.BidAmount; if (auction.IsSilent == false) { cost = auction.PublicWinningBid(); } balanceEntry = new BalanceEntry(bid.AppUser, auction.Prize() + " Auction", 0 - cost, DateTime.Now); bid.AppUser.AddBalanceEntry(balanceEntry); bid.AppUser.AddClaimedProductKey(userKey); bid.AppUser.Balance -= cost; userRepository.UpdateAppUserSynch(bid.AppUser); } unitOfWork.Save(); }
public void DeleteClaimedProductKey(int claimedProductKeyId) { ClaimedProductKey claimedProductKey = context.ClaimedProductKeys.Find(claimedProductKeyId); context.ClaimedProductKeys.Remove(claimedProductKey); }
public void InsertClaimedProductKey(ClaimedProductKey claimedProductKey) { context.ClaimedProductKeys.Add(claimedProductKey); }
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)); }