public async Task <IActionResult> UpdateScore(Guid matchId, [FromBody] UpdateTeamMatch model) { var loggedUser = LoggedInUser.Current?.Id; if (loggedUser.HasValue) { var user = await _userService.GetAsync(LoggedInUser.Current.Id) .ConfigureAwait(true); _matchService.UpdateScoreTeamMatch(user.Id, matchId, model); return(Ok()); } return(BadRequest()); }
public void UpdateScoreTeamMatch(Guid userId, Guid matchId, UpdateTeamMatch model) { CheckExist(matchId); CheckIsAdmin(userId); var match = _matchRepository.Include(x => x.MatchTeams, x => x.PriceConfig).Single(x => x.Id == matchId); CheckTimeUpdateScore(match.TimeMatch); var firstTeam = match.MatchTeams.First(); var secondTeam = match.MatchTeams.Skip(1).First(); firstTeam.Rate = model.FirstTeamRate; firstTeam.Goals = model.FirstTeamGoal; secondTeam.Goals = model.SecondTeamGoal; secondTeam.Rate = model.SecondTeamRate; _matchTeamRepository.Update(firstTeam, x => x.Goals, x => x.Rate); _matchTeamRepository.Update(secondTeam, x => x.Goals, x => x.Rate); var checkWin = Guid.NewGuid(); var temp = Guid.NewGuid(); var check = ((firstTeam.Goals + firstTeam.Rate) - (secondTeam.Goals + secondTeam.Rate)); if (check > 0) { checkWin = firstTeam.TeamId; } else { if (check < 0) { checkWin = secondTeam.TeamId; } else { checkWin = temp; } } var listUser = _userRepository.Include(x => x.Amout, x => x.UserBets).Where(x => x.DeletedTime == null).ToList(); foreach (var user in listUser) { var a = 0; if (DateTime.Compare(user.CreatedTime.DateTime, match.TimeMatch) > 0) { a = 1; } if (a != 1) { foreach (var userBet in user.UserBets) { if (userBet.MatchId == match.Id) { var amount = _amoutRepository.Get(x => x.UserId == userBet.UserId).Single(); userBet.MoneyLost = 0; amount.Total -= userBet.MoneyLostLast; if (userBet.TeamWinId == checkWin) { userBet.MoneyLost = 0; userBet.MoneyLostLast = 0; _betRepository.Update(userBet, x => x.MoneyLost, x => x.MoneyLostLast); } else { if (userBet.TeamWinId != Guid.Empty && checkWin == temp) { userBet.MoneyLost = match.PriceConfig.Price * 50 / 100; userBet.MoneyLostLast = match.PriceConfig.Price * 50 / 100; _betRepository.Update(userBet, x => x.MoneyLost, x => x.MoneyLostLast); } else { userBet.MoneyLost = match.PriceConfig.Price; userBet.MoneyLostLast = match.PriceConfig.Price; _betRepository.Update(userBet, x => x.MoneyLost, x => x.MoneyLostLast); } } amount.Total += userBet.MoneyLost; _amoutRepository.Update(amount, x => x.Total); a = 1; break; } } } if (a == 0) { var betEntity = new UserBetEntity() { UserId = user.Id, MatchId = match.Id, MoneyLost = match.PriceConfig.Price, MoneyLostLast = match.PriceConfig.Price, IsUpdated = 1 }; var amount = _amoutRepository.Get(x => x.UserId == user.Id).Single(); amount.Total += match.PriceConfig.Price; _amoutRepository.Update(amount, x => x.Total); _betRepository.Add(betEntity); _unitOfWork.SaveChanges(); } } match.IsUpdated = 1; _matchRepository.Update(match, x => x.IsUpdated); _unitOfWork.SaveChanges(); // UpdateTotalAmoutAll(userId); }