From 0a1a8a3033c50efb5785dbe267080ed72d9295d4 Mon Sep 17 00:00:00 2001 From: Simon Schneider Date: Mon, 30 Mar 2020 13:48:40 +0200 Subject: [PATCH] #140 Delete function and postalCode fix --- .../Contracts/IRestaurantService.cs | 11 +++- .../Controllers/RestaurantController.cs | 41 ++++++++++++- .../Models/RestaurantViewModel.cs | 30 ++++++---- .../Services/RestaurantService.cs | 24 +++++++- .../Views/Restaurant/Delete.cshtml | 57 +++++++++++++++++++ .../Views/Restaurant/Index.cshtml | 2 +- 6 files changed, 144 insertions(+), 21 deletions(-) create mode 100644 PlanB.Butler.Admin/PlanB.Butler.Admin/Views/Restaurant/Delete.cshtml diff --git a/PlanB.Butler.Admin/PlanB.Butler.Admin/Contracts/IRestaurantService.cs b/PlanB.Butler.Admin/PlanB.Butler.Admin/Contracts/IRestaurantService.cs index 8d2009f..31d93dd 100644 --- a/PlanB.Butler.Admin/PlanB.Butler.Admin/Contracts/IRestaurantService.cs +++ b/PlanB.Butler.Admin/PlanB.Butler.Admin/Contracts/IRestaurantService.cs @@ -24,10 +24,10 @@ public interface IRestaurantService /// /// The meal. /// True or false. - Task CreateRestaurant(RestaurantViewModel restaurant); + Task CreateRestaurant(RestaurantViewModel restaurant); /// - /// Updates the restaurant. + /// Updates the restaurant. /// /// The restuarant. /// Restaurant. @@ -39,5 +39,12 @@ public interface IRestaurantService /// The identifier. /// Restaurant by Id. Task GetRestaurant(string id); + + /// + /// Deletes the meal. + /// + /// The identifier. + /// Meal by Id. + Task DeleteRestaurant(string id); } } diff --git a/PlanB.Butler.Admin/PlanB.Butler.Admin/Controllers/RestaurantController.cs b/PlanB.Butler.Admin/PlanB.Butler.Admin/Controllers/RestaurantController.cs index 0f3d521..e7be729 100644 --- a/PlanB.Butler.Admin/PlanB.Butler.Admin/Controllers/RestaurantController.cs +++ b/PlanB.Butler.Admin/PlanB.Butler.Admin/Controllers/RestaurantController.cs @@ -19,7 +19,7 @@ namespace PlanB.Butler.Admin.Controllers public class RestaurantController : Controller { /// GET: / - private IRestaurantService restaurantService; + private readonly IRestaurantService restaurantService; /// /// Initializes a new instance of the class. @@ -53,7 +53,7 @@ public IActionResult Create() /// Meal. [HttpPost] [ValidateAntiForgeryToken] - public async Task Create([Bind("Id,CorrelationId,Date,Price,Name,Restaurant")] Models.RestaurantViewModel restaurant) + public async Task Create([Bind("Id,CorrelationId,Date,Price,Name,Restaurant,PhoneNumber,City, Street, PostalCode, Url, Email")] Models.RestaurantViewModel restaurant) { if (this.ModelState.IsValid) { @@ -72,7 +72,7 @@ public async Task Create([Bind("Id,CorrelationId,Date,Price,Name, /// IActionResult. [HttpPost] [ValidateAntiForgeryToken] - public async Task Edit(string id, [Bind("Id,CorrelationId,Date,Price,Name,Restaurant,PhoneNumber,City")] Models.RestaurantViewModel restaurant) + public async Task Edit(string id, [Bind("Id,CorrelationId,Date,Price,Name,Restaurant,PhoneNumber,City, Street, PostalCode, Url, Email")] Models.RestaurantViewModel restaurant) { if (id != restaurant.Id) { @@ -110,6 +110,41 @@ public async Task Edit(string id) return this.View(restaurant); } + /// + /// Deletes the specified identifier. + /// + /// The identifier. + /// IActionResult. + public async Task Delete(string id) + { + if (string.IsNullOrEmpty(id)) + { + return this.NotFound(); + } + + var restaurant = await this.restaurantService.GetRestaurant(id); + + if (restaurant == null) + { + return this.NotFound(); + } + + return this.View(restaurant); + } + + /// + /// Deletes the confirmed. + /// + /// The identifier. + /// IActionResult. + [HttpPost] + [ActionName("Delete")] + [ValidateAntiForgeryToken] + public async Task DeleteConfirmed(string id) + { + await this.restaurantService.DeleteRestaurant(id); + return this.RedirectToAction(nameof(this.Index)); + } } } diff --git a/PlanB.Butler.Admin/PlanB.Butler.Admin/Models/RestaurantViewModel.cs b/PlanB.Butler.Admin/PlanB.Butler.Admin/Models/RestaurantViewModel.cs index e3a1859..61f9330 100644 --- a/PlanB.Butler.Admin/PlanB.Butler.Admin/Models/RestaurantViewModel.cs +++ b/PlanB.Butler.Admin/PlanB.Butler.Admin/Models/RestaurantViewModel.cs @@ -39,12 +39,30 @@ public string Id [JsonProperty("url")] public Uri Url { get; set; } + /// + /// Gets or sets the correlationid. + /// + /// + /// The correlationid. + /// [JsonProperty("correlationid")] public Guid? CorrelationId { get; set; } + /// + /// Gets or sets the name. + /// + /// + /// The name. + /// [JsonProperty("name")] public string Name { get; set; } + /// + /// Gets or sets the street. + /// + /// + /// The street. + /// [JsonProperty("street")] public string Street { get; set; } @@ -83,17 +101,5 @@ public string Id /// [JsonProperty("emailAddress")] public string EmailAddress { get; set; } - - /// - /// Gets or sets the date. - /// - /// - /// The date. - /// - [DisplayFormat(DataFormatString = "{0:dd.MM.yyyy}")] - [DataType(DataType.Date)] - [JsonProperty("date")] - public DateTime Date { get; set; } - } } diff --git a/PlanB.Butler.Admin/PlanB.Butler.Admin/Services/RestaurantService.cs b/PlanB.Butler.Admin/PlanB.Butler.Admin/Services/RestaurantService.cs index 9014d4e..803fa13 100644 --- a/PlanB.Butler.Admin/PlanB.Butler.Admin/Services/RestaurantService.cs +++ b/PlanB.Butler.Admin/PlanB.Butler.Admin/Services/RestaurantService.cs @@ -47,7 +47,7 @@ public RestaurantService(HttpClient httpClient, IConfiguration configuration) /// /// True or false. /// - public async Task CreateRestaurant(RestaurantViewModel restaurant) + public async Task CreateRestaurant(RestaurantViewModel restaurant) { Guid correlationId = Guid.NewGuid(); restaurant.CorrelationId = correlationId; @@ -63,8 +63,9 @@ public async Task CreateRestaurant(RestaurantViewModel restaurant) Util.AddDefaultEsbHeaders(httpRequestMessage, correlationId, this.config["FunctionsKey"]); var result = await this.httpClient.SendAsync(httpRequestMessage); result.EnsureSuccessStatusCode(); - var success = result.IsSuccessStatusCode; - return success; + + RestaurantViewModel responseModel = JsonConvert.DeserializeObject(result.Content.ReadAsStringAsync().Result); + return responseModel; } /// @@ -133,5 +134,22 @@ public async Task UpdateRestaurant(RestaurantViewModel rest var updatedRestaurant = JsonConvert.DeserializeObject(responseString); return updatedRestaurant; } + + /// + /// Deletes the restaurant. + /// + /// The identifier. + /// + /// Succes or failure. + /// + public async Task DeleteRestaurant(string id) + { + var uri = this.config["RestaurantUri"].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/Restaurant/Delete.cshtml b/PlanB.Butler.Admin/PlanB.Butler.Admin/Views/Restaurant/Delete.cshtml new file mode 100644 index 0000000..0573424 --- /dev/null +++ b/PlanB.Butler.Admin/PlanB.Butler.Admin/Views/Restaurant/Delete.cshtml @@ -0,0 +1,57 @@ +@model PlanB.Butler.Admin.Models.RestaurantViewModel + +@{ + ViewData["Title"] = "Delete"; +} + +

Delete

+ +

Are you sure you want to delete this?

+
+

MealViewModel

+
+
+
+ @Html.DisplayNameFor(model => model.City) +
+
+ @Html.DisplayFor(model => model.City) +
+
+ @Html.DisplayNameFor(model => model.PostalCode) +
+
+ @Html.DisplayFor(model => model.PostalCode) +
+ +
+ @Html.DisplayNameFor(model => model.Name) +
+
+ @Html.DisplayFor(model => model.Name) +
+
+ @Html.DisplayNameFor(model => model.Street) +
+
+ @Html.DisplayFor(model => model.Street) +
+
+ @Html.DisplayNameFor(model => model.EmailAddress) +
+
+ @Html.DisplayFor(model => model.EmailAddress) +
+
+ @Html.DisplayNameFor(model => model.PhoneNumber) +
+
+ @Html.DisplayFor(model => model.PhoneNumber) +
+
+ +
+ | + Back to List +
+
diff --git a/PlanB.Butler.Admin/PlanB.Butler.Admin/Views/Restaurant/Index.cshtml b/PlanB.Butler.Admin/PlanB.Butler.Admin/Views/Restaurant/Index.cshtml index f0d89ac..a79a43b 100644 --- a/PlanB.Butler.Admin/PlanB.Butler.Admin/Views/Restaurant/Index.cshtml +++ b/PlanB.Butler.Admin/PlanB.Butler.Admin/Views/Restaurant/Index.cshtml @@ -54,7 +54,7 @@ @Html.DisplayFor(modelItem => item.EmailAddress) - @Html.DisplayNameFor(model => model.PostalCode) + @Html.DisplayFor(modelItem => item.PostalCode) Edit