Skip to content

Commit

Permalink
Merge pull request #127 from MarkusMeyer13/master
Browse files Browse the repository at this point in the history
Merge
  • Loading branch information
MarkusMeyer13 authored Mar 3, 2020
2 parents 548e4e9 + 647ce03 commit 4d05912
Show file tree
Hide file tree
Showing 9 changed files with 304 additions and 205 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
using Newtonsoft.Json;
using PlanB.Butler.Services.Extensions;

namespace PlanB.Butler.Services
namespace PlanB.Butler.Services.Controllers
{
/// <summary>
/// Finance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,7 +22,7 @@
using PlanB.Butler.Services.Extensions;
using PlanB.Butler.Services.Models;

namespace PlanB.Butler.Services
namespace PlanB.Butler.Services.Controllers
{
/// <summary>
/// MealService.
Expand Down Expand Up @@ -82,14 +83,16 @@ public static async Task<IActionResult> 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";
Expand Down Expand Up @@ -261,96 +264,98 @@ public static async Task<IActionResult> GetMeals(
IActionResult actionResult = null;

List<MealModel> meals = new List<MealModel>();

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<IListBlobItem> cloudBlockBlobs = new List<IListBlobItem>();
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<IListBlobItem> cloudBlockBlobs = new List<IListBlobItem>();
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<MealModel>(await blobContent);
meals.Add(blobMeal);
var isDateInRange = IsDateInRange(start, end, mealDate);
if (isDateInRange)
{
var blobContent = blob.DownloadTextAsync();
var blobMeal = JsonConvert.DeserializeObject<MealModel>(await blobContent);
meals.Add(blobMeal);
}
}
}
}
else
{
var content = blob.DownloadTextAsync();
var meal = JsonConvert.DeserializeObject<MealModel>(await content);
meals.Add(meal);
}
}
else
{
var content = blob.DownloadTextAsync();
var meal = JsonConvert.DeserializeObject<MealModel>(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;
Expand Down Expand Up @@ -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<MealModel>(blob);
try
{
trace.Add(Constants.ButlerCorrelationTraceName, correlationId.ToString());
trace.Add("id", id);
mealModel = JsonConvert.DeserializeObject<MealModel>(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;
Expand Down Expand Up @@ -540,5 +547,18 @@ internal static bool Validate(MealModel mealModel, Guid correlationId, out Error

return isValid;
}

/// <summary>
/// Creates the name of the file.
/// </summary>
/// <param name="model">The model.</param>
/// <returns>FileName without extension.</returns>
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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
using PlanB.Butler.Services.Extensions;
using PlanB.Butler.Services.Models;

namespace PlanB.Butler.Services
namespace PlanB.Butler.Services.Controllers
{
/// <summary>
/// OrderService.
Expand Down
Loading

0 comments on commit 4d05912

Please sign in to comment.