diff --git a/PlanB.Butler.Library/PlanB.Butler.Library/BotMethods.cs b/PlanB.Butler.Library/PlanB.Butler.Library/BotMethods.cs index 8172999..da6df6d 100644 --- a/PlanB.Butler.Library/PlanB.Butler.Library/BotMethods.cs +++ b/PlanB.Butler.Library/PlanB.Butler.Library/BotMethods.cs @@ -40,9 +40,19 @@ public static string GetDocument(string container, string resourceName, string s /// The que. /// The service bus connection string. /// - public static HttpStatusCode PutDocument(string container, string resourceName, string body, string que, string serviceBusConnectionString) + public static async Task PutDocument(string container, string resourceName, string body, string que, string serviceBusConnectionString) { BackendCommunication backendcom = new BackendCommunication(); + //client.DefaultRequestHeaders.Add("body", serviceBusConnectionString); + + var content = new StringContent(body.ToString()); + + + HttpResponseMessage responseMessage = await client.PostAsync("http://localhost:7071/api/orders", content); + + var tmp = 0; + + HttpStatusCode taskUrl = backendcom.PutDocument(container, resourceName, body, que, serviceBusConnectionString); return taskUrl; } @@ -51,7 +61,7 @@ public static HttpStatusCode PutDocument(string container, string resourceName, /// /// /// - public static HttpStatusCode UploadMoney(Order order, string storageAccountUrl, string storageAccountKey, string serviceBusConnectionString) + public static async Task UploadMoney(Order order, string storageAccountUrl, string storageAccountKey, string serviceBusConnectionString) { try { @@ -63,7 +73,7 @@ public static HttpStatusCode UploadMoney(Order order, string storageAccountUrl, User user = new User() { Name = order.Name, Owe = order.Price }; _money.User.Add(user); - HttpStatusCode status = PutDocument("moneylog", "money_" + DateTime.Now.Month.ToString() + "_" + DateTime.Now.Year + ".json", JsonConvert.SerializeObject(_money), "q.planbutlerupdatemoney", serviceBusConnectionString); + HttpStatusCode status = await PutDocument("moneylog", "money_" + DateTime.Now.Month.ToString() + "_" + DateTime.Now.Year + ".json", JsonConvert.SerializeObject(_money), "q.planbutlerupdatemoney", serviceBusConnectionString); return status; } else // enters if everything is normal @@ -72,7 +82,7 @@ public static HttpStatusCode UploadMoney(Order order, string storageAccountUrl, newOwe += order.Price; _money.User[userId].Owe = newOwe; - HttpStatusCode status = PutDocument("moneylog", "money_" + DateTime.Now.Month.ToString() + "_" + DateTime.Now.Year + ".json", JsonConvert.SerializeObject(_money), "q.planbutlerupdatemoney", serviceBusConnectionString); + HttpStatusCode status = await PutDocument("moneylog", "money_" + DateTime.Now.Month.ToString() + "_" + DateTime.Now.Year + ".json", JsonConvert.SerializeObject(_money), "q.planbutlerupdatemoney", serviceBusConnectionString); return status; } } @@ -83,7 +93,7 @@ public static HttpStatusCode UploadMoney(Order order, string storageAccountUrl, users.Add(user); MoneyLog money = new MoneyLog() { Monthnumber = DateTime.Now.Month, Title = "moneylog", User = users }; - HttpStatusCode status = PutDocument("moneylog", "money_" + DateTime.Now.Month.ToString() + "_" + DateTime.Now.Year + ".json", JsonConvert.SerializeObject(money), "q.planbutlerupdatemoney", serviceBusConnectionString); + HttpStatusCode status = await PutDocument("moneylog", "money_" + DateTime.Now.Month.ToString() + "_" + DateTime.Now.Year + ".json", JsonConvert.SerializeObject(money), "q.planbutlerupdatemoney", serviceBusConnectionString); return status; } } @@ -95,7 +105,7 @@ public static HttpStatusCode UploadMoney(Order order, string storageAccountUrl, /// The storage account key. /// The service bus connection string. /// - public static HttpStatusCode UploadOrder(Order order, string storageAccountUrl, string storageAccountKey, string serviceBusConnectionString) + public static async Task UploadOrder(Order order, string storageAccountUrl, string storageAccountKey, string serviceBusConnectionString) { DateTime date = DateTime.Now; @@ -110,7 +120,7 @@ public static HttpStatusCode UploadOrder(Order order, string storageAccountUrl, orderBlob.OrderList.Add(order); - HttpStatusCode status = BotMethods.PutDocument("orders", "orders_" + stringDate + "_" + order.Name + ".json", JsonConvert.SerializeObject(orderBlob), "q.planbutlerupdateorder", serviceBusConnectionString); + HttpStatusCode status = await BotMethods.PutDocument("orders", "orders_" + stringDate + "_" + order.Name + ".json", JsonConvert.SerializeObject(orderBlob), "q.planbutlerupdateorder", serviceBusConnectionString); return status; } @@ -122,7 +132,7 @@ public static HttpStatusCode UploadOrder(Order order, string storageAccountUrl, orderBlob.OrderList = new List(); orderBlob.OrderList.Add(order); - HttpStatusCode status = BotMethods.PutDocument("orders", "orders_" + stringDate + "_" + order.Name + ".json", JsonConvert.SerializeObject(orderBlob), "q.planbutlerupdateorder", serviceBusConnectionString); + HttpStatusCode status = await BotMethods.PutDocument("orders", "orders_" + stringDate + "_" + order.Name + ".json", JsonConvert.SerializeObject(orderBlob), "q.planbutlerupdateorder", serviceBusConnectionString); return status; } catch (Exception ex) @@ -183,7 +193,7 @@ internal static DateTime CalculateNextDay(string day) /// The storage account key. /// The service bus connection string. /// - public static HttpStatusCode UploadForOtherDay(Order order, DateTime day, string storageAccountUrl, string storageAccountKey, string serviceBusConnectionString) + public static async Task UploadForOtherDay(Order order, DateTime day, string storageAccountUrl, string storageAccountKey, string serviceBusConnectionString) { string stringDate = day.ToString("yyyy-MM-dd"); @@ -193,7 +203,7 @@ public static HttpStatusCode UploadForOtherDay(Order order, DateTime day, string orderBlob.OrderList = new List(); orderBlob = JsonConvert.DeserializeObject(BotMethods.GetDocument("orders", "orders_" + stringDate + "_" + order.Name + ".json", storageAccountUrl, storageAccountKey)); orderBlob.OrderList.Add(order); - HttpStatusCode status = BotMethods.PutDocument("orders", "orders_" + stringDate + "_" + order.Name + ".json", JsonConvert.SerializeObject(orderBlob), "q.planbutlerupdateorder", serviceBusConnectionString); + HttpStatusCode status = await BotMethods.PutDocument("orders", "orders_" + stringDate + "_" + order.Name + ".json", JsonConvert.SerializeObject(orderBlob), "q.planbutlerupdateorder", serviceBusConnectionString); return status; } catch @@ -204,7 +214,7 @@ public static HttpStatusCode UploadForOtherDay(Order order, DateTime day, string orderBlob.OrderList = new List(); order.Date = DateTime.Now; orderBlob.OrderList.Add(order); - HttpStatusCode status = BotMethods.PutDocument("orders", "orders_" + stringDate + "_" + order.Name + ".json", JsonConvert.SerializeObject(orderBlob), "q.planbutlerupdateorder", serviceBusConnectionString); + HttpStatusCode status = await BotMethods.PutDocument("orders", "orders_" + stringDate + "_" + order.Name + ".json", JsonConvert.SerializeObject(orderBlob), "q.planbutlerupdateorder", serviceBusConnectionString); return status; } catch (Exception ex) @@ -223,7 +233,7 @@ public static HttpStatusCode UploadForOtherDay(Order order, DateTime day, string /// The storage account key. /// The service bus connection string. /// - public static HttpStatusCode UploadOrderforSalaryDeduction(Order order, string storageAccountUrl, string storageAccountKey, string serviceBusConnectionString) + public static async Task UploadOrderforSalaryDeduction(Order order, string storageAccountUrl, string storageAccountKey, string serviceBusConnectionString) { SalaryDeduction salaryDeduction = new SalaryDeduction(); int dayNumber = order.Date.DayOfYear; @@ -231,7 +241,7 @@ public static HttpStatusCode UploadOrderforSalaryDeduction(Order order, string s { salaryDeduction = JsonConvert.DeserializeObject(GetDocument("salarydeduction", "orders_" + dayNumber.ToString() + "_" + DateTime.Now.Year + ".json", storageAccountUrl, storageAccountKey)); salaryDeduction.Order.Add(order); - HttpStatusCode status = PutDocument("salarydeduction", "orders_" + dayNumber.ToString() + "_" + DateTime.Now.Year.ToString() + ".json", JsonConvert.SerializeObject(salaryDeduction), "q.planbutlerupdatesalary", serviceBusConnectionString); + HttpStatusCode status = await PutDocument("salarydeduction", "orders_" + dayNumber.ToString() + "_" + DateTime.Now.Year.ToString() + ".json", JsonConvert.SerializeObject(salaryDeduction), "q.planbutlerupdatesalary", serviceBusConnectionString); return status; } catch // enters if blob dont exist @@ -244,7 +254,7 @@ public static HttpStatusCode UploadOrderforSalaryDeduction(Order order, string s orders.Add(order); salaryDeduction.Order = orders; - HttpStatusCode status = PutDocument("salarydeduction", "orders_" + dayNumber.ToString() + "_" + DateTime.Now.Year.ToString() + ".json", JsonConvert.SerializeObject(salaryDeduction), "q.planbutlerupdatesalary", serviceBusConnectionString); + HttpStatusCode status = await PutDocument("salarydeduction", "orders_" + dayNumber.ToString() + "_" + DateTime.Now.Year.ToString() + ".json", JsonConvert.SerializeObject(salaryDeduction), "q.planbutlerupdatesalary", serviceBusConnectionString); return status; } } @@ -257,7 +267,7 @@ public static HttpStatusCode UploadOrderforSalaryDeduction(Order order, string s /// The storage account key. /// The service bus connection string. /// - public static HttpStatusCode UploadOrderforSalaryDeductionForAnotherDay(Order order, DateTime day, string storageAccountUrl, string storageAccountKey, string serviceBusConnectionString) + public static async Task UploadOrderforSalaryDeductionForAnotherDay(Order order, DateTime day, string storageAccountUrl, string storageAccountKey, string serviceBusConnectionString) { SalaryDeduction salaryDeduction = new SalaryDeduction(); order.Date = day; @@ -266,7 +276,7 @@ public static HttpStatusCode UploadOrderforSalaryDeductionForAnotherDay(Order or { salaryDeduction = JsonConvert.DeserializeObject(GetDocument("salarydeduction", "orders_" + dayNumber.ToString() + "_" + DateTime.Now.Year + ".json", storageAccountUrl, storageAccountKey)); salaryDeduction.Order.Add(order); - HttpStatusCode status = PutDocument("salarydeduction", "orders_" + dayNumber.ToString() + "_" + DateTime.Now.Year.ToString() + ".json", JsonConvert.SerializeObject(salaryDeduction), "q.planbutlerupdatesalary", serviceBusConnectionString); + HttpStatusCode status = await PutDocument("salarydeduction", "orders_" + dayNumber.ToString() + "_" + DateTime.Now.Year.ToString() + ".json", JsonConvert.SerializeObject(salaryDeduction), "q.planbutlerupdatesalary", serviceBusConnectionString); return status; } catch // enters if blob dont exist @@ -279,7 +289,7 @@ public static HttpStatusCode UploadOrderforSalaryDeductionForAnotherDay(Order or orders.Add(order); salaryDeduction.Order = orders; - HttpStatusCode status = PutDocument("salarydeduction", "orders_" + dayNumber.ToString() + "_" + DateTime.Now.Year.ToString() + ".json", JsonConvert.SerializeObject(salaryDeduction), "q.planbutlerupdatesalary", serviceBusConnectionString); + HttpStatusCode status = await PutDocument("salarydeduction", "orders_" + dayNumber.ToString() + "_" + DateTime.Now.Year.ToString() + ".json", JsonConvert.SerializeObject(salaryDeduction), "q.planbutlerupdatesalary", serviceBusConnectionString); return status; } } @@ -320,7 +330,7 @@ public static void DeleteMoney(Order order, string day, string storageAccountUrl /// The storage account key. /// The service bus connection string. /// - public static HttpStatusCode UploadMoneyCompany(Order order, string storageAccountUrl, string storageAccountKey, string serviceBusConnectionString) + public static async Task UploadMoneyCompany(Order order, string storageAccountUrl, string storageAccountKey, string serviceBusConnectionString) { try { @@ -336,7 +346,7 @@ public static HttpStatusCode UploadMoneyCompany(Order order, string storageAccou User user = new User() { Name = order.Name, Owe = order.Price }; _money.User.Add(user); - HttpStatusCode status = PutDocument("moneylog", "money_" + DateTime.Now.Month.ToString() + "_" + DateTime.Now.Year + ".json", JsonConvert.SerializeObject(_money), "q.planbutlerupdatemoney", serviceBusConnectionString); + HttpStatusCode status = await PutDocument("moneylog", "money_" + DateTime.Now.Month.ToString() + "_" + DateTime.Now.Year + ".json", JsonConvert.SerializeObject(_money), "q.planbutlerupdatemoney", serviceBusConnectionString); return status; } else // enters if everything is normal @@ -345,7 +355,7 @@ public static HttpStatusCode UploadMoneyCompany(Order order, string storageAccou newOwe += order.Price; _money.User[userId].Owe = newOwe; - HttpStatusCode status = PutDocument("moneylog", "money_" + DateTime.Now.Month.ToString() + "_" + DateTime.Now.Year + ".json", JsonConvert.SerializeObject(_money), "q.planbutlerupdatemoney", serviceBusConnectionString); + HttpStatusCode status = await PutDocument("moneylog", "money_" + DateTime.Now.Month.ToString() + "_" + DateTime.Now.Year + ".json", JsonConvert.SerializeObject(_money), "q.planbutlerupdatemoney", serviceBusConnectionString); return status; } } @@ -359,7 +369,7 @@ public static HttpStatusCode UploadMoneyCompany(Order order, string storageAccou User user = new User() { Name = order.CompanyName, Owe = order.Price }; _money.User.Add(user); - HttpStatusCode status = PutDocument("moneylog", "money_" + DateTime.Now.Month.ToString() + "_" + DateTime.Now.Year + ".json", JsonConvert.SerializeObject(_money), "q.planbutlerupdatemoney", serviceBusConnectionString); + HttpStatusCode status = await PutDocument("moneylog", "money_" + DateTime.Now.Month.ToString() + "_" + DateTime.Now.Year + ".json", JsonConvert.SerializeObject(_money), "q.planbutlerupdatemoney", serviceBusConnectionString); return status; } else // enters if everything is normal @@ -368,7 +378,7 @@ public static HttpStatusCode UploadMoneyCompany(Order order, string storageAccou newOwe += order.Price; _money.User[userId].Owe = newOwe; - HttpStatusCode status = PutDocument("moneylog", "money_" + DateTime.Now.Month.ToString() + "_" + DateTime.Now.Year + ".json", JsonConvert.SerializeObject(_money), "q.planbutlerupdatemoney", serviceBusConnectionString); + HttpStatusCode status = await PutDocument("moneylog", "money_" + DateTime.Now.Month.ToString() + "_" + DateTime.Now.Year + ".json", JsonConvert.SerializeObject(_money), "q.planbutlerupdatemoney", serviceBusConnectionString); return status; } } @@ -381,7 +391,7 @@ public static HttpStatusCode UploadMoneyCompany(Order order, string storageAccou users.Add(user); MoneyLog money = new MoneyLog() { Monthnumber = DateTime.Now.Month, Title = "moneylog", User = users }; - HttpStatusCode status = PutDocument("moneylog", "money_" + DateTime.Now.Month.ToString() + "_" + DateTime.Now.Year + ".json", JsonConvert.SerializeObject(money), "q.planbutlerupdatemoney", serviceBusConnectionString); + HttpStatusCode status = await PutDocument("moneylog", "money_" + DateTime.Now.Month.ToString() + "_" + DateTime.Now.Year + ".json", JsonConvert.SerializeObject(money), "q.planbutlerupdatemoney", serviceBusConnectionString); return status; } } @@ -453,7 +463,7 @@ public static void DeleteOrderforSalaryDeductionCompany(Order order, string stor /// The storage account key. /// The service bus connection string. /// - public static HttpStatusCode UploadOrderforAnotherDay(Order order, string storageAccountUrl, string storageAccountKey, string serviceBusConnectionString) + public static async Task UploadOrderforAnotherDay(Order order, string storageAccountUrl, string storageAccountKey, string serviceBusConnectionString) { DateTime date = DateTime.Now; @@ -468,7 +478,7 @@ public static HttpStatusCode UploadOrderforAnotherDay(Order order, string storag List orders = new List(); orders.Add(order); - HttpStatusCode status = PutDocument("orders", "orders_" + stringDate + "_" + order.Name + ".json", JsonConvert.SerializeObject(orderBlob), "q.planbutlerupdateorder", serviceBusConnectionString); + HttpStatusCode status = await PutDocument("orders", "orders_" + stringDate + "_" + order.Name + ".json", JsonConvert.SerializeObject(orderBlob), "q.planbutlerupdateorder", serviceBusConnectionString); return status; } @@ -480,7 +490,7 @@ public static HttpStatusCode UploadOrderforAnotherDay(Order order, string storag - HttpStatusCode status = PutDocument("orders", "orders_" + stringDate + "_" + order.Name + ".json", JsonConvert.SerializeObject(orderBlob), "q.planbutlerupdateorder", serviceBusConnectionString); + HttpStatusCode status = await PutDocument("orders", "orders_" + stringDate + "_" + order.Name + ".json", JsonConvert.SerializeObject(orderBlob), "q.planbutlerupdateorder", serviceBusConnectionString); return status; } } @@ -582,7 +592,7 @@ public static void DeleteOrderforSalaryDeduction(Order order, string storageAcco /// The storage account key. /// The service bus connection string. /// - public static HttpStatusCode NextOrderUpload(Order order, string storageAccountUrl, string storageAccountKey, string serviceBusConnectionString) + public static async Task NextOrderUpload(Order order, string storageAccountUrl, string storageAccountKey, string serviceBusConnectionString) { DateTime date = DateTime.Now; var stringDate = date.ToString("yyyy-MM-dd"); @@ -594,7 +604,7 @@ public static HttpStatusCode NextOrderUpload(Order order, string storageAccountU List orders = new List(); orders.Add(order); - HttpStatusCode status = PutDocument("orders", "orders_" + stringDate + "_" + order.Name + ".json", JsonConvert.SerializeObject(orderBlob), "q.planbutlerupdateorder", serviceBusConnectionString); + HttpStatusCode status = await PutDocument("orders", "orders_" + stringDate + "_" + order.Name + ".json", JsonConvert.SerializeObject(orderBlob), "q.planbutlerupdateorder", serviceBusConnectionString); return status; } catch // enters if blob dont exist @@ -603,7 +613,7 @@ public static HttpStatusCode NextOrderUpload(Order order, string storageAccountU orders.Add(order); - HttpStatusCode status = PutDocument("orders", "orders_" + stringDate + "_" + order.Name + ".json", JsonConvert.SerializeObject(orderBlob), "q.planbutlerupdateorder", serviceBusConnectionString); + HttpStatusCode status = await PutDocument("orders", "orders_" + stringDate + "_" + order.Name + ".json", JsonConvert.SerializeObject(orderBlob), "q.planbutlerupdateorder", serviceBusConnectionString); return status; } } diff --git a/PlanB.Butler.Services/OrderService.cs b/PlanB.Butler.Services/OrderService.cs index 0fdccd9..263b657 100644 --- a/PlanB.Butler.Services/OrderService.cs +++ b/PlanB.Butler.Services/OrderService.cs @@ -4,18 +4,24 @@ using System; using System.Collections.Generic; using System.IO; +using System.Net.Http; +using System.Reflection; using System.Text; using System.Threading.Tasks; using BotLibraryV2; using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; using Microsoft.Azure.WebJobs; using Microsoft.Azure.WebJobs.Extensions.Http; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using Microsoft.WindowsAzure.Storage; using Microsoft.WindowsAzure.Storage.Blob; +using Microsoft.Azure.ServiceBus; using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using PlanB.Butler.Services.Extensions; namespace PlanB.Butler.Services { @@ -150,8 +156,8 @@ public static async Task GetDailyOrderOverviewForUser( [Singleton] [FunctionName(nameof(PostDocumentOrder))] public static async void PostDocumentOrder( - [ServiceBusTrigger("q.planbutlerupdateorder", Connection = "butlerSend")]Microsoft.Azure.ServiceBus.Message messageHeader, - [Blob("{Label}", FileAccess.ReadWrite, Connection = "StorageSend")]CloudBlockBlob blob, + [ServiceBusTrigger("q.planbutlerupdateorder", Connection = "butlerSend")]Message messageHeader, + [Blob("orders/{Label}", FileAccess.ReadWrite, Connection = "StorageSend")]CloudBlockBlob blob, ILogger log, ExecutionContext context) { @@ -175,5 +181,73 @@ public static async void PostDocumentOrder( await blob.UploadTextAsync(payload); await blob.SetMetadataAsync(); } + + /// + /// Reads the meals. + /// + /// The Input from the Trigger. + /// The log. + /// + /// All meals. + /// + [Singleton] + [FunctionName("CreateOrder")] + [return: ServiceBus("q.planbutlerupdateorder", Connection = "ServiceBusConnection")] + public static Message PostOrderToQueue([HttpTrigger(AuthorizationLevel.Function, "POST", Route = "orders")] HttpRequest input, ILogger log) + { + if (log is null) + { + throw new ArgumentNullException(nameof(log)); + } + + HttpRequest tmp = input; + Message msg = new Message(); + Guid correlationId = Util.ReadCorrelationId(input.Headers); + var methodName = MethodBase.GetCurrentMethod().Name; + var trace = new Dictionary(); + EventId eventId = new EventId(correlationId.GetHashCode(), Constants.ButlerCorrelationTraceName); + try + { + byte[] result; + using (var streamReader = new MemoryStream()) + { + input.Body.CopyTo(streamReader); + result = streamReader.ToArray(); + } + + msg.Body = result; + + string json = System.Text.Encoding.Default.GetString(result); + OrderBlob orderBlob = JsonConvert.DeserializeObject(json); + string name = string.Empty; + DateTime date = DateTime.Now; + foreach (var item in orderBlob.OrderList) + { + name = item.Name; + date = item.Date; + break; + } + + var stringDate = date.ToString("yyyy-MM-dd"); + msg.Label = $"{name}_{stringDate}"; + + + } + catch (Exception e) + { + trace.Add(string.Format("{0} - {1}", MethodBase.GetCurrentMethod().Name, "rejected"), e.Message); + trace.Add(string.Format("{0} - {1} - StackTrace", MethodBase.GetCurrentMethod().Name, "rejected"), e.StackTrace); + log.LogInformation(correlationId, $"'{methodName}' - rejected", trace); + log.LogError(correlationId, $"'{methodName}' - rejected", trace); + throw; + } + finally + { + log.LogTrace(eventId, $"'{methodName}' - busobjkey finished"); + log.LogInformation(correlationId, $"'{methodName}' - finished", trace); + } + + return msg; + } } }