Пример #1
0
        public void CreateFirstAuction(bool external = false)
        {
            DateTime now = DateTime.Now;

            Auction auction = new Auction
            {
                StartTime         = now,
                EndTime           = now.AddHours(1),
                TotalPoolCoins    = GetPoolCoins(),
                TotalSoldCoins    = 0,
                Status            = EnumTypes.AuctionStatus.Active.ToString(),
                IsManualScheduled = false
            };

            _context.Add(auction);

            if (external)
            {
                _context.SaveChangesAsync();
            }
        }
        public async Task <IActionResult> PlaceBid(BidSent newBid)
        {
            IDbContextTransaction transaction = _context.Database.BeginTransaction();

            try
            {
                var user = _context.Users.FirstOrDefault(u => u.UserId == newBid.RequestUsersId);

                newBid.BidDate = DateTime.Now;
                _context.Add(newBid);
                await _context.SaveChangesAsync();

                var lastBidReq = await _context.BidsSent.OrderByDescending(x => x.Id).FirstOrDefaultAsync();

                BidRequest bidReq = new BidRequest
                {
                    UserId          = newBid.UserId,
                    BidSentId       = lastBidReq.Id,
                    BidStatus       = newBid.BidStatus,
                    BidCoins        = newBid.BidCoins,
                    BidType         = newBid.BidCoinsType,
                    BidderName      = $"{user.Firstname} {user.Lastname}",
                    BidderCellphone = user.Cellphone,
                    RecipientName   = newBid.RecipientName,
                    BidDate         = DateTime.Now
                };

                _context.Add(bidReq);
                await _context.SaveChangesAsync();

                transaction.Commit();
            }
            catch
            {
                transaction.Rollback();
            }
            return(RedirectToAction(nameof(Dashboard)));
        }
        public async Task <IActionResult> Create(AuctionPostViewModel auctionVM)
        {
            SetSessionValues();
            if (IsLoggedOut())
            {
                return(LogOut());
            }
            if (ModelState.IsValid)
            {
                DateTime start = Convert.ToDateTime(auctionVM.Auction.StartTime);
                DateTime end   = Convert.ToDateTime(auctionVM.Auction.EndTime);

                auctionVM.Auction.TotalPoolCoins = new AuctionExecution().GetPoolCoins();

                if (end.CompareTo(start) < 0)
                {
                    auctionVM.EndTimeError = "End time cannot be earlier than start time";
                    return(View(auctionVM));
                }

                if (start.CompareTo(DateTime.Now) < 0)
                {
                    auctionVM.StartTimeError = "Choose start time later than now!";
                    return(View(auctionVM));
                }

                if (end.CompareTo(DateTime.Now) < 0)
                {
                    auctionVM.EndTimeError = "Choose End time later than now!";
                    return(View(auctionVM));
                }
                auctionVM.Auction.Status            = EnumTypes.AuctionStatus.Pending.ToString();
                auctionVM.Auction.IsManualScheduled = true;
                _context.Add(auctionVM.Auction);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(auctionVM.Auction));
        }