예제 #1
0
        public static async Task <IActionResult> RegisterPushNotifications(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = "/api/player/{playerId}/push")] HttpRequest req,
            string playerId,
            ILogger log)
        {
            if (!Validation.TryValidateGuid(playerId, out Guid playerGuidId))
            {
                log.LogError($"Player ID ({playerId}) was not a valid GUID.");
                return(new BadRequestObjectResult("Invalid Player ID."));
            }

            PushRegistrationRequest pushRegistration = JsonConvert.DeserializeObject <PushRegistrationRequest>(await req.ReadAsStringAsync());

            bool success = await PushNotifications.Register(playerGuidId,
                                                            pushRegistration.PushPlatform,
                                                            pushRegistration.PnsToken,
                                                            pushRegistration.PlatformPushTemplate,
                                                            log);

            if (!success)
            {
                return(new BadRequestResult());
            }

            return(new CreatedResult("", ""));
        }
예제 #2
0
        public static async Task <IActionResult> AddQuestion(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = "/api/player/{playerId}/questions")] HttpRequest req,
            string playerId,
            ILogger log)
        {
            if (req.Headers.ContentLength == 0 || req.Headers?.ContentLength == null)
            {
                log.LogError($"{DateTime.UtcNow}: Request body was empty.");
                return(new BadRequestObjectResult("Request body cannot be empty."));
            }

            if (!Validation.TryValidateGuid(playerId, out Guid playerGuidId))
            {
                log.LogError($"Player ID ({playerId}) was not a valid GUID.");
                return(new BadRequestObjectResult("Invalid Player ID."));
            }

            var dbClient = Database.GetDatabaseClient(log);

            log.LogInformation($"{DateTime.UtcNow}: AddQuestion called for player with ID {playerGuidId}");

            Question question = JsonConvert.DeserializeObject <Question>(await req.ReadAsStringAsync());

            log.LogInformation($"{DateTime.UtcNow}: Adding question with text {question.Text} to player with ID {playerGuidId}");

            Question addedQuestion = await dbClient.AddQuestionToPlayer(playerGuidId, question);

            if (addedQuestion == null)
            {
                return(new BadRequestResult());
            }

            string addedQuestionJson = JsonConvert.SerializeObject(addedQuestion);
            string questionBody      = question.Text;

            if (questionBody.Length > MaxBodyLength)
            {
                questionBody = $"{questionBody.Substring(0, MaxBodyLength)}...";
            }
            await PushNotifications.SendPushNotification(addedQuestion.SequenceNumber, playerGuidId, question.Title, questionBody, addedQuestionJson, log);

            return(new CreatedResult("", addedQuestion));
        }