From e17496bc7080ea287c58de3cfd6e0c70156ab38d Mon Sep 17 00:00:00 2001 From: Markus Meyer Date: Tue, 3 Mar 2020 05:12:39 +0100 Subject: [PATCH 1/4] #66 Delete Restaurant --- .../RestaurantService.cs | 69 ++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/PlanB.Butler.Services/PlanB.Butler.Services/RestaurantService.cs b/PlanB.Butler.Services/PlanB.Butler.Services/RestaurantService.cs index 61768b4..ee9e892 100644 --- a/PlanB.Butler.Services/PlanB.Butler.Services/RestaurantService.cs +++ b/PlanB.Butler.Services/PlanB.Butler.Services/RestaurantService.cs @@ -156,7 +156,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 { From 26feaeca304f1ece2d3f66885cf80d49d4e4d628 Mon Sep 17 00:00:00 2001 From: Markus Meyer Date: Tue, 3 Mar 2020 05:55:39 +0100 Subject: [PATCH 2/4] #66 Change Restaurant Id --- .../Models/RestaurantModel.cs | 8 ++----- .../RestaurantService.cs | 24 +++++++++++++++---- 2 files changed, 22 insertions(+), 10 deletions(-) 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; } /// diff --git a/PlanB.Butler.Services/PlanB.Butler.Services/RestaurantService.cs b/PlanB.Butler.Services/PlanB.Butler.Services/RestaurantService.cs index ee9e892..3e4078f 100644 --- a/PlanB.Butler.Services/PlanB.Butler.Services/RestaurantService.cs +++ b/PlanB.Butler.Services/PlanB.Butler.Services/RestaurantService.cs @@ -8,7 +8,7 @@ using System.Reflection; using System.Text; using System.Threading.Tasks; - +using System.Web; using AzureFunctions.Extensions.Swashbuckle.Attribute; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; @@ -271,12 +271,16 @@ public static async Task CreateRestaurant( if (isValid) { - var filename = $"{restaurantModel.Id}.json"; - trace.Add($"filename", filename); + 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($"{filename}"); + CloudBlockBlob blob = cloudBlobContainer.GetBlockBlobReference($"{fullFileName}"); if (blob != null) { blob.Properties.ContentType = "application/json"; @@ -377,5 +381,17 @@ internal static bool Validate(RestaurantModel model, Guid correlationId, ILogger return isValid; } + + /// + /// Creates the name of the file. + /// + /// The model. + /// + internal static string CreateFileName(RestaurantModel model) + { + string fileName = $"{model.Name}-{model.City}"; + fileName = HttpUtility.UrlEncode(fileName); + return fileName; + } } } From 2dcdfbe4c1bccbe8a8379389d6579548a1f5429a Mon Sep 17 00:00:00 2001 From: Markus Meyer Date: Tue, 3 Mar 2020 06:05:20 +0100 Subject: [PATCH 3/4] #66 Refactor --- .../MealServiceMockTest.cs | 1 + .../MealServiceTest.cs | 1 + .../RestaurantServiceMockTest.cs | 1 + .../{ => Controllers}/FinanceService.cs | 2 +- .../{ => Controllers}/MealService.cs | 200 +++++++++--------- .../{ => Controllers}/OrderService.cs | 2 +- .../{ => Controllers}/RestaurantService.cs | 181 ++++++++-------- 7 files changed, 200 insertions(+), 188 deletions(-) rename PlanB.Butler.Services/PlanB.Butler.Services/{ => Controllers}/FinanceService.cs (99%) rename PlanB.Butler.Services/PlanB.Butler.Services/{ => Controllers}/MealService.cs (75%) rename PlanB.Butler.Services/PlanB.Butler.Services/{ => Controllers}/OrderService.cs (99%) rename PlanB.Butler.Services/PlanB.Butler.Services/{ => Controllers}/RestaurantService.cs (70%) 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 75% rename from PlanB.Butler.Services/PlanB.Butler.Services/MealService.cs rename to PlanB.Butler.Services/PlanB.Butler.Services/Controllers/MealService.cs index e9dfc89..67d897c 100644 --- a/PlanB.Butler.Services/PlanB.Butler.Services/MealService.cs +++ b/PlanB.Butler.Services/PlanB.Butler.Services/Controllers/MealService.cs @@ -21,7 +21,7 @@ using PlanB.Butler.Services.Extensions; using PlanB.Butler.Services.Models; -namespace PlanB.Butler.Services +namespace PlanB.Butler.Services.Controllers { /// /// MealService. @@ -261,96 +261,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 +386,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; 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 70% rename from PlanB.Butler.Services/PlanB.Butler.Services/RestaurantService.cs rename to PlanB.Butler.Services/PlanB.Butler.Services/Controllers/RestaurantService.cs index 3e4078f..8670425 100644 --- a/PlanB.Butler.Services/PlanB.Butler.Services/RestaurantService.cs +++ b/PlanB.Butler.Services/PlanB.Butler.Services/Controllers/RestaurantService.cs @@ -9,6 +9,7 @@ using System.Text; using System.Threading.Tasks; using System.Web; + using AzureFunctions.Extensions.Swashbuckle.Attribute; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; @@ -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); @@ -259,69 +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 = CreateFileName(restaurantModel); - trace.Add($"fileName", fileName); - restaurantModel.Id = fileName; + trace.Add(Constants.ButlerCorrelationTraceName, correlationId.ToString()); + string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); + trace.Add("requestBody", requestBody); - var fullFileName = $"{fileName}.json"; - trace.Add($"fullFileName", fullFileName); + RestaurantModel restaurantModel = JsonConvert.DeserializeObject(requestBody); - req.HttpContext.Response.Headers.Add(Constants.ButlerCorrelationTraceHeader, correlationId.ToString()); + bool isValid = Validate(restaurantModel, correlationId, log, out ErrorModel errorModel); - CloudBlockBlob blob = cloudBlobContainer.GetBlockBlobReference($"{fullFileName}"); - if (blob != null) + if (isValid) { - 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); + 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; @@ -386,7 +391,7 @@ internal static bool Validate(RestaurantModel model, Guid correlationId, ILogger /// Creates the name of the file. /// /// The model. - /// + /// FileName without extension. internal static string CreateFileName(RestaurantModel model) { string fileName = $"{model.Name}-{model.City}"; From 647ce03e8b587aa582878247f0145cdf39743537 Mon Sep 17 00:00:00 2001 From: Markus Meyer Date: Tue, 3 Mar 2020 06:43:41 +0100 Subject: [PATCH 4/4] #66 Change Meal FileName --- .../Controllers/MealService.cs | 24 +++++++++++++++---- .../PlanB.Butler.Services/Models/MealModel.cs | 12 ++-------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/PlanB.Butler.Services/PlanB.Butler.Services/Controllers/MealService.cs b/PlanB.Butler.Services/PlanB.Butler.Services/Controllers/MealService.cs index 67d897c..0d26c25 100644 --- a/PlanB.Butler.Services/PlanB.Butler.Services/Controllers/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; @@ -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"; @@ -544,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/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.