public async Task <Match> Create(AddMatchViewModel model) { var tournament = await _dbContext.Tournaments.Include(t => t.Gamblers).FirstOrDefaultAsync(t => t.Rounds.Any(r => r.Id == model.RoundId)); var match = await _dbContext.Matches.FirstOrDefaultAsync(t => t.TeamAName.Equals(model.TeamAName, StringComparison.InvariantCultureIgnoreCase) && t.TeamBName.Equals(model.TeamBName, StringComparison.InvariantCultureIgnoreCase) && t.RoundId == model.RoundId); if (match == null) { match = new Match { TeamAName = model.TeamAName, TeamBName = model.TeamBName, TeamAScore = model.TeamAScore, TeamBScore = model.TeamBScore, RoundId = model.RoundId }; await _dbContext.Matches.AddAsync(match); await _dbContext.SaveChangesAsync(); var activeGamblers = tournament.Gamblers.Where(g => g.IsActive); foreach (var gambler in activeGamblers) { await _matchBetService.AddOrUpdateGamblerMatchBets(tournament.Id, gambler.GamblerId); } } return(match); }
public async Task <bool> SetGamblers(SetTournamentGamblersViewModel model) { var tournamentGamblers = await _dbContext.TournamentGamblers.Where(t => t.TournamentId == model.TournamentId).ToListAsync(); var gamblersToActivate = tournamentGamblers.Where(g => model.GamblersIds.Contains(g.GamblerId) && !g.IsActive).ToList(); foreach (var gambler in gamblersToActivate) { gambler.IsActive = true; _dbContext.Entry(gambler).State = EntityState.Modified; await _matchBetService.AddOrUpdateGamblerMatchBets(model.TournamentId, gambler.GamblerId); } var gamblersToDeactivate = tournamentGamblers.Where(g => !model.GamblersIds.Contains(g.GamblerId)).ToList(); foreach (var gambler in gamblersToDeactivate) { gambler.IsActive = false; _dbContext.Entry(gambler).State = EntityState.Modified; } var gamblersIdsToAdd = model.GamblersIds.Except(tournamentGamblers.Select(g => g.GamblerId)); foreach (var gamblerId in gamblersIdsToAdd) { var tournamentGambler = new TournamentGambler { GamblerId = gamblerId, TournamentId = model.TournamentId }; _dbContext.TournamentGamblers.Add(tournamentGambler); await _matchBetService.AddOrUpdateGamblerMatchBets(model.TournamentId, gamblerId); } await _dbContext.SaveChangesAsync(); return(true); }