public ActionResult Open(string GUID) { if (!Auth.Check() || !Auth.User.IsAdmin()) { return(RedirectToLogin()); } // admin wants to open auction var existingAuction = this.db.Auctions.Find(GUID); if (null == existingAuction) { return(HttpNotFound()); } string error = "Invalid input"; if (existingAuction.StatusText != "READY") { error = "You can only open ready auctions"; } else { existingAuction.Status = 1; existingAuction.CurrentPrice = existingAuction.StartingPrice; existingAuction.DateOpened = DateTime.UtcNow; existingAuction.DateClosed = DateTime.UtcNow.AddSeconds((double)existingAuction.Duration); db.Entry(existingAuction).State = EntityState.Modified; db.SaveChanges(); // TODO: notify all clients Flash.SuccessMessage("Auction opened successfully"); return(RedirectToAction("Index")); } Flash.ErrorMessage(error); return(RedirectToAction("Index")); }
private ActionResult RedirectToLogin(string msg = "You must be logged in to access this page") { Flash.ErrorMessage(msg); return(RedirectToAction("Login", "Users")); }
public ActionResult Bid(string GUID, decimal tokensOffered, string OnDetailsPage = null) { if (!Auth.Check()) { return(RedirectToLogin()); } string error = "Failed to place bid"; // first refresh Auth user //Auth.RefreshUser(); // modify tokens offered based on currency tokensOffered = (tokensOffered / (decimal)Utils.TokenValue); using (var transaction = db.Database.BeginTransaction(IsolationLevel.Serializable)) { try { var existingAuction = this.db.Auctions.Find(GUID); if (existingAuction != null && existingAuction.IsOpened() && existingAuction.CurrentPrice.Value < tokensOffered) { decimal numRemainingTokens = Auth.User.NumTokens.Value - tokensOffered; var lastBidder = GetLastBidder(existingAuction); if (lastBidder != null && Auth.Id == lastBidder.Id) { numRemainingTokens += existingAuction.CurrentPrice.Value; } if (numRemainingTokens >= 0) { // insert new bid Bid bid = new Bid(); bid.AuctionID = existingAuction.GUID; bid.UserID = Auth.Id; bid.UserName = Auth.User.DisplayName; bid.DateOfBidding = DateTime.UtcNow; bid.TokensOffered = tokensOffered; db.Bids.Add(bid); // update num tokens for last bidder if (lastBidder != null) { lastBidder.NumTokens += existingAuction.CurrentPrice; db.Entry(lastBidder).State = EntityState.Modified; } // we must modify the same instance var user = Auth.User; if (lastBidder != null && lastBidder.Id == user.Id) { user = lastBidder; } // update amount of tokens for new bidder user.NumTokens = numRemainingTokens; db.Entry(user).State = EntityState.Modified; // update current price of auction existingAuction.CurrentPrice = tokensOffered; db.Entry(existingAuction).State = EntityState.Modified; db.SaveChanges(); transaction.Commit(); // update all clients Hubs.AuctionHub.UpdateClientAuctions(existingAuction.GUID, tokensOffered, user.DisplayName, user.NumTokens.Value, lastBidder != null ? lastBidder.DisplayName : "", lastBidder != null ? lastBidder.NumTokens.Value : 0); Flash.SuccessMessage("Bid placed successfully"); return(RedirectAfterBid(OnDetailsPage, GUID)); } else { error = "You don't have enough tokens"; } } Flash.ErrorMessage(error); return(RedirectAfterBid(OnDetailsPage, GUID)); } catch (Exception) { transaction.Rollback(); Flash.ErrorMessage("Transaction error"); return(RedirectAfterBid(OnDetailsPage, GUID)); } } }