diff --git a/PlanB.Butler.Services/PlanB.Butler.Services/FinanceService.cs b/PlanB.Butler.Services/PlanB.Butler.Services/FinanceService.cs index e99105c..1f10a80 100644 --- a/PlanB.Butler.Services/PlanB.Butler.Services/FinanceService.cs +++ b/PlanB.Butler.Services/PlanB.Butler.Services/FinanceService.cs @@ -110,8 +110,8 @@ public static void PostDocumentMoney( } 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); + trace.Add(string.Format("{0} - {1}", methodName, "rejected"), e.Message); + trace.Add(string.Format("{0} - {1} - StackTrace", methodName, "rejected"), e.StackTrace); trace.Add("MessageId", messageHeader.MessageId); trace.Add("DeliveryCount", messageHeader.SystemProperties.DeliveryCount.ToString()); if (messageHeader.SystemProperties.DeliveryCount == 1) @@ -125,7 +125,7 @@ public static void PostDocumentMoney( } finally { - log.LogTrace(eventId, $"'{methodName}' - busobjkey finished"); + log.LogTrace(eventId, $"'{methodName}' - finished"); log.LogInformation(correlationId, $"'{methodName}' - {messageHeader.SystemProperties.DeliveryCount} - finished", trace); } } diff --git a/PlanB.Butler.Services/PlanB.Butler.Services/MealService.cs b/PlanB.Butler.Services/PlanB.Butler.Services/MealService.cs index 6843e4b..123b3ea 100644 --- a/PlanB.Butler.Services/PlanB.Butler.Services/MealService.cs +++ b/PlanB.Butler.Services/PlanB.Butler.Services/MealService.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Globalization; using System.IO; +using System.Net.Mime; using System.Reflection; using System.Threading.Tasks; @@ -48,6 +49,7 @@ public static class MealService /// IActionResult. /// [FunctionName("CreateMeal")] + [Consumes(MediaTypeNames.Application.Json)] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(typeof(ErrorModel), StatusCodes.Status400BadRequest)] public static async Task CreateMeal( @@ -94,13 +96,13 @@ public static async Task CreateMeal( Task task = blob.UploadTextAsync(requestBody); } - log.LogInformation(correlationId, $"'{methodName}' - success", trace); actionResult = new OkResult(); + log.LogInformation(correlationId, $"'{methodName}' - success", trace); } 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); + trace.Add(string.Format("{0} - {1}", methodName, "rejected"), e.Message); + trace.Add(string.Format("{0} - {1} - StackTrace", methodName, "rejected"), e.StackTrace); log.LogInformation(correlationId, $"'{methodName}' - rejected", trace); log.LogError(correlationId, $"'{methodName}' - rejected", trace); ErrorModel errorModel = new ErrorModel() @@ -115,7 +117,7 @@ public static async Task CreateMeal( } finally { - log.LogTrace(eventId, $"'{methodName}' - busobjkey finished"); + log.LogTrace(eventId, $"'{methodName}' - finished"); log.LogInformation(correlationId, $"'{methodName}' - finished", trace); } @@ -220,8 +222,8 @@ public static async Task GetMeals( } 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); + trace.Add(string.Format("{0} - {1}", methodName, "rejected"), e.Message); + trace.Add(string.Format("{0} - {1} - StackTrace", methodName, "rejected"), e.StackTrace); log.LogInformation(correlationId, $"'{methodName}' - rejected", trace); log.LogError(correlationId, $"'{methodName}' - rejected", trace); @@ -231,11 +233,11 @@ public static async Task GetMeals( Details = e.StackTrace, Message = e.Message, }; - actionResult = new BadRequestObjectResult(meals); + actionResult = new BadRequestObjectResult(errorModel); } finally { - log.LogTrace(eventId, $"'{methodName}' - busobjkey finished"); + log.LogTrace(eventId, $"'{methodName}' - finished"); log.LogInformation(correlationId, $"'{methodName}' - finished", trace); } @@ -282,8 +284,8 @@ public static IActionResult GetMealById( } 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); + trace.Add(string.Format("{0} - {1}", methodName, "rejected"), e.Message); + trace.Add(string.Format("{0} - {1} - StackTrace", methodName, "rejected"), e.StackTrace); log.LogInformation(correlationId, $"'{methodName}' - rejected", trace); log.LogError(correlationId, $"'{methodName}' - rejected", trace); diff --git a/PlanB.Butler.Services/PlanB.Butler.Services/Models/OrderModel.cs b/PlanB.Butler.Services/PlanB.Butler.Services/Models/OrderModel.cs new file mode 100644 index 0000000..a5e201c --- /dev/null +++ b/PlanB.Butler.Services/PlanB.Butler.Services/Models/OrderModel.cs @@ -0,0 +1,85 @@ +// Copyright (c) PlanB. GmbH. All Rights Reserved. +// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. + +using System; + +namespace PlanB.Butler.Services.Models +{ + /// + /// OrderModel. + /// + public class OrderModel + { + /// + /// Gets or sets the date. + /// + /// + /// The date. + /// + public DateTime Date { get; set; } + + /// + /// Gets or sets the company status. + /// + /// + /// The company status. + /// + public string CompanyStatus { get; set; } + + /// + /// Gets or sets the name. + /// + /// + /// The name. + /// + public string Name { get; set; } + + /// + /// Gets or sets the name of the company. + /// + /// + /// The name of the company. + /// + public string CompanyName { get; set; } + + /// + /// Gets or sets the restaurant. + /// + /// + /// The restaurant. + /// + public string Restaurant { get; set; } + + /// + /// Gets or sets the meal. + /// + /// + /// The meal. + /// + public string Meal { get; set; } + + /// + /// Gets or sets the price. + /// + /// + /// The price. + /// + public double Price { get; set; } + + /// + /// Gets or sets the quantity. + /// + /// + /// The quantity. + /// + public int Quantity { get; set; } + + /// + /// Gets or sets the total. + /// + /// + /// The total. + /// + public double Total { get; set; } + } +} diff --git a/PlanB.Butler.Services/PlanB.Butler.Services/Models/OrdersModel.cs b/PlanB.Butler.Services/PlanB.Butler.Services/Models/OrdersModel.cs new file mode 100644 index 0000000..4b0105e --- /dev/null +++ b/PlanB.Butler.Services/PlanB.Butler.Services/Models/OrdersModel.cs @@ -0,0 +1,40 @@ +// Copyright (c) PlanB. GmbH. All Rights Reserved. +// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. + +using System; +using System.Collections.Generic; +using System.Text; + +namespace PlanB.Butler.Services.Models +{ + /// + /// OrdersModel. + /// + public class OrdersModel + { + /// + /// Gets or sets the date. + /// + /// + /// The date. + /// + public DateTime Date { get; set; } + + /// + /// Gets or sets the name of the login. + /// + /// john.doe@domain.com. + /// + /// The name of the login. + /// + public string LoginName { get; set; } + + /// + /// Gets or sets the orders. + /// + /// + /// The orders. + /// + public List Orders { get; set; } + } +} diff --git a/PlanB.Butler.Services/PlanB.Butler.Services/OrderService.cs b/PlanB.Butler.Services/PlanB.Butler.Services/OrderService.cs index 952846f..9203bdd 100644 --- a/PlanB.Butler.Services/PlanB.Butler.Services/OrderService.cs +++ b/PlanB.Butler.Services/PlanB.Butler.Services/OrderService.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.IO; using System.Net.Http; +using System.Net.Mime; using System.Reflection; using System.Text; using System.Threading.Tasks; @@ -22,6 +23,7 @@ using Newtonsoft.Json; using Newtonsoft.Json.Linq; using PlanB.Butler.Services.Extensions; +using PlanB.Butler.Services.Models; namespace PlanB.Butler.Services { @@ -97,15 +99,15 @@ public static async Task GetDailyOrderOverview( } 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); + trace.Add(string.Format("{0} - {1}", methodName, "rejected"), e.Message); + trace.Add(string.Format("{0} - {1} - StackTrace", methodName, "rejected"), e.StackTrace); log.LogInformation(correlationId, $"'{methodName}' - rejected", trace); log.LogError(correlationId, $"'{methodName}' - rejected", trace); throw; } finally { - log.LogTrace(eventId, $"'{methodName}' - busobjkey finished"); + log.LogTrace(eventId, $"'{methodName}' - finished"); log.LogInformation(correlationId, $"'{methodName}' - finished", trace); } @@ -168,15 +170,15 @@ public static async Task GetDailyOrderOverviewForUser( } 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); + trace.Add(string.Format("{0} - {1}", methodName, "rejected"), e.Message); + trace.Add(string.Format("{0} - {1} - StackTrace", methodName, "rejected"), e.StackTrace); log.LogInformation(correlationId, $"'{methodName}' - rejected", trace); log.LogError(correlationId, $"'{methodName}' - rejected", trace); throw; } finally { - log.LogTrace(eventId, $"'{methodName}' - busobjkey finished"); + log.LogTrace(eventId, $"'{methodName}' - finished"); log.LogInformation(correlationId, $"'{methodName}' - finished", trace); } @@ -230,87 +232,91 @@ public static async void PostDocumentOrder( } 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); + trace.Add(string.Format("{0} - {1}", methodName, "rejected"), e.Message); + trace.Add(string.Format("{0} - {1} - StackTrace", methodName, "rejected"), e.StackTrace); log.LogInformation(correlationId, $"'{methodName}' - rejected", trace); log.LogError(correlationId, $"'{methodName}' - rejected", trace); throw; } finally { - log.LogTrace(eventId, $"'{methodName}' - busobjkey finished"); + log.LogTrace(eventId, $"'{methodName}' - finished"); log.LogInformation(correlationId, $"'{methodName}' - finished", trace); } } /// - /// Reads the meals. + /// Creates the order. /// - /// The Input from the Trigger. + /// The input. + /// The output message. /// The log. - /// - /// All meals. - /// + /// IActionResult. + /// log. [Singleton] [FunctionName("CreateOrder")] - [return: ServiceBus("q.planbutlerupdateorder", Connection = "ServiceBusConnection")] - public static Message PostOrderToQueue([HttpTrigger(AuthorizationLevel.Function, "POST", Route = "orders")] HttpRequest input, ILogger log) + [Consumes(MediaTypeNames.Application.Json)] + [ProducesResponseType(StatusCodes.Status202Accepted)] + [ProducesResponseType(typeof(ErrorModel), StatusCodes.Status400BadRequest)] + public static IActionResult CreateOrder( + [HttpTrigger(AuthorizationLevel.Function, "POST", Route = "orders")] HttpRequest input, + [ServiceBus("q.planbutlerupdateorder", Connection = "ServiceBusConnection")] out Message outputMessage, + ILogger log) { if (log is null) { throw new ArgumentNullException(nameof(log)); } - 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); + IActionResult actionResult = null; + outputMessage = null; + try { - byte[] result; - using (var streamReader = new MemoryStream()) - { - input.Body.CopyTo(streamReader); - result = streamReader.ToArray(); - } - - msg.Body = result; + trace.Add(Constants.ButlerCorrelationTraceName, correlationId.ToString()); + var body = input.ReadAsStringAsync().Result; + trace.Add("body", body); + OrdersModel orders = JsonConvert.DeserializeObject(body); + var stringDate = orders.Date.ToString("yyyy-MM-dd"); + trace.Add("date", stringDate); - 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) + byte[] bytes = Encoding.ASCII.GetBytes(body); + outputMessage = new Message(bytes) { - name = item.Name; - date = item.Date; - break; - } + Label = $"{orders.LoginName}_{stringDate}", + CorrelationId = correlationId.ToString(), + }; - var stringDate = date.ToString("yyyy-MM-dd"); - msg.Label = $"{name}_{stringDate}"; - trace.Add("name", name); - trace.Add("jsondata", json); - trace.Add("date", stringDate); - trace.Add("label", msg.Label); - trace.Add("name", name); + trace.Add("loginname", orders.LoginName); + trace.Add("label", outputMessage.Label); + actionResult = new AcceptedResult(); + log.LogInformation(correlationId, $"'{methodName}' - success", trace); } 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); + trace.Add(string.Format("{0} - {1}", methodName, "rejected"), e.Message); + trace.Add(string.Format("{0} - {1} - StackTrace", methodName, "rejected"), e.StackTrace); log.LogInformation(correlationId, $"'{methodName}' - rejected", trace); log.LogError(correlationId, $"'{methodName}' - rejected", trace); - throw; + ErrorModel errorModel = new ErrorModel() + { + CorrelationId = correlationId, + Details = e.StackTrace, + Message = e.Message, + }; + actionResult = new BadRequestObjectResult(errorModel); } finally { - log.LogTrace(eventId, $"'{methodName}' - busobjkey finished"); + log.LogTrace(eventId, $"'{methodName}' - finished"); log.LogInformation(correlationId, $"'{methodName}' - finished", trace); } - return msg; + return actionResult; } } } diff --git a/PlanB.Butler.Services/PlanB.Butler.Services/RestaurantService.cs b/PlanB.Butler.Services/PlanB.Butler.Services/RestaurantService.cs index c8f4f00..8e5feb9 100644 --- a/PlanB.Butler.Services/PlanB.Butler.Services/RestaurantService.cs +++ b/PlanB.Butler.Services/PlanB.Butler.Services/RestaurantService.cs @@ -73,8 +73,8 @@ public static async Task GetRestaurants( } 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); + trace.Add(string.Format("{0} - {1}", methodName, "rejected"), e.Message); + trace.Add(string.Format("{0} - {1} - StackTrace", methodName, "rejected"), e.StackTrace); log.LogInformation(correlationId, $"'{methodName}' - rejected", trace); log.LogError(correlationId, $"'{methodName}' - rejected", trace); @@ -84,11 +84,11 @@ public static async Task GetRestaurants( Details = e.StackTrace, Message = e.Message, }; - actionResult = new BadRequestObjectResult(restaurant); + actionResult = new BadRequestObjectResult(errorModel); } finally { - log.LogTrace(eventId, $"'{methodName}' - busobjkey finished"); + log.LogTrace(eventId, $"'{methodName}' - finished"); log.LogInformation(correlationId, $"'{methodName}' - finished", trace); } @@ -147,8 +147,8 @@ public static async Task CreateRestaurant( } 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); + trace.Add(string.Format("{0} - {1}", methodName, "rejected"), e.Message); + trace.Add(string.Format("{0} - {1} - StackTrace", methodName, "rejected"), e.StackTrace); log.LogInformation(correlationId, $"'{methodName}' - rejected", trace); log.LogError(correlationId, $"'{methodName}' - rejected", trace); ErrorModel errorModel = new ErrorModel() @@ -163,7 +163,7 @@ public static async Task CreateRestaurant( } finally { - log.LogTrace(eventId, $"'{methodName}' - busobjkey finished"); + log.LogTrace(eventId, $"'{methodName}' - finished"); log.LogInformation(correlationId, $"'{methodName}' - finished", trace); }