Example #1
0
        public async Task <HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = "Comment")]
            HttpRequest req, [ServiceBus("pendingqueue", EntityType.Queue, Connection = "ServiceBusConnection")]
            ICollector <string> queueCollector,
            ILogger log)
        {
            var messageBody = await req.ReadAsStringAsync();

            if (messageBody != null && messageBody.Trim().Length > 0)
            {
                try
                {
                    log.LogInformation(
                        $"uploaded message starting {(messageBody.Length > 100 ? messageBody.Substring(0, 100) : messageBody)}");
                    var message = await _commentDataStoreService.CreateAndStoreMessage(messageBody);

                    queueCollector.Add(message.UniqueID.ToString());

                    return(new HttpResponseMessage
                    {
                        StatusCode = HttpStatusCode.Accepted,
                        Content = new StringContent(JsonSerializer.Serialize(message, _serializerOptions), Encoding.UTF8,
                                                    AppConstants.JsonResponse)
                    });
                }
                catch (Exception ex)
                {
                    log.LogError("An error has occured logging the request.", ex);
                    return(new HttpResponseMessage
                    {
                        StatusCode = HttpStatusCode.InternalServerError,
                        Content = new StringContent("Sorry - an internal error has occured", Encoding.UTF8,
                                                    AppConstants.TextResponse)
                    });
                }
            }
            else
            {
                log.LogInformation("Request with no comment received");
                return(new HttpResponseMessage
                {
                    StatusCode = HttpStatusCode.BadRequest,
                    Content = new StringContent("Please ensure the post message contains the comment to rate", Encoding.UTF8,
                                                AppConstants.TextResponse)
                });
            }
        }