Exemplo n.º 1
0
        public Response GetItem(string userId)
        {
            RequestHelper.ValidateId(userId);

            // The logged in user is only allowed to request its own profile.
            //TODO Let MODDERVOKKIN op
            bool allowed = "17eqhq".Equals(userId, StringComparison.OrdinalIgnoreCase);

            if (!allowed)
            {
                throw ResponseHelper.Get404NotFound("");
            }

            var userService = ServiceFactory.CreateUserService();
            var user        = userService.GetUser(userId);

            if (user == null)
            {
                throw ResponseHelper.Get404NotFound("");
            }

            var userResource = new UserMapper(UriHelper).Map(user, UserMapper.Firstname, UserMapper.Lastname, UserMapper.Username, UserMapper.Email);

            var halDocument = CreateHalDocument(UriHelper.GetUserUri(userId));

            halDocument.AddResource("rel:user", userResource);

            var response = GetResponse(halDocument);

            return(response);
        }
Exemplo n.º 2
0
        public Response GetSeasonItem(string gameId, string seasonId)
        {
            RequestHelper.ValidateId(gameId);
            RequestHelper.ValidateId(seasonId);

            var gameInfo = GetGameInfo(gameId);

            RequestHelper.ValidateId(seasonId);

            var statisticsService = ServiceFactory.CreateStatisticsService(gameInfo);
            var seasonStatistics  = statisticsService.GetSeasonStatistics(seasonId);

            if (seasonStatistics == null)
            {
                throw ResponseHelper.Get404NotFound($"Season with ID '{seasonId}' not found");
            }

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

            var resource = new SeasonStatisticsMapper(UriHelper).Map(seasonStatistics);

            halDocument.AddResource("rel:season", resource);

            var seasonListResourceFactory = new SeasonListResourceFactory(gameInfo, UriHelper, UriHelper.GetSeasonUri(gameId, "###seasonid###"));

            halDocument.AddResource("rel:seasons", seasonListResourceFactory.Create());

            var response = GetResponse(halDocument);

            return(response);
        }
Exemplo n.º 3
0
        public Response GetTeamPlayers(string gameId, string teamId)
        {
            var game = GetGameInfo(gameId);

            RequestHelper.ValidateId(teamId);

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

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

            // Get the team's players from the database.
            var playerService = ServiceFactory.CreatePlayerService(game);
            var players       = playerService.GetByTeam(team).OrderBy(player => player.TeamOrder);

            // Map the players to player resources.
            var playerResources = players.Select(GetPlayerResource).ToList();

            var halDocument = CreateHalDocument(UriHelper.GetTeamPlayersUri(gameId, teamId), game);

            halDocument.AddResource("rel:players", playerResources);

            var teamListResourceFactory = new TeamListResourceFactory(game, UriHelper, UriHelper.GetTeamPlayersUri(gameId, "###teamid###"));

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

            var response = GetResponse(halDocument);

            return(response);
        }
Exemplo n.º 4
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.º 5
0
        protected HttpException Handle(BusinessLogicException businessLogicException)
        {
            if (businessLogicException is NotFoundException)
            {
                return(ResponseHelper.Get404NotFound(businessLogicException.Message));
            }

            if (businessLogicException is ConflictException)
            {
                return(ResponseHelper.Get409Conflict(businessLogicException.Message));
            }

            return(ResponseHelper.Get500InternalServerError("Unknown business logic exception"));
        }
Exemplo n.º 6
0
        internal GameInfo GetGameInfo(string gameId)
        {
            var gameService = ServiceFactory.CreateGameService();

            //TODO Let MODDERVOKKIN op
            string userId = "17eqhq";

            var gameInfo = gameService.GetGame(gameId, userId);

            if (gameInfo == null)
            {
                throw ResponseHelper.Get404NotFound($"Game '{gameId}' not found");
            }

            return(gameInfo);
        }
Exemplo n.º 7
0
        public Response GetTeamMatches(string gameId, string seasonId, string teamId)
        {
            var game = GetGameInfo(gameId);

            RequestHelper.ValidateId(seasonId);
            RequestHelper.ValidateId(teamId);

            // Check team exists.
            var teamService = ServiceFactory.CreateTeamService(game);
            var team        = teamService.GetTeam(teamId);

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

            var matchService = ServiceFactory.CreateMatchService(game);

            var matches = matchService.GetTeamRoundMatches(teamId, seasonId, team.CurrentLeagueCompetitionId).ToList();

            if (!matches.Any())
            {
                throw ResponseHelper.Get404NotFound($"No matches found for seasonId '{seasonId}' and teamId '{teamId}'");
            }

            var halDocument = CreateHalDocument(UriHelper.GetSeasonTeamMatchesUri(gameId, seasonId, teamId), game);

            halDocument.AddLink("rel:matches-of-team", new Link(UriHelper.GetTeamUri(gameId, teamId)));

            var resourceFactory = new TeamMatchResourceFactory(UriHelper);
            var resources       = resourceFactory.Create(matches, gameId, seasonId, teamId);

            halDocument.AddResource("rel:matches", resources);

            var teamListResourceFactory = new TeamListResourceFactory(game, UriHelper, UriHelper.GetSeasonTeamMatchesUri(gameId, seasonId, "###teamid###"));

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

            var seasonListResourceFactory = new SeasonListResourceFactory(game, UriHelper, UriHelper.GetSeasonTeamMatchesUri(gameId, "###seasonid###", teamId));

            halDocument.AddResource("rel:seasons", seasonListResourceFactory.Create());

            return(GetResponse(halDocument));
        }
Exemplo n.º 8
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.º 9
0
        public Response GetDayMatches(string gameId, string dayId)
        {
            var game = GetGameInfo(gameId);

            DateTime matchDay = ValidateAndParseMatchDay(dayId);

            var halDocument    = CreateHalDocument(UriHelper.GetMatchDayUri(gameId, dayId), game);
            var matchResources = GetDayMatchesResources(game, matchDay, out int _).ToList();

            if (!matchResources.Any())
            {
                throw ResponseHelper.Get404NotFound($"No matches found for match day '{dayId}'");
            }

            halDocument.AddResource("rel:matches-per-competition", matchResources);

            AddPlayNextMatchDayForm(game, halDocument, matchDay);

            return(GetResponse(halDocument));
        }
Exemplo n.º 10
0
        public Response PostEndSeasonItem(string gameId, string seasonId)
        {
            var game = GetGameInfo(gameId);

            RequestHelper.ValidateId(seasonId);

            var seasonService = ServiceFactory.CreateSeasonService(game);

            var season = seasonService.Get(seasonId);

            if (season == null)
            {
                throw ResponseHelper.Get404NotFound($"Season '{seasonId}' does not exist");
            }

            var currentSeason = seasonService.GetCurrentSeason();

            if (!currentSeason.Equals(season))
            {
                throw ResponseHelper.Get409Conflict("This is not the current season");
            }

            bool seasonEnded = seasonService.DetermineSeasonEnded(season.Id);

            if (!seasonEnded)
            {
                throw ResponseHelper.Get400BadRequest("The season is not finished yet");
            }

            seasonService.EndSeasonAndCreateNext(seasonId);

            var seasonUri = UriHelper.GetSeasonUri(gameId, seasonId);

            var response = new Response(HttpStatusCode.Created);

            response.Headers.Add("Location", seasonUri);

            return(response);
        }
Exemplo n.º 11
0
        public Response Post()
        {
            const string invalidRequestBodyError = "Invalid request body";

            LoginResource loginResource;

            try
            {
                loginResource = Request.MessageBody.DeserializeJson <LoginResource>();
            }
            catch (Exception)
            {
                throw ResponseHelper.Get400BadRequest(invalidRequestBodyError);
            }

            var userService = ServiceFactory.CreateUserService();

            User user;

            try
            {
                user = userService.GetUser(loginResource.Username, loginResource.Password);
            }
            catch (ValidationException validationException)
            {
                throw ResponseHelper.Get400BadRequest(validationException.Message);
            }

            if (user == null)
            {
                throw ResponseHelper.Get404NotFound("Username/password combination not found");
            }

            var response = new Response(HttpStatusCode.Ok);

            return(response);
        }