forked from ffilardi/fo-datapackageimport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PackageStatusUpdate.cs
181 lines (133 loc) · 5.9 KB
/
PackageStatusUpdate.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Microsoft.Azure.Cosmos.Table;
namespace FO_DataPackageImport
{
public static class PackageStatusUpdate
{
[FunctionName("PackageStatusUpdate")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req,
ILogger log)
{
string accountName = Environment.GetEnvironmentVariable("StorageAccountName");
string accountKey = Environment.GetEnvironmentVariable("StorageAccountKey");
EventEntity newEntity;
try
{
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
if (String.IsNullOrEmpty(requestBody))
{
throw new Exception("Empty request body");
}
BusinessEvent businessEvent = JsonConvert.DeserializeObject<BusinessEvent>(requestBody);
StorageCredentials creds = new StorageCredentials(accountName, accountKey);
CloudStorageAccount account = new CloudStorageAccount(creds, useHttps: true);
CloudTableClient client = account.CreateCloudTableClient();
CloudTable table = client.GetTableReference(Environment.GetEnvironmentVariable("StorageTable"));
table.CreateIfNotExists();
newEntity = new EventEntity()
{
PartitionKey = businessEvent.EntityName,
RowKey = businessEvent.EventId,
BusinessEventId = businessEvent.BusinessEventId,
ProjectName = businessEvent.ProjectName,
ProjectDescription = businessEvent.ProjectDescription,
ExecutionId = businessEvent.ExecutionId,
LegalEntity = businessEvent.LegalEntity,
NoOfRecords = businessEvent.NoOfRecords,
NoOfCreatedRecords = businessEvent.NoOfCreatedRecords,
NoOfUpdatedRecords = businessEvent.NoOfUpdatedRecords,
NoOfErrorRecords = businessEvent.NoOfErrorRecords,
OperationType = businessEvent.OperationType,
ProjectCategory = businessEvent.ProjectCategory,
StartedDateTime = businessEvent.StartedDateTime,
EndDateTime = businessEvent.EndDateTime,
Status = businessEvent.Status
};
await table.ExecuteAsync(TableOperation.Insert(newEntity));
}
catch (Exception ex)
{
log.LogError($"Error: '{ex.Message}'");
return new BadRequestObjectResult(ex.Message);
}
return new OkObjectResult(newEntity);
}
}
public class EventEntity : TableEntity
{
public EventEntity(string entityName, string eventId)
{
this.PartitionKey = entityName;
this.RowKey = eventId;
}
public EventEntity() { }
public string BusinessEventId { get; set; }
public string ProjectName { get; set; }
public string ProjectDescription { get; set; }
public string ExecutionId { get; set; }
public string LegalEntity { get; set; }
public long NoOfRecords { get; set; }
public long NoOfCreatedRecords { get; set; }
public long NoOfUpdatedRecords { get; set; }
public long NoOfErrorRecords { get; set; }
public string OperationType { get; set; }
public string ProjectCategory { get; set; }
public DateTimeOffset StartedDateTime { get; set; }
public DateTimeOffset EndDateTime { get; set; }
public string Status { get; set; }
}
public partial class BusinessEvent
{
[JsonProperty("BusinessEventId")]
public string BusinessEventId { get; set; }
[JsonProperty("ControlNumber")]
public long ControlNumber { get; set; }
[JsonProperty("EndDateTime")]
public DateTimeOffset EndDateTime { get; set; }
[JsonProperty("EntityName")]
public string EntityName { get; set; }
[JsonProperty("EventId")]
public string EventId { get; set; }
[JsonProperty("EventTime")]
public string EventTime { get; set; }
[JsonProperty("ExecutionId")]
public string ExecutionId { get; set; }
[JsonProperty("ProjectName")]
public string ProjectName { get; set; }
[JsonProperty("ProjectDescription")]
public string ProjectDescription { get; set; }
[JsonProperty("LegalEntity")]
public string LegalEntity { get; set; }
[JsonProperty("MajorVersion")]
public long MajorVersion { get; set; }
[JsonProperty("MinorVersion")]
public long MinorVersion { get; set; }
[JsonProperty("NoOfCreatedRecords")]
public long NoOfCreatedRecords { get; set; }
[JsonProperty("NoOfRecords")]
public long NoOfRecords { get; set; }
[JsonProperty("NoOfUpdatedRecords")]
public long NoOfUpdatedRecords { get; set; }
[JsonProperty("NoOfErrorRecords")]
public long NoOfErrorRecords { get; set; }
[JsonProperty("OperationType")]
public string OperationType { get; set; }
[JsonProperty("ProjectCategory")]
public string ProjectCategory { get; set; }
[JsonProperty("RecId")]
public long RecId { get; set; }
[JsonProperty("StartedDateTime")]
public DateTimeOffset StartedDateTime { get; set; }
[JsonProperty("Status")]
public string Status { get; set; }
}
}