Skip to content

Commit

Permalink
#140 Delete function and postalCode fix
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonSchneider99 committed Mar 30, 2020
1 parent 6932388 commit 0a1a8a3
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ public interface IRestaurantService
/// </summary>
/// <param name="restaurant">The meal.</param>
/// <returns>True or false.</returns>
Task<bool> CreateRestaurant(RestaurantViewModel restaurant);
Task<RestaurantViewModel> CreateRestaurant(RestaurantViewModel restaurant);

/// <summary>
/// Updates the restaurant.
/// Updates the restaurant.
/// </summary>
/// <param name="restaurant">The restuarant.</param>
/// <returns>Restaurant.</returns>
Expand All @@ -39,5 +39,12 @@ public interface IRestaurantService
/// <param name="id">The identifier.</param>
/// <returns>Restaurant by Id.</returns>
Task<RestaurantViewModel> GetRestaurant(string id);

/// <summary>
/// Deletes the meal.
/// </summary>
/// <param name="id">The identifier.</param>
/// <returns>Meal by Id.</returns>
Task<bool> DeleteRestaurant(string id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace PlanB.Butler.Admin.Controllers
public class RestaurantController : Controller
{
/// GET: /<controller>
private IRestaurantService restaurantService;
private readonly IRestaurantService restaurantService;

/// <summary>
/// Initializes a new instance of the <see cref="RestaurantController"/> class.
Expand Down Expand Up @@ -53,7 +53,7 @@ public IActionResult Create()
/// <returns>Meal.</returns>
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,CorrelationId,Date,Price,Name,Restaurant")] Models.RestaurantViewModel restaurant)
public async Task<IActionResult> Create([Bind("Id,CorrelationId,Date,Price,Name,Restaurant,PhoneNumber,City, Street, PostalCode, Url, Email")] Models.RestaurantViewModel restaurant)
{
if (this.ModelState.IsValid)
{
Expand All @@ -72,7 +72,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")] Models.RestaurantViewModel restaurant)
public async Task<IActionResult> Edit(string id, [Bind("Id,CorrelationId,Date,Price,Name,Restaurant,PhoneNumber,City, Street, PostalCode, Url, Email")] Models.RestaurantViewModel restaurant)
{
if (id != restaurant.Id)
{
Expand Down Expand Up @@ -110,6 +110,41 @@ public async Task<IActionResult> Edit(string id)

return this.View(restaurant);
}
/// <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 restaurant = await this.restaurantService.GetRestaurant(id);

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

return this.View(restaurant);
}

/// <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.restaurantService.DeleteRestaurant(id);

return this.RedirectToAction(nameof(this.Index));
}
}
}
30 changes: 18 additions & 12 deletions PlanB.Butler.Admin/PlanB.Butler.Admin/Models/RestaurantViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,30 @@ public string Id
[JsonProperty("url")]
public Uri Url { get; set; }

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

/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>
/// The name.
/// </value>
[JsonProperty("name")]
public string Name { get; set; }

/// <summary>
/// Gets or sets the street.
/// </summary>
/// <value>
/// The street.
/// </value>
[JsonProperty("street")]
public string Street { get; set; }

Expand Down Expand Up @@ -83,17 +101,5 @@ public string Id
/// </value>
[JsonProperty("emailAddress")]
public string EmailAddress { get; set; }

/// <summary>
/// Gets or sets the date.
/// </summary>
/// <value>
/// The date.
/// </value>
[DisplayFormat(DataFormatString = "{0:dd.MM.yyyy}")]
[DataType(DataType.Date)]
[JsonProperty("date")]
public DateTime Date { get; set; }

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public RestaurantService(HttpClient httpClient, IConfiguration configuration)
/// <returns>
/// True or false.
/// </returns>
public async Task<bool> CreateRestaurant(RestaurantViewModel restaurant)
public async Task <RestaurantViewModel> CreateRestaurant(RestaurantViewModel restaurant)
{
Guid correlationId = Guid.NewGuid();
restaurant.CorrelationId = correlationId;
Expand All @@ -63,8 +63,9 @@ public async Task<bool> 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<RestaurantViewModel>(result.Content.ReadAsStringAsync().Result);
return responseModel;
}

/// <summary>
Expand Down Expand Up @@ -133,5 +134,22 @@ public async Task<RestaurantViewModel> UpdateRestaurant(RestaurantViewModel rest
var updatedRestaurant = JsonConvert.DeserializeObject<RestaurantViewModel>(responseString);
return updatedRestaurant;
}

/// <summary>
/// Deletes the restaurant.
/// </summary>
/// <param name="id">The identifier.</param>
/// <returns>
/// Succes or failure.
/// </returns>
public async Task<bool> 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;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
@model PlanB.Butler.Admin.Models.RestaurantViewModel

@{
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.City)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.City)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.PostalCode)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.PostalCode)
</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.Street)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.Street)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.EmailAddress)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.EmailAddress)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.PhoneNumber)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.PhoneNumber)
</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 @@ -54,7 +54,7 @@
@Html.DisplayFor(modelItem => item.EmailAddress)
</td>
<td>
@Html.DisplayNameFor(model => model.PostalCode)
@Html.DisplayFor(modelItem => item.PostalCode)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.Id">Edit</a>
Expand Down

0 comments on commit 0a1a8a3

Please sign in to comment.