Skip to content

Commit

Permalink
Merge pull request #121 from PlanBGmbH/master
Browse files Browse the repository at this point in the history
Merge
  • Loading branch information
MarkusMeyer13 authored Mar 2, 2020
2 parents c70b60f + 3400b12 commit 58a0790
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ public interface IMealService
/// <returns>True or false.</returns>
Task<bool> CreateMeal(MealViewModel meal);

/// <summary>
/// Updates the meal.
/// </summary>
/// <param name="meal">The meal.</param>
/// <returns>Meal.</returns>
Task<MealViewModel> UpdateMeal(MealViewModel meal);

/// <summary>
/// Gets the meal.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,8 @@ public async Task<IActionResult> Edit(string id, [Bind("Id,CorrelationId,Date,Pr

if (this.ModelState.IsValid)
{
//try
//{
// _context.Update(movie);
// await _context.SaveChangesAsync();
//}
//catch (DbUpdateConcurrencyException)
//{
// if (!MovieExists(movie.Id))
// {
// return NotFound();
// }
// else
// {
// throw;
// }
//}

var result = await this.mealService.UpdateMeal(meal);
return this.RedirectToAction(nameof(this.Index));
}

Expand Down
29 changes: 29 additions & 0 deletions PlanB.Butler.Admin/PlanB.Butler.Admin/Services/MealService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,34 @@ public async Task<List<MealViewModel>> GetMeals()
var meals = JsonConvert.DeserializeObject<List<MealViewModel>>(responseString);
return meals;
}

/// <summary>
/// Updates the meal.
/// </summary>
/// <param name="meal">The meal.</param>
/// <returns>
/// Meal.
/// </returns>
public async Task<MealViewModel> UpdateMeal(MealViewModel meal)
{
Guid correlationId = Guid.NewGuid();
meal.CorrelationId = correlationId;
var json = JsonConvert.SerializeObject(meal);
StringContent content = Util.CreateStringContent(json, correlationId, null);
var uri = this.config["MealsUri"].TrimEnd('/') + "/" + meal.Id;

HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Put, uri)
{
Content = content,
};
httpRequestMessage.Headers.Clear();
Util.AddDefaultEsbHeaders(httpRequestMessage, correlationId, this.config["FunctionsKey"]);
var result = await this.httpClient.SendAsync(httpRequestMessage);
result.EnsureSuccessStatusCode();
var responseString = await result.Content.ReadAsStringAsync();

var updatedMeal = JsonConvert.DeserializeObject<MealViewModel>(responseString);
return updatedMeal;
}
}
}
85 changes: 85 additions & 0 deletions PlanB.Butler.Services/PlanB.Butler.Services/MealService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,91 @@ public static async Task<IActionResult> CreateMeal(
return actionResult;
}

/// <summary>
/// Updates the meal by identifier.
/// </summary>
/// <param name="req">The req.</param>
/// <param name="id">The identifier.</param>
/// <param name="existingContent">The BLOB.</param>
/// <param name="cloudBlobContainer">The cloud BLOB container.</param>
/// <param name="log">The log.</param>
/// <param name="context">The context.</param>
/// <returns>IActionResult.</returns>
[FunctionName("UpdateMealById")]
public static async Task<IActionResult> UpdateMealById(
[HttpTrigger(AuthorizationLevel.Function, "put", Route = "meals/{id}")] HttpRequest req,
string id,
[Blob("meals/{id}.json", FileAccess.ReadWrite, Connection = "StorageSend")] string existingContent,
[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<string, string>();
EventId eventId = new EventId(correlationId.GetHashCode(), Constants.ButlerCorrelationTraceName);
IActionResult actionResult = null;

MealModel mealModel = null;

try
{
trace.Add(Constants.ButlerCorrelationTraceName, correlationId.ToString());
trace.Add("id", id);
mealModel = JsonConvert.DeserializeObject<MealModel>(existingContent);

var date = mealModel.Date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);

var filename = $"{date}-{mealModel.Restaurant}.json";
trace.Add($"filename", filename);
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
trace.Add("requestBody", requestBody);
mealModel = JsonConvert.DeserializeObject<MealModel>(requestBody);

req.HttpContext.Response.Headers.Add(Constants.ButlerCorrelationTraceHeader, correlationId.ToString());

CloudBlockBlob blob = cloudBlobContainer.GetBlockBlobReference($"{filename}");
if (blob != null)
{
blob.Properties.ContentType = "application/json";
var metaDate = mealModel.Date.ToString("yyyyMMdd", CultureInfo.InvariantCulture);
blob.Metadata.Add(MetaDate, metaDate);
blob.Metadata.Add(MetaRestaurant, mealModel.Restaurant);
blob.Metadata.Add(Constants.ButlerCorrelationTraceName, correlationId.ToString().Replace("-", string.Empty));
var meal = JsonConvert.SerializeObject(mealModel);
trace.Add("meal", meal);

Task task = blob.UploadTextAsync(meal);
task.Wait();
}

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()
{
CorrelationId = correlationId,
Details = e.StackTrace,
Message = e.Message,
};
actionResult = new BadRequestObjectResult(mealModel);
}
finally
{
log.LogTrace(eventId, $"'{methodName}' - finished");
log.LogInformation(correlationId, $"'{methodName}' - finished", trace);
}

return actionResult;
}

/// <summary>
/// Reads the meals.
/// </summary>
Expand Down

0 comments on commit 58a0790

Please sign in to comment.