From 39942fee29dff1dc47ddb754aa9e36ffba7e2fda Mon Sep 17 00:00:00 2001 From: Simon Schneider Date: Tue, 31 Mar 2020 13:11:10 +0200 Subject: [PATCH] #140 Edit and Delete functions working --- .../Contracts/IRestaurantService.cs | 2 +- .../Controllers/RestaurantController.cs | 11 +++- .../Services/RestaurantService.cs | 15 +++-- .../Views/Meal/Index.cshtml | 55 +++++++++++-------- .../Views/Restaurant/Index.cshtml | 53 +++++++++--------- .../Controllers/MealService.cs | 2 +- .../Controllers/RestaurantService.cs | 14 ++--- .../Models/RestaurantModel.cs | 8 +-- 8 files changed, 86 insertions(+), 74 deletions(-) diff --git a/PlanB.Butler.Admin/PlanB.Butler.Admin/Contracts/IRestaurantService.cs b/PlanB.Butler.Admin/PlanB.Butler.Admin/Contracts/IRestaurantService.cs index 31d93dd..8d4643f 100644 --- a/PlanB.Butler.Admin/PlanB.Butler.Admin/Contracts/IRestaurantService.cs +++ b/PlanB.Butler.Admin/PlanB.Butler.Admin/Contracts/IRestaurantService.cs @@ -17,7 +17,7 @@ public interface IRestaurantService /// Gets the restaurant. /// /// Restaurant. - Task> GetRestaurant(); + Task> GetRestaurants(); /// /// Creates the meal. diff --git a/PlanB.Butler.Admin/PlanB.Butler.Admin/Controllers/RestaurantController.cs b/PlanB.Butler.Admin/PlanB.Butler.Admin/Controllers/RestaurantController.cs index e7be729..e3b6622 100644 --- a/PlanB.Butler.Admin/PlanB.Butler.Admin/Controllers/RestaurantController.cs +++ b/PlanB.Butler.Admin/PlanB.Butler.Admin/Controllers/RestaurantController.cs @@ -9,6 +9,7 @@ using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; using PlanB.Butler.Admin.Contracts; +using PlanB.Butler.Admin.Models; namespace PlanB.Butler.Admin.Controllers { @@ -18,6 +19,9 @@ namespace PlanB.Butler.Admin.Controllers /// public class RestaurantController : Controller { + /// + /// restaurantService + /// /// GET: / private readonly IRestaurantService restaurantService; @@ -33,7 +37,7 @@ public class RestaurantController : Controller /// Index. public IActionResult Index() { - var restaurant = this.restaurantService.GetRestaurant().Result; + var restaurant = this.restaurantService.GetRestaurants().Result; return this.View(restaurant); } @@ -53,7 +57,7 @@ public IActionResult Create() /// Meal. [HttpPost] [ValidateAntiForgeryToken] - public async Task Create([Bind("Id,CorrelationId,Date,Price,Name,Restaurant,PhoneNumber,City, Street, PostalCode, Url, Email")] Models.RestaurantViewModel restaurant) + public async Task Create([Bind("Id, CorrelationId, Date,Price,Name,Restaurant,PhoneNumber,City, Street, PostalCode, Url, EmailAddress")] RestaurantViewModel restaurant) { if (this.ModelState.IsValid) { @@ -72,7 +76,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, Street, PostalCode, Url, Email")] Models.RestaurantViewModel restaurant) + public async Task Edit(string id, [Bind("Id,CorrelationId, Date,Price,Name,Restaurant,PhoneNumber,City, Street, PostalCode, Url, EmailAddress")] RestaurantViewModel restaurant) { if (id != restaurant.Id) { @@ -84,6 +88,7 @@ public async Task Edit(string id, [Bind("Id,CorrelationId,Date,Pr if (this.ModelState.IsValid) { var result = await this.restaurantService.UpdateRestaurant(restaurant); + return this.RedirectToAction(nameof(this.Index)); } return this.View(restaurant); diff --git a/PlanB.Butler.Admin/PlanB.Butler.Admin/Services/RestaurantService.cs b/PlanB.Butler.Admin/PlanB.Butler.Admin/Services/RestaurantService.cs index 803fa13..6c2d02e 100644 --- a/PlanB.Butler.Admin/PlanB.Butler.Admin/Services/RestaurantService.cs +++ b/PlanB.Butler.Admin/PlanB.Butler.Admin/Services/RestaurantService.cs @@ -41,13 +41,13 @@ public RestaurantService(HttpClient httpClient, IConfiguration configuration) } /// - /// Creates the meal. + /// Creates the restaurant. /// /// The restaurant. /// /// True or false. /// - public async Task CreateRestaurant(RestaurantViewModel restaurant) + public async Task CreateRestaurant(RestaurantViewModel restaurant) { Guid correlationId = Guid.NewGuid(); restaurant.CorrelationId = correlationId; @@ -83,11 +83,9 @@ public async Task GetRestaurant(string id) httpRequestMessage.Headers.Clear(); Util.AddDefaultEsbHeaders(httpRequestMessage, correlationId, this.config["FunctionsKey"]); var result = await this.httpClient.SendAsync(httpRequestMessage); - result.EnsureSuccessStatusCode(); - var body = result.Content.ReadAsStringAsync().Result; - - var restaurant = JsonConvert.DeserializeObject(body); + var restaurant = JsonConvert.DeserializeObject(body); + result.EnsureSuccessStatusCode(); return restaurant; } @@ -97,7 +95,7 @@ public async Task GetRestaurant(string id) /// /// Restaurant. /// - public async Task> GetRestaurant() + public async Task> GetRestaurants() { var uri = this.config["RestaurantUri"]; this.httpClient.DefaultRequestHeaders.Add(Constants.FunctionsKeyHeader, this.config["FunctionsKey"]); @@ -129,8 +127,9 @@ public async Task UpdateRestaurant(RestaurantViewModel rest httpRequestMessage.Headers.Clear(); Util.AddDefaultEsbHeaders(httpRequestMessage, correlationId, this.config["FunctionsKey"]); var result = await this.httpClient.SendAsync(httpRequestMessage); - var responseString = await result.Content.ReadAsStringAsync(); result.EnsureSuccessStatusCode(); + var responseString = await result.Content.ReadAsStringAsync(); + var updatedRestaurant = JsonConvert.DeserializeObject(responseString); return updatedRestaurant; } diff --git a/PlanB.Butler.Admin/PlanB.Butler.Admin/Views/Meal/Index.cshtml b/PlanB.Butler.Admin/PlanB.Butler.Admin/Views/Meal/Index.cshtml index fefde87..9c999fd 100644 --- a/PlanB.Butler.Admin/PlanB.Butler.Admin/Views/Meal/Index.cshtml +++ b/PlanB.Butler.Admin/PlanB.Butler.Admin/Views/Meal/Index.cshtml @@ -6,11 +6,15 @@

Index

-

- Create New -

+ + +

+ Create New +

+ + + -@foreach (var item in Model) { - - - - - - - -} + @foreach (var item in Model) + { + + + + + + + + }
@Html.DisplayNameFor(model => model.Date) @@ -24,29 +28,32 @@ @Html.DisplayNameFor(model => model.Price) +
- @Html.DisplayFor(modelItem => item.Date) - - @Html.DisplayFor(modelItem => item.Restaurant) - - @Html.DisplayFor(modelItem => item.Name) - - @Html.DisplayFor(modelItem => item.Price) - - Edit | - Details | - Delete -
+ @Html.DisplayFor(modelItem => item.Date) + + @Html.DisplayFor(modelItem => item.Restaurant) + + @Html.DisplayFor(modelItem => item.Name) + + @Html.DisplayFor(modelItem => item.Price) + + Edit | + Details | + Delete +
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 a79a43b..2459db5 100644 --- a/PlanB.Butler.Admin/PlanB.Butler.Admin/Views/Restaurant/Index.cshtml +++ b/PlanB.Butler.Admin/PlanB.Butler.Admin/Views/Restaurant/Index.cshtml @@ -29,7 +29,8 @@ @Html.DisplayNameFor(model => model.PostalCode) - + + @@ -37,31 +38,31 @@ @foreach (var item in Model) { - - - @Html.DisplayFor(modelItem => item.Name) - - - @Html.DisplayFor(modelItem => item.PhoneNumber) - - - @Html.DisplayFor(modelItem => item.Street) - - - @Html.DisplayFor(modelItem => item.City) - - - @Html.DisplayFor(modelItem => item.EmailAddress) - - - @Html.DisplayFor(modelItem => item.PostalCode) - - - Edit - Details - Delete - - + + + @Html.DisplayFor(modelItem => item.Name) + + + @Html.DisplayFor(modelItem => item.PhoneNumber) + + + @Html.DisplayFor(modelItem => item.Street) + + + @Html.DisplayFor(modelItem => item.City) + + + @Html.DisplayFor(modelItem => item.EmailAddress) + + + @Html.DisplayFor(modelItem => item.PostalCode) + + + Edit + Details + Delete + + } diff --git a/PlanB.Butler.Services/PlanB.Butler.Services/Controllers/MealService.cs b/PlanB.Butler.Services/PlanB.Butler.Services/Controllers/MealService.cs index 5cd0511..30d59b6 100644 --- a/PlanB.Butler.Services/PlanB.Butler.Services/Controllers/MealService.cs +++ b/PlanB.Butler.Services/PlanB.Butler.Services/Controllers/MealService.cs @@ -103,7 +103,7 @@ public static async Task CreateMeal( var meal = JsonConvert.SerializeObject(mealModel); trace.Add("meal", meal); - Task task = blob.UploadTextAsync(requestBody); + Task task = blob.UploadTextAsync(meal); task.Wait(); actionResult = new OkObjectResult(mealModel); log.LogInformation(correlationId, $"'{methodName}' - success", trace); diff --git a/PlanB.Butler.Services/PlanB.Butler.Services/Controllers/RestaurantService.cs b/PlanB.Butler.Services/PlanB.Butler.Services/Controllers/RestaurantService.cs index ecb4fcc..e4c2286 100644 --- a/PlanB.Butler.Services/PlanB.Butler.Services/Controllers/RestaurantService.cs +++ b/PlanB.Butler.Services/PlanB.Butler.Services/Controllers/RestaurantService.cs @@ -83,9 +83,9 @@ public static async Task UpdateRestaurantById( trace.Add("id", id); restaurantModel = JsonConvert.DeserializeObject(existingContent); - var date = restaurantModel.Date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture); + //var date = restaurantModel.Date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture); - var filename = $"{date}-{restaurantModel.City}.json"; + var filename = $"{restaurantModel.Name}-{restaurantModel.City}.json"; trace.Add($"filename", filename); string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); trace.Add("requestBody", requestBody); @@ -100,8 +100,8 @@ public static async Task UpdateRestaurantById( if (blob != null) { blob.Properties.ContentType = "application/json"; - var metaDate = restaurantModel.Date.ToString("yyyyMMdd", CultureInfo.InvariantCulture); - blob.Metadata.Add(MetaDate, metaDate); + //var metaDate = restaurantModel.Date.ToString("yyyyMMdd", CultureInfo.InvariantCulture); + //blob.Metadata.Add(MetaDate, metaDate); blob.Metadata.Add(MetaRestaurant, restaurantModel.ToString()); blob.Metadata.Add(Constants.ButlerCorrelationTraceName, correlationId.ToString().Replace("-", string.Empty)); var restaurant = JsonConvert.SerializeObject(restaurantModel); @@ -403,12 +403,12 @@ public static async Task CreateRestaurant( { blob.Properties.ContentType = "application/json"; blob.Metadata.Add(Constants.ButlerCorrelationTraceName, correlationId.ToString().Replace("-", string.Empty)); - blob.Metadata.Add(MetaRestaurant, System.Web.HttpUtility.HtmlEncode(restaurantModel.Name)); - blob.Metadata.Add(MetaCity, System.Web.HttpUtility.HtmlEncode(restaurantModel.City)); + blob.Metadata.Add(MetaRestaurant, HttpUtility.HtmlEncode(restaurantModel.Name)); + blob.Metadata.Add(MetaCity, HttpUtility.HtmlEncode(restaurantModel.City)); var restaurant = JsonConvert.SerializeObject(restaurantModel); trace.Add("restaurant", restaurant); - Task task = blob.UploadTextAsync(requestBody); + Task task = blob.UploadTextAsync(restaurant); task.Wait(); actionResult = new OkObjectResult(restaurantModel); diff --git a/PlanB.Butler.Services/PlanB.Butler.Services/Models/RestaurantModel.cs b/PlanB.Butler.Services/PlanB.Butler.Services/Models/RestaurantModel.cs index aa9a5e2..177c441 100644 --- a/PlanB.Butler.Services/PlanB.Butler.Services/Models/RestaurantModel.cs +++ b/PlanB.Butler.Services/PlanB.Butler.Services/Models/RestaurantModel.cs @@ -91,12 +91,12 @@ public string Id public string EmailAddress { get; set; } /// - /// Gets or sets the date. + /// Gets or sets the correlationid. /// /// - /// The date. + /// The correlationid. /// - [JsonProperty("date")] - public DateTime Date { get; set; } + [JsonProperty("correlationid")] + public Guid? CorrelationId { get; set; } } }