Beispiel #1
0
        public async Task <ActionResult> BidNow(Guid?id, byte[] version)
        {
            log.Info("Action /Auction/BidNow has been fired.");


            using (var trans = db.Database.BeginTransaction(IsolationLevel.Serializable))
            {
                try
                {
                    if (id == null || version == null)
                    {
                        throw new Exception();
                    }
                    Auction auction = db.Auction.Find(id);
                    if (auction == null)
                    {
                        throw new Exception();
                    }
                    if (auction.State.Equals("READY") || auction.State.Equals("COMPLETED"))
                    {
                        throw new Exception();
                    }
                    User   lastUser   = auction.Winner;
                    string idLastUser = auction.IdWinner;
                    string idUser     = User.Identity.GetUserId();
                    User   user       = db.User.Find(idUser);
                    if (!checkNumberOfTokens(auction, user))
                    {
                        throw new Exception();
                    }
                    returnTokens(auction);
                    updateAuctionAndBid(auction);
                    reserveTokens(auction);
                    db.Entry(auction).OriginalValues["AuctionRowVersion"] = version;

                    await db.SaveChangesAsync();

                    trans.Commit();
                    var hubContext = GlobalHost.ConnectionManager.GetHubContext <AuctionHub>();
                    hubContext.Clients.All.refresh(auction.IdAuction, auction.CurrentPrice, db.User.Find(idUser).Email);
                    hubContext.Clients.All.bidding(db.User.Find(idUser).Email);
                    string v = @Convert.ToBase64String(auction.AuctionRowVersion);
                    hubContext.Clients.All.newversion(auction.IdAuction, v);
                    var hubC = GlobalHost.ConnectionManager.GetHubContext <TokenHub>();
                    if (lastUser == null)
                    {
                        hubC.Clients.All.tokens(null, null, user.Id, user.NumberOfTokens);
                    }
                    else
                    {
                        int number = db.User.Find(idLastUser).NumberOfTokens;
                        hubC.Clients.All.tokens(idLastUser, number, user.Id, user.NumberOfTokens);
                    }

                    return(RedirectToAction("Index"));
                } catch (Exception ex)
                {
                    trans.Rollback();
                    log.Error("Bid is not successful!");
                    return(RedirectToAction("Index"));
                }
            }
        }