Ejemplo n.º 1
0
        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);
        }
        public async Task <IActionResult> SetGamblers([FromBody] SetTournamentGamblersViewModel model)
        {
            var result = await _tournamentService.SetGamblers(model);

            return(Json(result));
        }