Exemplo n.º 1
0
        public Response GetItem(string gameId, string matchId)
        {
            var gameInfo = GetGameInfo(gameId);

            RequestHelper.ValidateId(matchId);

            var matchService = ServiceFactory.CreateMatchService(gameInfo);
            var match        = matchService.GetMatch(matchId);

            if (match == null)
            {
                throw ResponseHelper.Get404NotFound($"Match ID '{matchId}' not found");
            }

            var halDocument = CreateHalDocument(UriHelper.GetMatchUri(gameId, matchId), gameInfo);

            var matchMapper = new MatchMapper(UriHelper);

            var matchResource = matchMapper.Map(
                match,
                MatchMapper.HomeScore,
                MatchMapper.AwayScore,
                MatchMapper.PenaltiesTaken,
                MatchMapper.HomePenaltyScore,
                MatchMapper.AwayPenaltyScore,
                MatchMapper.Date,
                MatchMapper.Played,
                MatchMapper.Round);

            var teamMapper       = new TeamMapper(UriHelper);
            var homeTeamResource = teamMapper.Map(match.HomeTeam, TeamMapper.TeamName);
            var awayTeamResource = teamMapper.Map(match.AwayTeam, TeamMapper.TeamName);

            matchResource.AddResource("home-team", homeTeamResource);
            matchResource.AddResource("away-team", awayTeamResource);

            halDocument.AddResource("rel:match", matchResource);

            AddPlayNextMatchDayForm(gameInfo, halDocument, match.Date);

            // Add the other matches that are played on this match day. Unless there is only one match, then there's no need to add these matches.
            //var matchesPerCompetition = GetDayMatchesResources(gameInfo, match.Date, out int numberOfMatches);
            //if (numberOfMatches > 1)
            //{
            //   halDocument.AddResource("rel:matches-per-competition", matchesPerCompetition);
            //}

            var response = GetResponse(halDocument);

            return(response);
        }
Exemplo n.º 2
0
 public TeamDTO CreateTeam(TeamDTO team)
 {
     validator.ValidatePermissions();
     try
     {
         Team domainTeam = teamMapper.Map(team);
         teamRepository.Add(domainTeam);
         return(teamMapper.Map(domainTeam));
     }
     catch (DataAccessException e)
     {
         throw new ServicesException($"Failure to add team with name {team.Name}.", e);
     }
 }
Exemplo n.º 3
0
        public async Task <IEnumerable <Team> > GetAllTeams(int?competitionId = null)
        {
            var teamsFromBD = await _footballUnitOfWork.TeamRepository.GetAllTeams(competitionId);

            var teams = TeamMapper.Map(teamsFromBD);

            foreach (var team in teams)
            {
                _blobStorageService.PopulateUrlForBlob(team.PictureLogo);
            }

            return(teams);
        }
Exemplo n.º 4
0
 public IEnumerable <TeamDTO> GetFollowedTeams()
 {
     try
     {
         User recovered = userRepository.Get(loginServices.LoggedUser.UserName);
         Func <Team, TeamDTO> mapDTOs = team => teamMapper.Map(team);
         return(recovered.FollowedTeams.Select(mapDTOs));
     }
     catch (DataAccessException e)
     {
         throw new ServicesException($"Failed to get {loginServices.LoggedUser} followed teams.", e);
     }
 }
Exemplo n.º 5
0
        public async Task <Team> GetTeamByIdAndYear(int id, int year)
        {
            var teamfromBD = await _footballUnitOfWork.TeamRepository.GetTeamByIdAndYear(id);

            var team = TeamMapper.Map(teamfromBD, year);

            if (team != null)
            {
                _blobStorageService.PopulateUrlForBlob(team.PictureLogo);

                foreach (var player in team.PlayerList)
                {
                    _blobStorageService.PopulateUrlForBlob(player.Picture);
                }
            }

            return(team);
        }
Exemplo n.º 6
0
        public Response GetTeamItem(string gameId, string teamId)
        {
            var gameInfo = GetGameInfo(gameId);

            RequestHelper.ValidateId(teamId);

            var teamService = ServiceFactory.CreateTeamService(gameInfo);
            var team        = teamService.GetTeam(teamId);

            if (team == null)
            {
                throw ResponseHelper.Get404NotFound($"Team ID '{teamId}' not found");
            }

            var halDocument = CreateHalDocument(UriHelper.GetTeamUri(gameId, teamId), gameInfo);

            var teamMapper = new TeamMapper(UriHelper);

            var teamListResourceFactory = new TeamListResourceFactory(gameInfo, UriHelper, UriHelper.GetTeamUri(gameId, "###teamid###"));

            halDocument.AddResource("rel:teams", teamListResourceFactory.Create());

            var teamResource = teamMapper.Map(team, TeamMapper.TeamName, TeamMapper.Rating, TeamMapper.RatingPercentage);

            halDocument.AddResource("rel:team", teamResource);

            var seasonService = ServiceFactory.CreateSeasonService(gameInfo);
            var currentSeason = seasonService.GetCurrentSeason();

            var statisticsService            = ServiceFactory.CreateStatisticsService(gameInfo);
            var seasonTeamStatistics         = statisticsService.GetSeasonTeamStatistics(currentSeason.Id, teamId);
            var seasonTeamStatisticsResource = new SeasonTeamStatisticsMapper(UriHelper).Map(seasonTeamStatistics);

            halDocument.AddResource("rel:season-team-statistics", seasonTeamStatisticsResource);

            var teamStatistics         = statisticsService.GetTeamStatistics(teamId);
            var teamStatisticsResource = new TeamStatisticsMapper(UriHelper).Map(teamStatistics);

            halDocument.AddResource("rel:team-statistics", teamStatisticsResource);

            var response = GetResponse(halDocument);

            return(response);
        }
Exemplo n.º 7
0
        public async Task <TeamDto> RegisterTeam(TeamRegistrationDto team)
        {
            ValidateTeam(team);
            Team            teamToRegister     = _teamRegistrationMapper.Map(team);
            List <Location> availableLocations = await _dbContext.Locations.Where(x => x.Teams.Count == 0).ToListAsync();

            teamToRegister.Location = availableLocations.RandomSingle();
            if (teamToRegister.Location == null)
            {
                throw new Exception("No locations available");
            }
            teamToRegister.Score = 100;
            teamToRegister.TotalNumberOfAndroids = 1000;
            teamToRegister.Password = Crypt.HashPassword(teamToRegister.Password, 10, enhancedEntropy: true);
            _dbContext.Teams.Add(teamToRegister);
            await _dbContext.SaveChangesAsync();

            TeamDto registeredTeam = _teamMapper.Map(teamToRegister);

            registeredTeam.NumberOfAndroidsAvailable = teamToRegister.TotalNumberOfAndroids;
            return(registeredTeam);
        }
Exemplo n.º 8
0
        private bool LoadTeams(string team1Name, string team2Name)
        {
            bool result = true;

            var teams = CSVParser.ParseCsvFile <TeamEntity>(Constants.TEAMS_FILE);

            Team1 = teams.Select(x => TeamMapper.Map(x)).Where(t => t.TeamName.Equals(team1Name, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
            Team2 = teams.Select(x => TeamMapper.Map(x)).Where(t => t.TeamName.Equals(team2Name, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();

            if (Team1 == null)
            {
                Logger.Error("Failed to find team with name: " + team1Name);
                result = false;
            }

            if (Team2 == null)
            {
                Logger.Error("Failed to find team with name: " + team2Name);
                result = false;
            }

            return(result);
        }
Exemplo n.º 9
0
        public Response GetItem(string gameId)
        {
            RequestHelper.ValidateId(gameId);

            var gameInfo = GetGameInfo(gameId);

            if (gameInfo.CurrentTeam == null)
            {
                throw ResponseHelper.Get501NotImplemented("You must pick a team now, but this is not implemented yet...");
            }

            var halDocument = CreateHalDocument(UriHelper.GetGameUri(gameId), gameInfo);

            halDocument.AddLink("game-links", new Link(UriHelper.GetGameLinksUri(gameId)));

            // Add GameDateTime navigation.
            var gameDateTimeService = ServiceFactory.CreateGameDateTimeService(gameInfo);
            var now = gameDateTimeService.GetNow();
            var currentGameDateTimeResource = new GameDateTimeMapper(UriHelper).Map(now);

            halDocument.AddResource("rel:game-datetime-navigation", currentGameDateTimeResource);

            // Add my team.
            var teamResource = new TeamMapper(UriHelper).Map(gameInfo.CurrentTeam, TeamMapper.TeamName, TeamMapper.Rating, TeamMapper.RatingGoalkeeper, TeamMapper.RatingDefence, TeamMapper.RatingMidfield, TeamMapper.RatingAttack, TeamMapper.RatingPercentage);

            halDocument.AddResource("rel:my-team", teamResource);

            // Add season resource.
            var seasonService  = ServiceFactory.CreateSeasonService(gameInfo);
            var currentSeason  = seasonService.GetCurrentSeason();
            var seasonResource = new SeasonMapper(UriHelper).Map(currentSeason, SeasonMapper.SeasonShortName, SeasonMapper.SeasonLongName);

            bool endOfSeason  = seasonService.DetermineSeasonEnded(currentSeason.Id);
            bool endSeasonNow = currentSeason.EndDateTime == now.DateTime;

            if (endOfSeason && endSeasonNow)
            {
                var form = new Form("end-season")
                {
                    Action = UriHelper.GetSeasonUri(gameId, currentSeason.Id),
                    Method = "post",
                    Title  = "END SEASON!"
                };
                seasonResource.AddForm(form);
            }

            halDocument.AddResource("rel:current-season", seasonResource);

            // Add season team statistics.
            var statisticsService            = ServiceFactory.CreateStatisticsService(gameInfo);
            var seasonTeamStatistics         = statisticsService.GetSeasonTeamStatistics(currentSeason.Id, gameInfo.CurrentTeamId);
            var seasonTeamStatisticsResource = new SeasonTeamStatisticsMapper(UriHelper).Map(seasonTeamStatistics);

            halDocument.AddResource("rel:season-team-statistics", seasonTeamStatisticsResource);

            // Add next match day.
            var matchService  = ServiceFactory.CreateMatchService(gameInfo);
            var nextMatchDate = matchService.GetNextMatchDate(currentSeason.Id);

            if (nextMatchDate.HasValue)
            {
                var matchDayResourceFactory = new MatchDayResourceFactory(UriHelper, gameId, nextMatchDate.Value);

                var matchDayResource = matchDayResourceFactory.Create();

                // Add a resource for the match of the current team.
                var matchForCurrentTeam = matchService.GetByMatchDayAndTeam(nextMatchDate.Value, gameInfo.CurrentTeamId);
                if (matchForCurrentTeam != null)
                {
                    var matchResource = new MatchMapper(UriHelper).Map(
                        matchForCurrentTeam,
                        MatchMapper.CompetitionName,
                        MatchMapper.CompetitionType,
                        MatchMapper.Date,
                        MatchMapper.Round);

                    var teamMapper = new TeamMapper(UriHelper);

                    var homeTeamResource = teamMapper.Map(matchForCurrentTeam.HomeTeam, TeamMapper.TeamName, TeamMapper.LeagueName, TeamMapper.CurrentLeaguePosition);
                    matchResource.AddResource("home-team", homeTeamResource);

                    var awayTeamResource = teamMapper.Map(matchForCurrentTeam.AwayTeam, TeamMapper.TeamName, TeamMapper.LeagueName, TeamMapper.CurrentLeaguePosition);
                    matchResource.AddResource("away-team", awayTeamResource);

                    matchResource.AddResource("your-opponent", gameInfo.CurrentTeam.Equals(matchForCurrentTeam.HomeTeam) ? awayTeamResource : homeTeamResource);

                    matchDayResource.AddResource("next-match", matchResource);
                }

                // Only add the play next match day form when the matches are right now.
                if (nextMatchDate.Value == now.DateTime)
                {
                    var playNextMatchDayForm = matchDayResourceFactory.GetForm();
                    matchDayResource.AddForm(playNextMatchDayForm);
                }

                halDocument.AddResource("rel:next-match-day", matchDayResource);
            }

            // Add league table.
            var leagueTableService  = ServiceFactory.CreateLeagueTableService(gameInfo);
            var leagueTable         = leagueTableService.GetBySeasonAndCompetition(currentSeason.Id, gameInfo.CurrentTeam.CurrentLeagueCompetitionId);
            var leagueTableResource = new LeagueTableMapper(UriHelper).Map(leagueTable);

            leagueTableResource.AddLink("leaguetables", new Link(UriHelper.GetSeasonLeagueTablesUri(gameId, currentSeason.Id))
            {
                Name = "all", Title = "All league tables"
            });

            halDocument.AddResource("rel:leaguetable", leagueTableResource);

            var response = GetResponse(halDocument);

            return(response);
        }
Exemplo n.º 10
0
 public Team ToModel()
 {
     return(mapper.Map(this));
 }