public async Task <IActionResult> InsertMatch(int leagueId, AddMatchBindingModel model)
        {
            var result = await _matchService.InsertMatchAsync(leagueId, model);

            if (result.ErrorOccurred)
            {
                return(BadRequest(result));
            }

            return(Ok(result));
        }
        public async Task <ResponseDto <BaseModelDto> > InsertMatchAsync(int leagueId, AddMatchBindingModel model)
        {
            var response = new ResponseDto <BaseModelDto>();

            if (model.HostId == model.AwayId)
            {
                response.Errors.Add(ServiceErrors.MATCH_TEAMS_IDS_CANT_BE_EQUAL);
                return(response);
            }

            var league = _leagueRepository.GetById(leagueId);

            if (league == null)
            {
                response.Errors.Add(ServiceErrors.LEAGUE_DOESNT_EXIST);
                return(response);
            }

            var maxRound = int.MaxValue;

            if (league.Quantity % 2 == 0)
            {
                maxRound = (league.Quantity - 1) * 2;
            }
            else
            {
                maxRound = league.Quantity * 2;
            }

            if (model.Round > maxRound)
            {
                response.Errors.Add(ServiceErrors.MATCH_GIVEN_ROUND_IS_GREATER_THAN_MAX_ROUND);
                return(response);
            }

            var teams = _teamRepository.GetTeamsFromLeague(leagueId).ToList();

            if (teams == null)
            {
                response.Errors.Add(ServiceErrors.LEAGUE_DOESNT_EXIST);
                return(response);
            }

            var HostIsInLeague = teams.Exists(t => t.Id == model.HostId);
            var AwayIsInLeague = teams.Exists(t => t.Id == model.AwayId);

            if (!(HostIsInLeague && AwayIsInLeague))
            {
                response.Errors.Add(ServiceErrors.MATCH_TEAMS_ARENT_IN_THE_SAME_LEAGUE);
                return(response);
            }

            var host = _teamRepository.GetById(model.HostId);

            if (host == null)
            {
                response.Errors.Add(ServiceErrors.TEAM_DOESNT_EXIST);
                return(response);
            }

            var away = _teamRepository.GetById(model.AwayId);

            if (away == null)
            {
                response.Errors.Add(ServiceErrors.TEAM_DOESNT_EXIST);
                return(response);
            }

            var matches = _matchRepository.GetAll().Where(l => l.League.Id == leagueId);

            if (matches != null)
            {
                var hostMatches = matches.Where(m => m.Host.Id == model.HostId || m.Away.Id == model.HostId);
                var awayMatches = matches.Where(m => m.Host.Id == model.AwayId || m.Away.Id == model.AwayId);

                var hostHasMatchInGivenRound = hostMatches.Any(m => m.Round == model.Round);
                var awayHasMatchInGivenRound = awayMatches.Any(m => m.Round == model.Round);

                if (hostHasMatchInGivenRound || awayHasMatchInGivenRound)
                {
                    response.Errors.Add(ServiceErrors.MATCH_ONE_OF_TEAMS_ALREADY_PLAYS_MATCH_IN_GIVEN_ROUND);
                    return(response);
                }
            }

            var matchPlayers = new List <MatchPlayer>();

            var players = host.Players.Concat(away.Players).ToList();

            players.ForEach(player =>
            {
                var matchPlayer = new MatchPlayer
                {
                    PlayerId      = player.Id,
                    Player        = player,
                    EntryMinute   = 0,
                    DescentMinute = 90
                };
                matchPlayers.Add(matchPlayer);
            }
                            );

            var match = new Match
            {
                Away         = away,
                Host         = host,
                AwayScore    = model.AwayScore,
                HostScore    = model.HostScore,
                Date         = model.Date,
                League       = league,
                Round        = model.Round,
                MatchPlayers = matchPlayers
            };

            await _matchRepository.InsertAsync(match);

            var tasks = matchPlayers.ToList().Select(player => _matchPlayerRepository.InsertAsync(player));

            //matchPlayers.ForEach(async player => await _matchPlayerRepository.InsertAsync(player));

            return(response);
        }