diff --git a/PlanB.Butler.Services/PlanB.Butler.Services.Test/MealServiceMockTest.cs b/PlanB.Butler.Services/PlanB.Butler.Services.Test/MealServiceMockTest.cs index 72e2578..d5aca4d 100644 --- a/PlanB.Butler.Services/PlanB.Butler.Services.Test/MealServiceMockTest.cs +++ b/PlanB.Butler.Services/PlanB.Butler.Services.Test/MealServiceMockTest.cs @@ -14,6 +14,7 @@ using Microsoft.WindowsAzure.Storage.Blob; using Moq; using Newtonsoft.Json; +using PlanB.Butler.Services.Controllers; using PlanB.Butler.Services.Models; namespace PlanB.Butler.Services.Test diff --git a/PlanB.Butler.Services/PlanB.Butler.Services.Test/MealServiceTest.cs b/PlanB.Butler.Services/PlanB.Butler.Services.Test/MealServiceTest.cs index 294a4ca..37f3a31 100644 --- a/PlanB.Butler.Services/PlanB.Butler.Services.Test/MealServiceTest.cs +++ b/PlanB.Butler.Services/PlanB.Butler.Services.Test/MealServiceTest.cs @@ -4,6 +4,7 @@ using System; using Microsoft.VisualStudio.TestTools.UnitTesting; +using PlanB.Butler.Services.Controllers; using PlanB.Butler.Services.Models; namespace PlanB.Butler.Services.Test diff --git a/PlanB.Butler.Services/PlanB.Butler.Services.Test/RestaurantServiceMockTest.cs b/PlanB.Butler.Services/PlanB.Butler.Services.Test/RestaurantServiceMockTest.cs index 024b67d..efe9928 100644 --- a/PlanB.Butler.Services/PlanB.Butler.Services.Test/RestaurantServiceMockTest.cs +++ b/PlanB.Butler.Services/PlanB.Butler.Services.Test/RestaurantServiceMockTest.cs @@ -14,6 +14,7 @@ using Microsoft.WindowsAzure.Storage.Blob; using Moq; using Newtonsoft.Json; +using PlanB.Butler.Services.Controllers; using PlanB.Butler.Services.Models; namespace PlanB.Butler.Services.Test diff --git a/PlanB.Butler.Services/PlanB.Butler.Services/FinanceService.cs b/PlanB.Butler.Services/PlanB.Butler.Services/Controllers/FinanceService.cs similarity index 99% rename from PlanB.Butler.Services/PlanB.Butler.Services/FinanceService.cs rename to PlanB.Butler.Services/PlanB.Butler.Services/Controllers/FinanceService.cs index 1f10a80..875c35f 100644 --- a/PlanB.Butler.Services/PlanB.Butler.Services/FinanceService.cs +++ b/PlanB.Butler.Services/PlanB.Butler.Services/Controllers/FinanceService.cs @@ -19,7 +19,7 @@ using Newtonsoft.Json; using PlanB.Butler.Services.Extensions; -namespace PlanB.Butler.Services +namespace PlanB.Butler.Services.Controllers { /// /// Finance. diff --git a/PlanB.Butler.Services/PlanB.Butler.Services/MealService.cs b/PlanB.Butler.Services/PlanB.Butler.Services/Controllers/MealService.cs similarity index 73% rename from PlanB.Butler.Services/PlanB.Butler.Services/MealService.cs rename to PlanB.Butler.Services/PlanB.Butler.Services/Controllers/MealService.cs index e9dfc89..0d26c25 100644 --- a/PlanB.Butler.Services/PlanB.Butler.Services/MealService.cs +++ b/PlanB.Butler.Services/PlanB.Butler.Services/Controllers/MealService.cs @@ -8,6 +8,7 @@ using System.Net.Mime; using System.Reflection; using System.Threading.Tasks; +using System.Web; using AzureFunctions.Extensions.Swashbuckle.Attribute; using Microsoft.AspNetCore.Http; @@ -21,7 +22,7 @@ using PlanB.Butler.Services.Extensions; using PlanB.Butler.Services.Models; -namespace PlanB.Butler.Services +namespace PlanB.Butler.Services.Controllers { /// /// MealService. @@ -82,14 +83,16 @@ public static async Task CreateMeal( if (isValid) { - var date = mealModel.Date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture); + var fileName = CreateFileName(mealModel); + trace.Add($"fileName", fileName); + mealModel.Id = fileName; - var filename = $"{date}-{mealModel.Restaurant}.json"; - trace.Add($"filename", filename); + var fullFileName = $"{fileName}.json"; + trace.Add($"fullFileName", fullFileName); req.HttpContext.Response.Headers.Add(Constants.ButlerCorrelationTraceHeader, correlationId.ToString()); - CloudBlockBlob blob = cloudBlobContainer.GetBlockBlobReference($"{filename}"); + CloudBlockBlob blob = cloudBlobContainer.GetBlockBlobReference($"{fullFileName}"); if (blob != null) { blob.Properties.ContentType = "application/json"; @@ -261,96 +264,98 @@ public static async Task GetMeals( IActionResult actionResult = null; List meals = new List(); - - try + using (log.BeginScope("Method:{methodName} CorrelationId:{CorrelationId} Label:{Label}", methodName, correlationId.ToString(), context.InvocationId.ToString())) { - trace.Add(Constants.ButlerCorrelationTraceName, correlationId.ToString()); - string startDateQuery = req.Query["startDate"]; - string endDateQuery = req.Query["endDate"]; - string restaurantQuery = req.Query["restaurant"]; - string prefix = string.Empty; + try + { + trace.Add(Constants.ButlerCorrelationTraceName, correlationId.ToString()); + string startDateQuery = req.Query["startDate"]; + string endDateQuery = req.Query["endDate"]; + string restaurantQuery = req.Query["restaurant"]; + string prefix = string.Empty; - bool checkForDate = false; - DateTime start = DateTime.MinValue; - DateTime end = DateTime.MinValue; + bool checkForDate = false; + DateTime start = DateTime.MinValue; + DateTime end = DateTime.MinValue; - if (!(string.IsNullOrEmpty(startDateQuery) && string.IsNullOrEmpty(endDateQuery))) - { - checkForDate = true; - DateTime.TryParse(startDateQuery, out start); - DateTime.TryParse(endDateQuery, out end); - } + if (!(string.IsNullOrEmpty(startDateQuery) && string.IsNullOrEmpty(endDateQuery))) + { + checkForDate = true; + DateTime.TryParse(startDateQuery, out start); + DateTime.TryParse(endDateQuery, out end); + } - if (checkForDate) - { - prefix = CreateBlobPrefix(startDateQuery, endDateQuery); - } + if (checkForDate) + { + prefix = CreateBlobPrefix(startDateQuery, endDateQuery); + } - BlobContinuationToken blobContinuationToken = null; - var options = new BlobRequestOptions(); - var operationContext = new OperationContext(); + BlobContinuationToken blobContinuationToken = null; + var options = new BlobRequestOptions(); + var operationContext = new OperationContext(); - List cloudBlockBlobs = new List(); - do - { - var blobs = await cloudBlobContainer.ListBlobsSegmentedAsync(prefix, true, BlobListingDetails.All, null, blobContinuationToken, options, operationContext).ConfigureAwait(false); - blobContinuationToken = blobs.ContinuationToken; - cloudBlockBlobs.AddRange(blobs.Results); - } - while (blobContinuationToken != null); + List cloudBlockBlobs = new List(); + do + { + var blobs = await cloudBlobContainer.ListBlobsSegmentedAsync(prefix, true, BlobListingDetails.All, null, blobContinuationToken, options, operationContext).ConfigureAwait(false); + blobContinuationToken = blobs.ContinuationToken; + cloudBlockBlobs.AddRange(blobs.Results); + } + while (blobContinuationToken != null); - foreach (var item in cloudBlockBlobs) - { - CloudBlockBlob blob = (CloudBlockBlob)item; - if (checkForDate) + foreach (var item in cloudBlockBlobs) { - await blob.FetchAttributesAsync(); - if (blob.Metadata.ContainsKey(MetaDate)) + CloudBlockBlob blob = (CloudBlockBlob)item; + if (checkForDate) { - var mealMetaDate = blob.Metadata[MetaDate]; - DateTime mealDate = DateTime.MinValue; - if (DateTime.TryParse(mealMetaDate, out mealDate)) + await blob.FetchAttributesAsync(); + if (blob.Metadata.ContainsKey(MetaDate)) { - var isDateInRange = IsDateInRange(start, end, mealDate); - if (isDateInRange) + var mealMetaDate = blob.Metadata[MetaDate]; + DateTime mealDate = DateTime.MinValue; + if (DateTime.TryParse(mealMetaDate, out mealDate)) { - var blobContent = blob.DownloadTextAsync(); - var blobMeal = JsonConvert.DeserializeObject(await blobContent); - meals.Add(blobMeal); + var isDateInRange = IsDateInRange(start, end, mealDate); + if (isDateInRange) + { + var blobContent = blob.DownloadTextAsync(); + var blobMeal = JsonConvert.DeserializeObject(await blobContent); + meals.Add(blobMeal); + } } } } + else + { + var content = blob.DownloadTextAsync(); + var meal = JsonConvert.DeserializeObject(await content); + meals.Add(meal); + } } - else - { - var content = blob.DownloadTextAsync(); - var meal = JsonConvert.DeserializeObject(await content); - meals.Add(meal); - } - } - log.LogInformation(correlationId, $"'{methodName}' - success", trace); - actionResult = new OkObjectResult(meals); - } - catch (Exception e) - { - 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); + log.LogInformation(correlationId, $"'{methodName}' - success", trace); + actionResult = new OkObjectResult(meals); + } + catch (Exception e) + { + 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() + ErrorModel errorModel = new ErrorModel() + { + CorrelationId = correlationId, + Details = e.StackTrace, + Message = e.Message, + }; + actionResult = new BadRequestObjectResult(errorModel); + } + finally { - CorrelationId = correlationId, - Details = e.StackTrace, - Message = e.Message, - }; - actionResult = new BadRequestObjectResult(errorModel); - } - finally - { - log.LogTrace(eventId, $"'{methodName}' - finished"); - log.LogInformation(correlationId, $"'{methodName}' - finished", trace); + log.LogTrace(eventId, $"'{methodName}' - finished"); + log.LogInformation(correlationId, $"'{methodName}' - finished", trace); + } } return actionResult; @@ -384,35 +389,37 @@ public static IActionResult GetMealById( IActionResult actionResult = null; MealModel mealModel = null; - - try + using (log.BeginScope("Method:{methodName} CorrelationId:{CorrelationId} Label:{Label}", methodName, correlationId.ToString(), context.InvocationId.ToString())) { - trace.Add(Constants.ButlerCorrelationTraceName, correlationId.ToString()); - trace.Add("id", id); - mealModel = JsonConvert.DeserializeObject(blob); + try + { + trace.Add(Constants.ButlerCorrelationTraceName, correlationId.ToString()); + trace.Add("id", id); + mealModel = JsonConvert.DeserializeObject(blob); - log.LogInformation(correlationId, $"'{methodName}' - success", trace); - actionResult = new OkObjectResult(mealModel); - } - catch (Exception e) - { - 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); + log.LogInformation(correlationId, $"'{methodName}' - success", trace); + actionResult = new OkObjectResult(mealModel); + } + catch (Exception e) + { + 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() + ErrorModel errorModel = new ErrorModel() + { + CorrelationId = correlationId, + Details = e.StackTrace, + Message = e.Message, + }; + actionResult = new BadRequestObjectResult(mealModel); + } + finally { - CorrelationId = correlationId, - Details = e.StackTrace, - Message = e.Message, - }; - actionResult = new BadRequestObjectResult(mealModel); - } - finally - { - log.LogTrace(eventId, $"'{methodName}' - finished"); - log.LogInformation(correlationId, $"'{methodName}' - finished", trace); + log.LogTrace(eventId, $"'{methodName}' - finished"); + log.LogInformation(correlationId, $"'{methodName}' - finished", trace); + } } return actionResult; @@ -540,5 +547,18 @@ internal static bool Validate(MealModel mealModel, Guid correlationId, out Error return isValid; } + + /// + /// Creates the name of the file. + /// + /// The model. + /// FileName without extension. + internal static string CreateFileName(MealModel model) + { + var date = model.Date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture); + string fileName = $"{date}-{model.Restaurant}"; + fileName = HttpUtility.UrlEncode(fileName); + return fileName; + } } } diff --git a/PlanB.Butler.Services/PlanB.Butler.Services/OrderService.cs b/PlanB.Butler.Services/PlanB.Butler.Services/Controllers/OrderService.cs similarity index 99% rename from PlanB.Butler.Services/PlanB.Butler.Services/OrderService.cs rename to PlanB.Butler.Services/PlanB.Butler.Services/Controllers/OrderService.cs index 428c313..8dda202 100644 --- a/PlanB.Butler.Services/PlanB.Butler.Services/OrderService.cs +++ b/PlanB.Butler.Services/PlanB.Butler.Services/Controllers/OrderService.cs @@ -22,7 +22,7 @@ using PlanB.Butler.Services.Extensions; using PlanB.Butler.Services.Models; -namespace PlanB.Butler.Services +namespace PlanB.Butler.Services.Controllers { /// /// OrderService. diff --git a/PlanB.Butler.Services/PlanB.Butler.Services/RestaurantService.cs b/PlanB.Butler.Services/PlanB.Butler.Services/Controllers/RestaurantService.cs similarity index 51% rename from PlanB.Butler.Services/PlanB.Butler.Services/RestaurantService.cs rename to PlanB.Butler.Services/PlanB.Butler.Services/Controllers/RestaurantService.cs index 61768b4..8670425 100644 --- a/PlanB.Butler.Services/PlanB.Butler.Services/RestaurantService.cs +++ b/PlanB.Butler.Services/PlanB.Butler.Services/Controllers/RestaurantService.cs @@ -8,6 +8,7 @@ using System.Reflection; using System.Text; using System.Threading.Tasks; +using System.Web; using AzureFunctions.Extensions.Swashbuckle.Attribute; using Microsoft.AspNetCore.Http; @@ -21,7 +22,7 @@ using PlanB.Butler.Services.Extensions; using PlanB.Butler.Services.Models; -namespace PlanB.Butler.Services +namespace PlanB.Butler.Services.Controllers { /// /// RestaurantService. @@ -62,44 +63,46 @@ public static async Task GetRestaurants( IActionResult actionResult = null; List restaurant = new List(); - - try + using (log.BeginScope("Method:{methodName} CorrelationId:{CorrelationId} Label:{Label}", methodName, correlationId.ToString(), context.InvocationId.ToString())) { - BlobContinuationToken blobContinuationToken = null; - var options = new BlobRequestOptions(); - var operationContext = new OperationContext(); - - List cloudBlockBlobs = new List(); - do + try { - var blobs = await cloudBlobContainer.ListBlobsSegmentedAsync(null, true, BlobListingDetails.All, null, blobContinuationToken, options, operationContext).ConfigureAwait(false); - blobContinuationToken = blobs.ContinuationToken; - cloudBlockBlobs.AddRange(blobs.Results); - } - while (blobContinuationToken != null); + BlobContinuationToken blobContinuationToken = null; + var options = new BlobRequestOptions(); + var operationContext = new OperationContext(); - log.LogInformation(correlationId, $"'{methodName}' - success", trace); - actionResult = new OkObjectResult(restaurant); - } - catch (Exception e) - { - 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); + List cloudBlockBlobs = new List(); + do + { + var blobs = await cloudBlobContainer.ListBlobsSegmentedAsync(null, true, BlobListingDetails.All, null, blobContinuationToken, options, operationContext).ConfigureAwait(false); + blobContinuationToken = blobs.ContinuationToken; + cloudBlockBlobs.AddRange(blobs.Results); + } + while (blobContinuationToken != null); - ErrorModel errorModel = new ErrorModel() + log.LogInformation(correlationId, $"'{methodName}' - success", trace); + actionResult = new OkObjectResult(restaurant); + } + catch (Exception e) { - CorrelationId = correlationId, - Details = e.StackTrace, - Message = e.Message, - }; - actionResult = new BadRequestObjectResult(errorModel); - } - finally - { - log.LogTrace(eventId, $"'{methodName}' - finished"); - log.LogInformation(correlationId, $"'{methodName}' - finished", trace); + 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() + { + CorrelationId = correlationId, + Details = e.StackTrace, + Message = e.Message, + }; + actionResult = new BadRequestObjectResult(errorModel); + } + finally + { + log.LogTrace(eventId, $"'{methodName}' - finished"); + log.LogInformation(correlationId, $"'{methodName}' - finished", trace); + } } return actionResult; @@ -131,14 +134,13 @@ public static IActionResult GetRestaurantById( EventId eventId = new EventId(correlationId.GetHashCode(), Constants.ButlerCorrelationTraceName); IActionResult actionResult = null; - RestaurantModel model = null; using (log.BeginScope("Method:{methodName} CorrelationId:{CorrelationId} Label:{Label}", methodName, correlationId.ToString(), context.InvocationId.ToString())) { try { trace.Add(Constants.ButlerCorrelationTraceName, correlationId.ToString()); trace.Add("id", id); - model = JsonConvert.DeserializeObject(blob); + RestaurantModel model = JsonConvert.DeserializeObject(blob); log.LogInformation(correlationId, $"'{methodName}' - success", trace); actionResult = new OkObjectResult(model); @@ -156,7 +158,74 @@ public static IActionResult GetRestaurantById( Details = e.StackTrace, Message = e.Message, }; - actionResult = new BadRequestObjectResult(model); + actionResult = new BadRequestObjectResult(errorModel); + } + finally + { + log.LogTrace(eventId, $"'{methodName}' - finished"); + log.LogInformation(correlationId, $"'{methodName}' - finished", trace); + } + } + + return actionResult; + } + + /// + /// Deletes the restaurant by identifier. + /// + /// The req. + /// The identifier. + /// The BLOB. + /// The log. + /// The context. + /// IActionResult. + [ProducesResponseType(typeof(ErrorModel), StatusCodes.Status400BadRequest)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + [ProducesResponseType(StatusCodes.Status200OK)] + [FunctionName("DeleteRestaurantById")] + public static IActionResult DeleteRestaurantById( + [HttpTrigger(AuthorizationLevel.Function, "delete", Route = "restaurants/{id}")] HttpRequest req, + string id, + [Blob("restaurants/{id}.json", FileAccess.ReadWrite, Connection = "StorageSend")] CloudBlockBlob blob, + ILogger log, + ExecutionContext context) + { + Guid correlationId = Util.ReadCorrelationId(req.Headers); + var methodName = MethodBase.GetCurrentMethod().Name; + var trace = new Dictionary(); + EventId eventId = new EventId(correlationId.GetHashCode(), Constants.ButlerCorrelationTraceName); + IActionResult actionResult = null; + + using (log.BeginScope("Method:{methodName} CorrelationId:{CorrelationId} Label:{Label}", methodName, correlationId.ToString(), context.InvocationId.ToString())) + { + try + { + trace.Add(Constants.ButlerCorrelationTraceName, correlationId.ToString()); + trace.Add("id", id); + + if (blob != null) + { + Task task = blob.DeleteIfExistsAsync(); + task.Wait(); + + actionResult = new OkResult(); + log.LogInformation(correlationId, $"'{methodName}' - success", trace); + } + } + catch (Exception e) + { + 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() + { + CorrelationId = correlationId, + Details = e.StackTrace, + Message = e.Message, + }; + actionResult = new BadRequestObjectResult(errorModel); } finally { @@ -192,65 +261,72 @@ public static async Task CreateRestaurant( EventId eventId = new EventId(correlationId.GetHashCode(), Constants.ButlerCorrelationTraceName); IActionResult actionResult = null; - try + using (log.BeginScope("Method:{methodName} CorrelationId:{CorrelationId} Label:{Label}", methodName, correlationId.ToString(), context.InvocationId.ToString())) { - trace.Add(Constants.ButlerCorrelationTraceName, correlationId.ToString()); - string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); - trace.Add("requestBody", requestBody); - - RestaurantModel restaurantModel = JsonConvert.DeserializeObject(requestBody); - - bool isValid = Validate(restaurantModel, correlationId, log, out ErrorModel errorModel); - - if (isValid) + try { - var filename = $"{restaurantModel.Id}.json"; - trace.Add($"filename", filename); + trace.Add(Constants.ButlerCorrelationTraceName, correlationId.ToString()); + string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); + trace.Add("requestBody", requestBody); - req.HttpContext.Response.Headers.Add(Constants.ButlerCorrelationTraceHeader, correlationId.ToString()); + RestaurantModel restaurantModel = JsonConvert.DeserializeObject(requestBody); - CloudBlockBlob blob = cloudBlobContainer.GetBlockBlobReference($"{filename}"); - if (blob != null) - { - blob.Properties.ContentType = "application/json"; - blob.Metadata.Add(Constants.ButlerCorrelationTraceName, correlationId.ToString().Replace("-", string.Empty)); - blob.Metadata.Add(MetaRestaurant, System.Web.HttpUtility.HtmlEncode(restaurantModel.Name)); - blob.Metadata.Add(MetaCity, System.Web.HttpUtility.HtmlEncode(restaurantModel.City)); - var restaurant = JsonConvert.SerializeObject(restaurantModel); - trace.Add("restaurant", restaurant); - - Task task = blob.UploadTextAsync(requestBody); - task.Wait(); + bool isValid = Validate(restaurantModel, correlationId, log, out ErrorModel errorModel); - actionResult = new OkObjectResult(restaurantModel); - log.LogInformation(correlationId, $"'{methodName}' - success", trace); + if (isValid) + { + var fileName = CreateFileName(restaurantModel); + trace.Add($"fileName", fileName); + restaurantModel.Id = fileName; + + var fullFileName = $"{fileName}.json"; + trace.Add($"fullFileName", fullFileName); + + req.HttpContext.Response.Headers.Add(Constants.ButlerCorrelationTraceHeader, correlationId.ToString()); + + CloudBlockBlob blob = cloudBlobContainer.GetBlockBlobReference($"{fullFileName}"); + if (blob != null) + { + blob.Properties.ContentType = "application/json"; + blob.Metadata.Add(Constants.ButlerCorrelationTraceName, correlationId.ToString().Replace("-", string.Empty)); + blob.Metadata.Add(MetaRestaurant, System.Web.HttpUtility.HtmlEncode(restaurantModel.Name)); + blob.Metadata.Add(MetaCity, System.Web.HttpUtility.HtmlEncode(restaurantModel.City)); + var restaurant = JsonConvert.SerializeObject(restaurantModel); + trace.Add("restaurant", restaurant); + + Task task = blob.UploadTextAsync(requestBody); + task.Wait(); + + actionResult = new OkObjectResult(restaurantModel); + log.LogInformation(correlationId, $"'{methodName}' - success", trace); + } + } + else + { + actionResult = new BadRequestObjectResult(errorModel); + log.LogInformation(correlationId, $"'{methodName}' - is not valid", trace); } } - else + catch (Exception e) { + 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() + { + CorrelationId = correlationId, + Details = e.StackTrace, + Message = e.Message, + }; + actionResult = new BadRequestObjectResult(errorModel); - log.LogInformation(correlationId, $"'{methodName}' - is not valid", trace); } - } - catch (Exception e) - { - 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() + finally { - CorrelationId = correlationId, - Details = e.StackTrace, - Message = e.Message, - }; - - actionResult = new BadRequestObjectResult(errorModel); - } - finally - { - log.LogTrace(eventId, $"'{methodName}' - finished"); - log.LogInformation(correlationId, $"'{methodName}' - finished", trace); + log.LogTrace(eventId, $"'{methodName}' - finished"); + log.LogInformation(correlationId, $"'{methodName}' - finished", trace); + } } return actionResult; @@ -310,5 +386,17 @@ internal static bool Validate(RestaurantModel model, Guid correlationId, ILogger return isValid; } + + /// + /// Creates the name of the file. + /// + /// The model. + /// FileName without extension. + internal static string CreateFileName(RestaurantModel model) + { + string fileName = $"{model.Name}-{model.City}"; + fileName = HttpUtility.UrlEncode(fileName); + return fileName; + } } } diff --git a/PlanB.Butler.Services/PlanB.Butler.Services/Models/MealModel.cs b/PlanB.Butler.Services/PlanB.Butler.Services/Models/MealModel.cs index 176ad51..bf4118a 100644 --- a/PlanB.Butler.Services/PlanB.Butler.Services/Models/MealModel.cs +++ b/PlanB.Butler.Services/PlanB.Butler.Services/Models/MealModel.cs @@ -14,21 +14,13 @@ namespace PlanB.Butler.Services.Models public class MealModel { /// - /// Gets the identifier. + /// Gets or sets the identifier. /// /// /// The identifier. /// [JsonProperty("id")] - public string Id - { - get - { - var date = this.Date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture); - var id = $"{date}-{this.Restaurant}"; - return id; - } - } + public string Id { get; set; } /// /// Gets or sets the correlation identifier. diff --git a/PlanB.Butler.Services/PlanB.Butler.Services/Models/RestaurantModel.cs b/PlanB.Butler.Services/PlanB.Butler.Services/Models/RestaurantModel.cs index eacc708..39acb14 100644 --- a/PlanB.Butler.Services/PlanB.Butler.Services/Models/RestaurantModel.cs +++ b/PlanB.Butler.Services/PlanB.Butler.Services/Models/RestaurantModel.cs @@ -16,7 +16,7 @@ namespace PlanB.Butler.Services.Models public class RestaurantModel { /// - /// Gets the identifier. + /// Gets or sets the identifier. /// /// /// The identifier. @@ -24,11 +24,7 @@ public class RestaurantModel [JsonProperty("id")] public string Id { - get - { - var id = $"{this.Name}-{this.City}"; - return id; - } + get; set; } ///