Skip to content

Commit

Permalink
Merge pull request #128 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 4d05912 + 7d8fbf7 commit c21f963
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 7 deletions.
10 changes: 8 additions & 2 deletions PlanB.Butler.Admin/PlanB.Butler.Admin/Contracts/IMealService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public interface IMealService
/// Creates the meal.
/// </summary>
/// <param name="meal">The meal.</param>
/// <returns>True or false.</returns>
Task<bool> CreateMeal(MealViewModel meal);
/// <returns>Meal.</returns>
Task<MealViewModel> CreateMeal(MealViewModel meal);

/// <summary>
/// Updates the meal.
Expand All @@ -40,5 +40,11 @@ public interface IMealService
/// <returns>Meal by Id.</returns>
Task<MealViewModel> GetMeal(string id);

/// <summary>
/// Deletes the meal.
/// </summary>
/// <param name="id">The identifier.</param>
/// <returns>Succes or failure.</returns>
Task<bool> DeleteMeal(string id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ public async Task<IActionResult> Edit(string id, [Bind("Id,CorrelationId,Date,Pr

if (this.ModelState.IsValid)
{

var result = await this.mealService.UpdateMeal(meal);
return this.RedirectToAction(nameof(this.Index));
}
Expand All @@ -94,7 +93,7 @@ public async Task<IActionResult> Edit(string id, [Bind("Id,CorrelationId,Date,Pr
/// </summary>
/// <param name="id">The identifier.</param>
/// <returns>Meal.</returns>
public async Task<IActionResult> Edit(string? id)
public async Task<IActionResult> Edit(string id)
{
if (string.IsNullOrEmpty(id))
{
Expand All @@ -110,5 +109,42 @@ public async Task<IActionResult> Edit(string? id)

return this.View(meal);
}

/// <summary>
/// Deletes the specified identifier.
/// </summary>
/// <param name="id">The identifier.</param>
/// <returns>IActionResult.</returns>
public async Task<IActionResult> Delete(string id)
{
if (string.IsNullOrEmpty(id))
{
return this.NotFound();
}

var meal = await this.mealService.GetMeal(id);

if (meal == null)
{
return this.NotFound();
}

return this.View(meal);
}

/// <summary>
/// Deletes the confirmed.
/// </summary>
/// <param name="id">The identifier.</param>
/// <returns>IActionResult.</returns>
[HttpPost]
[ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(string id)
{
await this.mealService.DeleteMeal(id);

return this.RedirectToAction(nameof(this.Index));
}
}
}
24 changes: 21 additions & 3 deletions PlanB.Butler.Admin/PlanB.Butler.Admin/Services/MealService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public MealService(HttpClient httpClient, IConfiguration configuration)
/// <returns>
/// True or false.
/// </returns>
public async Task<bool> CreateMeal(MealViewModel meal)
public async Task<MealViewModel> CreateMeal(MealViewModel meal)
{
Guid correlationId = Guid.NewGuid();
meal.CorrelationId = correlationId;
Expand All @@ -63,8 +63,26 @@ public async Task<bool> CreateMeal(MealViewModel meal)
Util.AddDefaultEsbHeaders(httpRequestMessage, correlationId, this.config["FunctionsKey"]);
var result = await this.httpClient.SendAsync(httpRequestMessage);
result.EnsureSuccessStatusCode();
var success = result.IsSuccessStatusCode;
return success;

MealViewModel responseModel = JsonConvert.DeserializeObject<MealViewModel>(result.Content.ReadAsStringAsync().Result);
return responseModel;
}

/// <summary>
/// Deletes the meal.
/// </summary>
/// <param name="id">The identifier.</param>
/// <returns>
/// Succes or failure.
/// </returns>
public async Task<bool> DeleteMeal(string id)
{
var uri = this.config["MealsUri"].TrimEnd('/') + "/" + id;

this.httpClient.DefaultRequestHeaders.Add(Constants.FunctionsKeyHeader, this.config["FunctionsKey"]);
var response = await this.httpClient.DeleteAsync(uri);

return response.IsSuccessStatusCode;
}

/// <summary>
Expand Down
50 changes: 50 additions & 0 deletions PlanB.Butler.Admin/PlanB.Butler.Admin/Views/Meal/Delete.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
@model PlanB.Butler.Admin.Models.MealViewModel

@{
ViewData["Title"] = "Delete";
}

<h1>Delete</h1>

<h3>Are you sure you want to delete this?</h3>
<div>
<h4>MealViewModel</h4>
<hr />
<dl class="row">
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.Id)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.Id)
</dd>
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.Date)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.Date)
</dd>
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.Price)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.Price)
</dd>
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.Name)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.Name)
</dd>
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.Restaurant)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.Restaurant)
</dd>
</dl>

<form asp-action="Delete">
<input type="submit" value="Delete" class="btn btn-danger" /> |
<a asp-action="Index">Back to List</a>
</form>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,73 @@ public static async Task<IActionResult> CreateMeal(
return actionResult;
}

/// <summary>
/// Deletes the meal by identifier.
/// </summary>
/// <param name="req">The req.</param>
/// <param name="id">The identifier.</param>
/// <param name="blob">The BLOB.</param>
/// <param name="log">The log.</param>
/// <param name="context">The context.</param>
/// <returns>IActionResult.</returns>
[ProducesResponseType(typeof(ErrorModel), StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status200OK)]
[FunctionName("DeleteMealById")]
public static IActionResult DeleteMealById(
[HttpTrigger(AuthorizationLevel.Function, "delete", Route = "meals/{id}")] HttpRequest req,
string id,
[Blob("meals/{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<string, string>();
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
{
log.LogTrace(eventId, $"'{methodName}' - finished");
log.LogInformation(correlationId, $"'{methodName}' - finished", trace);
}
}

return actionResult;
}

/// <summary>
/// Updates the meal by identifier.
/// </summary>
Expand Down

0 comments on commit c21f963

Please sign in to comment.