diff --git a/PlanB.Butler.Admin/PlanB.Butler.Admin/Contracts/IMealService.cs b/PlanB.Butler.Admin/PlanB.Butler.Admin/Contracts/IMealService.cs index eb05bf5..bf6306f 100644 --- a/PlanB.Butler.Admin/PlanB.Butler.Admin/Contracts/IMealService.cs +++ b/PlanB.Butler.Admin/PlanB.Butler.Admin/Contracts/IMealService.cs @@ -23,8 +23,8 @@ public interface IMealService /// Creates the meal. /// /// The meal. - /// True or false. - Task CreateMeal(MealViewModel meal); + /// Meal. + Task CreateMeal(MealViewModel meal); /// /// Updates the meal. @@ -40,5 +40,11 @@ public interface IMealService /// Meal by Id. Task GetMeal(string id); + /// + /// Deletes the meal. + /// + /// The identifier. + /// Succes or failure. + Task DeleteMeal(string id); } } diff --git a/PlanB.Butler.Admin/PlanB.Butler.Admin/Controllers/MealController.cs b/PlanB.Butler.Admin/PlanB.Butler.Admin/Controllers/MealController.cs index 48ff4f7..06f0ef6 100644 --- a/PlanB.Butler.Admin/PlanB.Butler.Admin/Controllers/MealController.cs +++ b/PlanB.Butler.Admin/PlanB.Butler.Admin/Controllers/MealController.cs @@ -81,7 +81,6 @@ public async Task 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)); } @@ -94,7 +93,7 @@ public async Task Edit(string id, [Bind("Id,CorrelationId,Date,Pr /// /// The identifier. /// Meal. - public async Task Edit(string? id) + public async Task Edit(string id) { if (string.IsNullOrEmpty(id)) { @@ -110,5 +109,42 @@ public async Task Edit(string? id) return this.View(meal); } + + /// + /// Deletes the specified identifier. + /// + /// The identifier. + /// IActionResult. + public async Task 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); + } + + /// + /// Deletes the confirmed. + /// + /// The identifier. + /// IActionResult. + [HttpPost] + [ActionName("Delete")] + [ValidateAntiForgeryToken] + public async Task DeleteConfirmed(string id) + { + await this.mealService.DeleteMeal(id); + + return this.RedirectToAction(nameof(this.Index)); + } } } diff --git a/PlanB.Butler.Admin/PlanB.Butler.Admin/Services/MealService.cs b/PlanB.Butler.Admin/PlanB.Butler.Admin/Services/MealService.cs index 715a2cf..5d0e706 100644 --- a/PlanB.Butler.Admin/PlanB.Butler.Admin/Services/MealService.cs +++ b/PlanB.Butler.Admin/PlanB.Butler.Admin/Services/MealService.cs @@ -47,7 +47,7 @@ public MealService(HttpClient httpClient, IConfiguration configuration) /// /// True or false. /// - public async Task CreateMeal(MealViewModel meal) + public async Task CreateMeal(MealViewModel meal) { Guid correlationId = Guid.NewGuid(); meal.CorrelationId = correlationId; @@ -63,8 +63,26 @@ public async Task 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(result.Content.ReadAsStringAsync().Result); + return responseModel; + } + + /// + /// Deletes the meal. + /// + /// The identifier. + /// + /// Succes or failure. + /// + public async Task 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; } /// diff --git a/PlanB.Butler.Admin/PlanB.Butler.Admin/Views/Meal/Delete.cshtml b/PlanB.Butler.Admin/PlanB.Butler.Admin/Views/Meal/Delete.cshtml new file mode 100644 index 0000000..64973a3 --- /dev/null +++ b/PlanB.Butler.Admin/PlanB.Butler.Admin/Views/Meal/Delete.cshtml @@ -0,0 +1,50 @@ +@model PlanB.Butler.Admin.Models.MealViewModel + +@{ + ViewData["Title"] = "Delete"; +} + +

Delete

+ +

Are you sure you want to delete this?

+
+

MealViewModel

+
+
+
+ @Html.DisplayNameFor(model => model.Id) +
+
+ @Html.DisplayFor(model => model.Id) +
+
+ @Html.DisplayNameFor(model => model.Date) +
+
+ @Html.DisplayFor(model => model.Date) +
+
+ @Html.DisplayNameFor(model => model.Price) +
+
+ @Html.DisplayFor(model => model.Price) +
+
+ @Html.DisplayNameFor(model => model.Name) +
+
+ @Html.DisplayFor(model => model.Name) +
+
+ @Html.DisplayNameFor(model => model.Restaurant) +
+
+ @Html.DisplayFor(model => model.Restaurant) +
+
+ +
+ | + Back to List +
+
diff --git a/PlanB.Butler.Services/PlanB.Butler.Services/Controllers/MealService.cs b/PlanB.Butler.Services/PlanB.Butler.Services/Controllers/MealService.cs index 0d26c25..01bdb4e 100644 --- a/PlanB.Butler.Services/PlanB.Butler.Services/Controllers/MealService.cs +++ b/PlanB.Butler.Services/PlanB.Butler.Services/Controllers/MealService.cs @@ -140,6 +140,73 @@ public static async Task CreateMeal( return actionResult; } + /// + /// Deletes the meal by identifier. + /// + /// The req. + /// The identifier. + /// The BLOB. + /// The log. + /// The context. + /// IActionResult. + [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(); + 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; + } + /// /// Updates the meal by identifier. ///