Example #1
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = "ReceiveOrder")] HttpRequest req,
            [Queue("orderreceivedqueue", Connection = "StorageAccount")] IAsyncCollector <Order> outputQueueItem,
            [Table("ordersTable", Connection = "StorageAccount")] IAsyncCollector <OrderRow> outputTable,
            ILogger log)
        {
            log.LogInformation($"Order received!");

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

            Order order = null;

            try
            {
                order = JsonConvert.DeserializeObject <Order>(jsonContent);

                await outputQueueItem.AddAsync(order);

                OrderRow orderRow = new OrderRow(order);
                await outputTable.AddAsync(orderRow);
            }
            catch (Exception ex)
            {
                log.LogError("Error during order received operation", ex);
                return(new BadRequestObjectResult("Error during order received operation"));
            }

            return((ActionResult) new OkObjectResult($"Order {order} received."));
        }
        public static void Run([BlobTrigger("invoices/{filename}")] Stream myBlob,
                               string filename,
                               [Table("ordersTable", "Order", "{filename}", Filter = null, Take = 50)] OrderRow orderRow,
                               ILogger log,
                               [SendGrid(ApiKey = "SendGridApiKey")] out SendGridMessage message)
        {
            log.LogInformation($"File Processed : {filename}");
            log.LogInformation($"Order: {orderRow}");
            log.LogInformation($"Customer mail: {orderRow.custEmail}");

            message = CreateMailMessage(orderRow, myBlob);
        }
        private static SendGridMessage CreateMailMessage(OrderRow orderRow, Stream myBlob)
        {
            var message = new SendGridMessage()
            {
                Subject = "Azure Functions Invoice",
                From    = new EmailAddress("*****@*****.**")
            };

            message.AddTo(new EmailAddress(orderRow.custEmail));

            var buffer = ReadBufferFromStream(myBlob);

            var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(buffer);
            var text           = System.Convert.ToBase64String(plainTextBytes);

            message.AddContent("text/plain", System.Text.Encoding.UTF8.GetString(plainTextBytes));
            message.AddAttachment("invoice.txt", text, "text/plain", "attachment", "Invoice File");

            return(message);
        }