Skip to content

Commit

Permalink
#140 Edit and Delete functions working
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonSchneider99 committed Mar 31, 2020
1 parent 0a1a8a3 commit 39942fe
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public interface IRestaurantService
/// Gets the restaurant.
/// </summary>
/// <returns>Restaurant.</returns>
Task<List<RestaurantViewModel>> GetRestaurant();
Task<List<RestaurantViewModel>> GetRestaurants();

/// <summary>
/// Creates the meal.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -18,6 +19,9 @@ namespace PlanB.Butler.Admin.Controllers
/// <seealso cref="Microsoft.AspNetCore.Mvc.Controller" />
public class RestaurantController : Controller
{
/// <summary>
/// restaurantService
/// </summary>
/// GET: /<controller>
private readonly IRestaurantService restaurantService;

Expand All @@ -33,7 +37,7 @@ public class RestaurantController : Controller
/// <returns>Index.</returns>
public IActionResult Index()
{
var restaurant = this.restaurantService.GetRestaurant().Result;
var restaurant = this.restaurantService.GetRestaurants().Result;
return this.View(restaurant);
}

Expand All @@ -53,7 +57,7 @@ public IActionResult Create()
/// <returns>Meal.</returns>
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,CorrelationId,Date,Price,Name,Restaurant,PhoneNumber,City, Street, PostalCode, Url, Email")] Models.RestaurantViewModel restaurant)
public async Task<IActionResult> Create([Bind("Id, CorrelationId, Date,Price,Name,Restaurant,PhoneNumber,City, Street, PostalCode, Url, EmailAddress")] RestaurantViewModel restaurant)
{
if (this.ModelState.IsValid)
{
Expand All @@ -72,7 +76,7 @@ public async Task<IActionResult> Create([Bind("Id,CorrelationId,Date,Price,Name,
/// <returns>IActionResult.</returns>
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(string id, [Bind("Id,CorrelationId,Date,Price,Name,Restaurant,PhoneNumber,City, Street, PostalCode, Url, Email")] Models.RestaurantViewModel restaurant)
public async Task<IActionResult> Edit(string id, [Bind("Id,CorrelationId, Date,Price,Name,Restaurant,PhoneNumber,City, Street, PostalCode, Url, EmailAddress")] RestaurantViewModel restaurant)
{
if (id != restaurant.Id)
{
Expand All @@ -84,6 +88,7 @@ public async Task<IActionResult> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ public RestaurantService(HttpClient httpClient, IConfiguration configuration)
}

/// <summary>
/// Creates the meal.
/// Creates the restaurant.
/// </summary>
/// <param name="restaurant">The restaurant.</param>
/// <returns>
/// True or false.
/// </returns>
public async Task <RestaurantViewModel> CreateRestaurant(RestaurantViewModel restaurant)
public async Task<RestaurantViewModel> CreateRestaurant(RestaurantViewModel restaurant)
{
Guid correlationId = Guid.NewGuid();
restaurant.CorrelationId = correlationId;
Expand Down Expand Up @@ -83,11 +83,9 @@ public async Task<RestaurantViewModel> 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<RestaurantViewModel>(body);
var restaurant = JsonConvert.DeserializeObject<RestaurantViewModel>(body);
result.EnsureSuccessStatusCode();
return restaurant;
}

Expand All @@ -97,7 +95,7 @@ public async Task<RestaurantViewModel> GetRestaurant(string id)
/// <returns>
/// Restaurant.
/// </returns>
public async Task<List<RestaurantViewModel>> GetRestaurant()
public async Task<List<RestaurantViewModel>> GetRestaurants()
{
var uri = this.config["RestaurantUri"];
this.httpClient.DefaultRequestHeaders.Add(Constants.FunctionsKeyHeader, this.config["FunctionsKey"]);
Expand Down Expand Up @@ -129,8 +127,9 @@ public async Task<RestaurantViewModel> 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<RestaurantViewModel>(responseString);
return updatedRestaurant;
}
Expand Down
55 changes: 31 additions & 24 deletions PlanB.Butler.Admin/PlanB.Butler.Admin/Views/Meal/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@

<h1>Index</h1>

<p>
<a asp-action="Create">Create New</a>
</p>

<table class="table">
<thead>
<tr>
<p>
<a asp-action="Create">Create New</a>
</p>
</tr>

<tr>
<th>
@Html.DisplayNameFor(model => model.Date)
Expand All @@ -24,29 +28,32 @@
<th>
@Html.DisplayNameFor(model => model.Price)
</th>
<th>
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Date)
</td>
<td>
@Html.DisplayFor(modelItem => item.Restaurant)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
<a asp-action="Details" asp-route-id="@item.Id">Details</a> |
<a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
</td>
</tr>
}
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Date)
</td>
<td>
@Html.DisplayFor(modelItem => item.Restaurant)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
<a asp-action="Details" asp-route-id="@item.Id">Details</a> |
<a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
</td>
</tr>
}
</tbody>
</table>
53 changes: 27 additions & 26 deletions PlanB.Butler.Admin/PlanB.Butler.Admin/Views/Restaurant/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -29,39 +29,40 @@
<th>
@Html.DisplayNameFor(model => model.PostalCode)
</th>

<th>
</th>


</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.PhoneNumber)
</td>
<td>
@Html.DisplayFor(modelItem => item.Street)
</td>
<td>
@Html.DisplayFor(modelItem => item.City)
</td>
<td>
@Html.DisplayFor(modelItem => item.EmailAddress)
</td>
<td>
@Html.DisplayFor(modelItem => item.PostalCode)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.Id">Edit</a>
<a asp-action="Details" asp-route-id="@item.Id">Details</a>
<a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
</td>
</tr>
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.PhoneNumber)
</td>
<td>
@Html.DisplayFor(modelItem => item.Street)
</td>
<td>
@Html.DisplayFor(modelItem => item.City)
</td>
<td>
@Html.DisplayFor(modelItem => item.EmailAddress)
</td>
<td>
@Html.DisplayFor(modelItem => item.PostalCode)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.Id">Edit</a>
<a asp-action="Details" asp-route-id="@item.Id">Details</a>
<a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
</td>
</tr>
}
</tbody>
</table>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public static async Task<IActionResult> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ public static async Task<IActionResult> UpdateRestaurantById(
trace.Add("id", id);
restaurantModel = JsonConvert.DeserializeObject<RestaurantModel>(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);
Expand All @@ -100,8 +100,8 @@ public static async Task<IActionResult> 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);
Expand Down Expand Up @@ -403,12 +403,12 @@ public static async Task<IActionResult> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ public string Id
public string EmailAddress { get; set; }

/// <summary>
/// Gets or sets the date.
/// Gets or sets the correlationid.
/// </summary>
/// <value>
/// The date.
/// The correlationid.
/// </value>
[JsonProperty("date")]
public DateTime Date { get; set; }
[JsonProperty("correlationid")]
public Guid? CorrelationId { get; set; }
}
}

0 comments on commit 39942fe

Please sign in to comment.