Skip to content

Commit

Permalink
#66 Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkusMeyer13 committed Mar 3, 2020
1 parent 26feaec commit 2dcdfbe
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 188 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 @@ -21,7 +21,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 @@ -261,96 +261,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 +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<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
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 2dcdfbe

Please sign in to comment.