public void MatureCoins()
        {
            var users = _context.Users.ToList();

            foreach (var usr in users)
            {
                var coins = _context.Coins.Where(c => c.UserId == usr.UserId && c.MaturityStatus == EnumTypes.CoinsMaturityStatus.Pending.ToString());
                foreach (var c in coins)
                {
                    MatureCoinForId(c.Id, true);
                }
            }
            _context.SaveChangesAsync();
        }
        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));
        }
Exemple #3
0
        public async void Run()
        {
            var auction = _context.Auctions.OrderByDescending(x => x.Id).FirstOrDefault();

            if (auction != null)
            {
                var hour = DateTime.Now.Hour;
                if (auction.Status == EnumTypes.AuctionStatus.Pending.ToString())
                {
                    if ((auction.IsManualScheduled && auction.StartTime.Hour == hour) || hour == 9 || hour == 17)
                    {
                        ActivateAuction(auction);
                    }
                }
                else if (auction.Status == EnumTypes.AuctionStatus.Active.ToString())
                {
                    if ((auction.IsManualScheduled && auction.EndTime.Hour == hour) || hour == 10 || hour == 18)
                    {
                        TerminateAuction(auction);
                    }
                }
                else if (auction.Status == EnumTypes.AuctionStatus.Stopped.ToString() || auction.Status == EnumTypes.AuctionStatus.Completed.ToString())
                {
                    if (hour == 9 || hour == 17)
                    {
                        CreateFirstAuction();
                    }
                }
            }
            else
            {
                var now = DateTime.Now.Hour;
                if (now == 9 || now == 17)
                {
                    await StartAuction();
                }
            }
            await _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)));
        }
        private void MatureCoins()
        {
            var users = _context.Users.ToList();

            foreach (var usr in users)
            {
                var coins = _context.Coins.Where(c => c.UserId == usr.UserId && c.MaturityStatus == EnumTypes.CoinsMaturityStatus.Pending.ToString());
                foreach (var c in coins)
                {
                    if (true)//DateTime.Now.CompareTo(c.MaturityDate) >= 0)
                    {
                        c.MaturityStatus = EnumTypes.CoinsMaturityStatus.Matured.ToString();

                        var wallet = _context.Wallets.FirstOrDefault(u => u.UserId == c.UserId);
                        wallet.TotalCoins += c.TotalCoins;

                        _context.Update(wallet);
                        _context.Update(c);
                    }
                }
            }
            _context.SaveChangesAsync();
        }
        public async Task <IActionResult> Create(UserViewModel userVM)
        {
            SetSessionValues();

            if (ModelState.IsValid)
            {
                var loadByUsername = await _context.Users.FirstOrDefaultAsync(u => u.Username == userVM.User.Username);

                if (loadByUsername != null)
                {
                    userVM.UsernameError = "This username already exist!";
                    return(View(userVM));
                }

                var loadByEmail = await _context.Users.FirstOrDefaultAsync(u => u.Email == userVM.User.Email);

                if (loadByEmail != null)
                {
                    userVM.EmailError = "This email already exist!";
                    return(View(userVM));
                }

                if (_context.Users.Count() == 0)
                {
                    userVM.User.IsAdmin = true;
                }
                else
                {
                    userVM.User.IsAdmin = false;
                }

                _context.Users.Add(userVM.User);
                await _context.SaveChangesAsync();

                User user = await _context.Users.FirstOrDefaultAsync(u => u.Username == userVM.User.Username);



                userVM.Bank.UserId = user.UserId;
                Wallet wallet = new Wallet {
                    UserId = user.UserId, TotalCoins = 0
                };

                _context.Banks.Add(userVM.Bank);
                _context.Wallets.Add(wallet);
                await _context.SaveChangesAsync();

                if (string.IsNullOrEmpty((string)ViewData["role"]))
                {
                    HttpContext.Session.SetString("userId", user.UserId.ToString());
                    HttpContext.Session.SetString("role", user.IsAdmin ? EnumTypes.Role.Admin.ToString() : EnumTypes.Role.User.ToString());

                    return(RedirectToAction("Dashboard", "Dashboard"));
                }
                else if ((string)ViewData["role"] == EnumTypes.Role.Admin.ToString())
                {
                    return(RedirectToAction("Index", "Users"));
                }
            }
            return(View());
        }