diff --git a/PlanB.Butler.Services/MealService.cs b/PlanB.Butler.Services/MealService.cs
index 703d776..d2c4915 100644
--- a/PlanB.Butler.Services/MealService.cs
+++ b/PlanB.Butler.Services/MealService.cs
@@ -3,9 +3,9 @@
using System;
using System.Collections.Generic;
+using System.Globalization;
using System.IO;
using System.Reflection;
-using System.Text;
using System.Threading.Tasks;
using AzureFunctions.Extensions.Swashbuckle.Attribute;
@@ -14,6 +14,7 @@
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
+using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Newtonsoft.Json;
using PlanB.Butler.Services.Extensions;
@@ -36,8 +37,8 @@ public static class MealService
///
/// IActionResult.
///
- [FunctionName(nameof(Meals))]
- public static async Task Meals(
+ [FunctionName("CreateMeal")]
+ public static async Task CreateMeal(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = "meals")]
[RequestBodyType(typeof(MealModel), "Meal request")]HttpRequest req,
[Blob("meals", FileAccess.ReadWrite, Connection = "StorageSend")] CloudBlobContainer cloudBlobContainer,
@@ -54,14 +55,29 @@ public static async Task Meals(
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
trace.Add("requestBody", requestBody);
- string filename = correlationId.ToString() + ".json";
+ MealModel mealModel = JsonConvert.DeserializeObject(requestBody);
+ if (mealModel.Id == null || mealModel.Id.Equals(Guid.Empty))
+ {
+ mealModel.Id = correlationId;
+ }
+
+ var date = mealModel.Date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
+
+ var filename = $"{date}-{mealModel.Restaurant}-{correlationId}.json";
trace.Add($"filename", filename);
+
req.HttpContext.Response.Headers.Add(Constants.ButlerCorrelationTraceHeader, correlationId.ToString());
CloudBlockBlob blob = cloudBlobContainer.GetBlockBlobReference($"{filename}");
if (blob != null)
{
blob.Properties.ContentType = "application/json";
+ blob.Metadata.Add("date", date);
+ blob.Metadata.Add("restaurant", mealModel.Restaurant);
+ blob.Metadata.Add(Constants.ButlerCorrelationTraceName, correlationId.ToString());
+ var meal = JsonConvert.SerializeObject(mealModel);
+ trace.Add("meal", meal);
+
Task task = blob.UploadTextAsync(requestBody);
}
@@ -83,5 +99,43 @@ public static async Task Meals(
return new OkResult();
}
+
+ ///
+ /// Reads the meals.
+ ///
+ /// The req.
+ /// The cloud BLOB container.
+ /// The log.
+ /// The context.
+ /// All meals.
+ [ProducesResponseType(StatusCodes.Status200OK)]
+ [FunctionName("GetMeals")]
+ public static async Task ReadMeals(
+ [HttpTrigger(AuthorizationLevel.Function, "get", Route = "meals")] HttpRequest req,
+ [Blob("meals", FileAccess.ReadWrite, Connection = "StorageSend")] CloudBlobContainer cloudBlobContainer,
+ 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);
+
+ BlobContinuationToken blobContinuationToken = null;
+ var options = new BlobRequestOptions();
+ var operationContext = new OperationContext();
+
+ var blobs = await cloudBlobContainer.ListBlobsSegmentedAsync(null, true, BlobListingDetails.All, null, blobContinuationToken, options, operationContext).ConfigureAwait(false);
+ List meals = new List();
+ foreach (var item in blobs.Results)
+ {
+ CloudBlockBlob blob = (CloudBlockBlob)item;
+ var content = blob.DownloadTextAsync();
+ var meal = JsonConvert.DeserializeObject(await content);
+ meals.Add(meal);
+ }
+
+ return (ActionResult)new OkObjectResult(meals);
+ }
}
}