Esempio n. 1
0
        public async Task <ResponseDto <BaseModelDto> > EditLeagueAsync(int leagueId, EditLeagueBindingModel model)
        {
            var response = new ResponseDto <BaseModelDto>();

            var league = _leagueRepository.GetById(leagueId);

            if (league == null)
            {
                response.Errors.Add(ServiceErrors.LEAGUE_DOESNT_EXIST);
                return(response);
            }
            league.Name = model.Name;
            await _leagueRepository.EditAsync(league);

            return(response);
        }
Esempio n. 2
0
        public async Task <ActionResult <League> > GetByID(int id)
        {
            var response = await _leagueRepo.GetById(id);

            if (response == null)
            {
                return(NotFound("No results found for league with the id of " + id));
            }
            ;

            return(Ok(response));
        }
Esempio n. 3
0
        public ResponseDto <LeagueRankingDto> GetLeagueStatistics(int leagueId)
        {
            var response = new ResponseDto <LeagueRankingDto>
            {
                Object = new LeagueRankingDto()
            };

            var league = _leagueRepository.GetById(leagueId);

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

            var players = new List <PlayerDtoLessDetails>();

            league.Teams.ToList().ForEach(team =>
                                          team.Players.ToList().ForEach(player =>
                                                                        players.Add(new PlayerDtoLessDetails
            {
                BirthDate = player.BirthDate,
                Firstname = player.Firstname,
                Id        = player.Id,
                Lastname  = player.Lastname,
                TeamName  = team.Name
            }
                                                                                    )
                                                                        )
                                          );

            players.ForEach(player =>
            {
                response.Object.Players.Add(
                    new PlayerStatisticsDto
                {
                    Player          = player,
                    Goals           = CountStatistics(player.Id)[Models.Enums.Action.Goal],
                    Assists         = CountStatistics(player.Id)[Models.Enums.Action.Assist],
                    YellowCards     = CountStatistics(player.Id)[Models.Enums.Action.YellowCard],
                    RedCards        = CountStatistics(player.Id)[Models.Enums.Action.RedCard],
                    MissedPenalties = CountStatistics(player.Id)[Models.Enums.Action.MissedPenalty]
                }
                    );
            });

            return(response);
        }
        public async Task <ResponseDto <BaseModelDto> > InsertTeamAsync(int leagueId, AddTeamBindingModel model)
        {
            var response = new ResponseDto <BaseModelDto>();

            var league = _leagueRepository.GetById(leagueId);

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

            var teamsQuantity = league.Teams.Count;

            if (teamsQuantity >= league.Quantity)
            {
                response.Errors.Add(ServiceErrors.TEAM_THER_IS_ENOUGH_TEAMS_IN_THAT_LEAGUE);
                return(response);
            }

            var teams  = _teamRepository.GetAll().ToList();
            var exists = teams.Exists(t => t.Name == model.Name);

            if (exists)
            {
                response.Errors.Add(ServiceErrors.TEAM_WITH_THAT_NAME_ALREADY_EXISTS);
                return(response);
            }

            var team = new Team
            {
                League = league,
                Name   = model.Name
            };

            await _teamRepository.InsertAsync(team);

            return(response);
        }
        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);
        }