public async Task DeleteUser(string id) { foreach (BetSlip item in _context.BetSlip.Where(b => b.User.Id == id)) { _context.Remove(item); } foreach (UserTransaction item in _context.UserTransaction.Where(b => b.UserId == id)) { _context.Remove(item); } foreach (UserBetMatch item in _context.UserBetMatch.Where(b => b.UserBet.User.Id == id)) { _context.Remove(item); } foreach (UserBet item in _context.UserBet.Where(b => b.User.Id == id)) { _context.Remove(item); } }
public async Task <IActionResult> UserBet(string stake, string TotalOdd, string submit) { var topMatchValue = await GetTopMatchValue(); var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value; var user = await _context.Users.FirstOrDefaultAsync(u => u.Id == userId); var wallet = await _context.Wallet.Where(x => x.User == user).FirstOrDefaultAsync(); TempData["betmsg"] = null; if (stake == null && submit != "Remove") { TempData["betmsg"] = "Enter the amount"; return(RedirectToAction("Index", "Home")); } if (submit == "Remove") { foreach (BetSlip item in _context.BetSlip.Where(b => b.User.Id == userId)) { _context.Remove(item); } await _context.SaveChangesAsync(); return(RedirectToAction("Index", "Home")); } var betStake = decimal.Parse(stake, new NumberFormatInfo() { NumberDecimalSeparator = "." }); bool tempTopStatus = false; int counter = 0; int counterOdd = 0; foreach (BetSlip item in _context.BetSlip.Include(m => m.Match) .Include(h => h.Match.HomeTeam) .Include(a => a.Match.AwayTeam) .Where(b => b.User.Id == userId)) { if (item.Match.Time < DateTime.Now) { TempData["betmsg"] = $"{item.Match.HomeTeam.Name} - {item.Match.AwayTeam.Name} is started already"; return(RedirectToAction("Index", "Home")); } if (item.TopMatch == true) { tempTopStatus = true; } if (item.Odd > 1.10m) { counterOdd++; } counter++; } if (counter == 0) { TempData["betmsg"] = "0 pair on ticket"; return(RedirectToAction("Index", "Home")); } if ((counterOdd <= topMatchValue || counter <= topMatchValue) && tempTopStatus == true) { TempData["betmsg"] = "The transaction is not successful"; return(RedirectToAction("Index", "Home")); } UserTransaction transaction = new UserTransaction(); List <UserTransaction> listTransactions = new List <UserTransaction>(); if (((wallet.Saldo - betStake) >= 0 && betStake >= 1)) { wallet.Saldo -= betStake; transaction.UserId = wallet.User.Id; transaction.Payment = stake; transaction.Transactions = "Uplata listica u iznosu od " + stake + " kn " + " " + DateTime.Now.ToString(); TempData["betmsg"] = "The transaction is successful"; listTransactions.Add(transaction); wallet.Transactions = listTransactions; await _context.SaveChangesAsync(); UserBet userBet = new UserBet(); decimal totOdd = decimal.Parse(TotalOdd); userBet.TimeStamp = DateTime.Now; userBet.BetAmount = betStake; userBet.CashOut = (betStake / 100 * 95) * totOdd; userBet.TotalOdd = totOdd; userBet.Win = "Pending"; userBet.User = user; List <UserBetMatch> listBetMatches = new List <UserBetMatch>(); foreach (BetSlip item in _context.BetSlip.Include(m => m.Match).ThenInclude(h => h.HomeTeam).Include(m => m.Match).ThenInclude(a => a.AwayTeam).Where(b => b.User.Id == userId)) { UserBetMatch temp = new UserBetMatch(); //var football = _context.Match.Include(h=>h.HomeTeam).Include(a=>a.AwayTeam).SingleOrDefault(q => q.Id == item.MatchId); temp.Match = item.Match; temp.Odd = item.Odd; temp.Type = item.Type; temp.Win = "Pending"; listBetMatches.Add(temp); } userBet.BetMatches = listBetMatches; await _context.UserBet.AddAsync(userBet); await _context.UserBet.AddAsync(userBet); await _context.SaveChangesAsync(); } else { TempData["betmsg"] = "The transaction is not successful"; return(RedirectToAction("Index", "Home")); } return(RedirectToAction("Index")); }