public async Task <JsonResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("OrderDetail function called.");

            string orderNumber = req.Query["orderNumber"];

            if (string.IsNullOrEmpty(orderNumber))
            {
                var result = new JsonResult("OrderDetail must be called with an order number", AdventureWorksSerializerSettings.Get());
                result.StatusCode = StatusCodes.Status400BadRequest;
                return(result);
            }

            log.LogInformation($"OrderDetail looking for order {orderNumber}");

            var order = await _repository.GetOrderFromOrderNumberAsync(orderNumber);

            if (order == null)
            {
                var result = new JsonResult($"Could not find order with order number {orderNumber}", AdventureWorksSerializerSettings.Get());
                result.StatusCode = StatusCodes.Status404NotFound;
                return(result);
            }

            return(new JsonResult(order, AdventureWorksSerializerSettings.Get()));
        }
        public async Task <JsonResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("OrderFromEmail function called.");

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();

            var email = JsonConvert.DeserializeObject <EmailContents>(requestBody);

            if (email == null)
            {
                var result = new JsonResult("Could not get email contents from post body.", AdventureWorksSerializerSettings.Get());
                result.StatusCode = StatusCodes.Status400BadRequest;
                return(result);
            }

            var salesOrderPattern = @"SO\d{5}\b";
            var salesOrderRegex   = new Regex(salesOrderPattern, RegexOptions.IgnoreCase);

            string salesOrderNumber = null;

            // Check the subject first
            var subjectMatch = salesOrderRegex.Match(email.Subject);

            if (subjectMatch.Success)
            {
                salesOrderNumber = subjectMatch.Value;
            }
            else
            {
                var bodyMatch = salesOrderRegex.Match(email.Body);
                if (bodyMatch.Success)
                {
                    salesOrderNumber = bodyMatch.Value;
                }
            }

            if (string.IsNullOrEmpty(salesOrderNumber))
            {
                var result = new JsonResult("Could not find a sales order number in the email.", AdventureWorksSerializerSettings.Get());
                result.StatusCode = StatusCodes.Status400BadRequest;
                return(result);
            }

            var salesOrder = await _repository.GetOrderFromOrderNumberAsync(salesOrderNumber);

            if (salesOrder == null)
            {
                var result = new JsonResult($"Could not find sales order with order number {salesOrderNumber}", AdventureWorksSerializerSettings.Get());
                result.StatusCode = StatusCodes.Status404NotFound;
                return(result);
            }

            return(new JsonResult(salesOrder, AdventureWorksSerializerSettings.Get()));
        }