-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebHook.cs
62 lines (57 loc) · 2.35 KB
/
WebHook.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System;
using System.IO;
using System.Collections;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Extensions.Storage ;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Microsoft.WindowsAzure.Storage.Queue;
namespace WebHook
{
public class OrderTable
{
public string PartitionKey { get; set; }
public string RowKey { get; set; }
public string OrderId { get; set; }
public string ProductId { get; set; }
public string ToEmail { get; set; }
public string FromEmail { get; set; }
public string Message { get; set; }
public string Price { get; set; }
}
public class Order
{
public string OrderId { get; set; }
public string ProductId { get; set; }
public string ToEmail { get; set; }
public string FromEmail { get; set; }
public string Message { get; set; }
public string Price { get; set; }
}
public static class WebHook
{
[FunctionName("Start_WebHook")]
public static async Task<object> Run(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = "create")] HttpRequest req, ILogger log,
[Queue("outputQueueItem", Connection = "AzureWebJobsStorage")] IAsyncCollector<Order> outQueue,
[Table("orders", Connection = "AzureWebJobsStorage")] IAsyncCollector<OrderTable> orderTable)
{
log.LogInformation($"Orderinfo received");
string jsonContent = await req.ReadAsStringAsync();
var order = JsonConvert.DeserializeObject<Order>(jsonContent);
var table = JsonConvert.DeserializeObject<OrderTable>(jsonContent);
table.PartitionKey = "Orders";
table.RowKey = order.OrderId;
log.LogInformation($"Order {order.OrderId} recevied from {order.FromEmail} for product {order.ProductId}");
await outQueue.AddAsync(order);
await orderTable.AddAsync(table);
return new CreatedResult(
"/api/route", $"Thank you for your order! Your order with order number {order.ProductId} " +
$"will be sent & and a confirmation will be sent to email {order.ToEmail}.");
}
}
}