Example #1
0
        public static void ValidateId(string id)
        {
            // As long as the ID is not empty it's OK.
            bool isValid = !string.IsNullOrWhiteSpace(id);

            if (!isValid)
            {
                throw ResponseHelper.Get400BadRequest($"The request parameter '{id}' is not a valid ID");
            }
        }
Example #2
0
        private static DateTime ValidateAndParseMatchDay(string dayId)
        {
            bool parseSuccessful = DateTime.TryParseExact(dayId, "yyyyMMddHH", new CultureInfo("en-US"), DateTimeStyles.None, out var matchDay);

            if (!parseSuccessful)
            {
                throw ResponseHelper.Get400BadRequest($"Invalid 'days' querystring argument: {dayId}");
            }

            return(matchDay);
        }
Example #3
0
        public Response PostAddUser()
        {
            const string invalidRequestBodyError = "Invalid request body";

            // Check the body is not empty.
            //TODO Klopt deze check en/of is deze verstandig op deze manier?
            if (Request?.MessageBody == null || Request.MessageBody.Length == 0)
            {
                throw ResponseHelper.Get400BadRequest(invalidRequestBodyError);
            }

            // Try to deserialize the body to an AddUserResource.
            AddUserResource addUserResource;

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

            var userService = ServiceFactory.CreateUserService();

            User user;

            try
            {
                user = userService.CreateUser(addUserResource.Firstname, addUserResource.Lastname, addUserResource.Username, addUserResource.Email, addUserResource.Password);
            }
            catch (ConflictException conflictException)
            {
                throw ResponseHelper.Get409Conflict(conflictException.Message);
            }
            catch (ValidationException validationException)
            {
                throw ResponseHelper.Get400BadRequest(validationException.Message);
            }

            var response = new Response(HttpStatusCode.Created);

            response.Headers.Add("Location", UriHelper.GetUserUri(user.Id));

            return(response);
        }
Example #4
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);
        }
Example #5
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);
        }