Skip to content

Commit

Permalink
#109 Restaurant Site activated
Browse files Browse the repository at this point in the history
  • Loading branch information
MrsCD committed Mar 17, 2020
1 parent 58a0790 commit 292f6c9
Show file tree
Hide file tree
Showing 14 changed files with 560 additions and 25 deletions.
4 changes: 4 additions & 0 deletions PlanB.Butler.Admin/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[*.cs]

# CS1591: Fehledes XML-Kommentar für öffentlich sichtbaren Typ oder Element
dotnet_diagnostic.CS1591.severity = none
5 changes: 5 additions & 0 deletions PlanB.Butler.Admin/PlanB.Butler.Admin.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ VisualStudioVersion = 16.0.29814.53
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PlanB.Butler.Admin", "PlanB.Butler.Admin\PlanB.Butler.Admin.csproj", "{60E6CBA7-955D-475D-8F8E-08751CF09E3D}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{719DFCA4-453B-41D0-A305-98AE13BE8468}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,5 @@ public interface IMealService
/// <param name="id">The identifier.</param>
/// <returns>Meal by Id.</returns>
Task<MealViewModel> GetMeal(string id);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) PlanB. GmbH. All Rights Reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

using System.Collections.Generic;
using System.Threading.Tasks;

using PlanB.Butler.Admin.Models;

namespace PlanB.Butler.Admin.Contracts
{
/// <summary>
/// IRestaurantService.
/// </summary>
public interface IRestaurantService
{
/// <summary>
/// Gets the restaurant.
/// </summary>
/// <returns>Restaurant.</returns>
Task<List<RestaurantViewModel>> GetRestaurant();

/// <summary>
/// Creates the meal.
/// </summary>
/// <param name="restaurant">The meal.</param>
/// <returns>True or false.</returns>
Task<bool> CreateRestaurant(RestaurantViewModel restaurant);

/// <summary>
/// Updates the restaurant.
/// </summary>
/// <param name="restaurant">The restuarant.</param>
/// <returns>Restaurant.</returns>
Task<RestaurantViewModel> UpdateRestaurant(RestaurantViewModel restaurant);

/// <summary>
/// Gets the restaurant.
/// </summary>
/// <param name="id">The identifier.</param>
/// <returns>Restaurant by Id.</returns>
Task<RestaurantViewModel> GetRestaurant(string id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Threading.Tasks;

using Microsoft.AspNetCore.Mvc;
using PlanB.Butler.Admin.Contracts;

namespace PlanB.Butler.Admin.Controllers
{
Expand All @@ -16,15 +17,99 @@ namespace PlanB.Butler.Admin.Controllers
/// <seealso cref="Microsoft.AspNetCore.Mvc.Controller" />
public class RestaurantController : Controller
{
// GET: /<controller>/
/// GET: /<controller>

private IRestaurantService restaurantService;

/// <summary>
/// Initializes a new instance of the <see cref="RestaurantController"/> class.
/// </summary>
/// <param name="restserv">The restaurant SVC.</param>
public RestaurantController(IRestaurantService restserv) => this.restaurantService = restserv;

/// <summary>
/// Indexes this instance.
/// </summary>
/// <returns>Index.</returns>
public IActionResult Index()
{
var restaurant = this.restaurantService.GetRestaurant().Result;
return this.View(restaurant);
}

/// <summary>
/// Creates this instance.
/// </summary>
/// <returns>IActionResult.</returns>
public IActionResult Create()
{
return this.View();
}
}

/// <summary>
/// Creates the specified meal.
/// </summary>
/// <param name="restaurant">The meal.</param>
/// <returns>Meal.</returns>
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,CorrelationId,Date,Price,Name,Restaurant")] Models.RestaurantViewModel restaurant)
{
if (this.ModelState.IsValid)
{
var result = await this.restaurantService.CreateRestaurant(restaurant);
return this.RedirectToAction("Index");
}

return this.View(restaurant);
}

/// <summary>
/// Edits the specified identifier.
/// </summary>
/// <param name="id">The identifier.</param>
/// <param name="restaurant">The meal.</param>
/// <returns>IActionResult.</returns>
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(string id, [Bind("Id,CorrelationId,Date,Price,Name,Restaurant")] Models.RestaurantViewModel restaurant)
{
if (id != restaurant.Id)
{
return this.NotFound();
}

if (this.ModelState.IsValid)
{
var result = await this.restaurantService.UpdateRestaurant(restaurant);
return this.RedirectToAction(nameof(this.Index));
}

return this.View(restaurant);
}

/// <summary>
/// Edits the specified identifier.
/// </summary>
/// <param name="id">The identifier.</param>
/// <returns>Meal.</returns>
public async Task<IActionResult> Edit(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);
}


}
}
29 changes: 17 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,21 +39,14 @@ public string Id
[JsonProperty("url")]
public Uri Url { get; set; }

/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>
/// The name.
/// </value>

[JsonProperty("correlationid")]
public Guid? CorrelationId { get; set; }


[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 @@ -92,5 +85,17 @@ 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 @@ -33,6 +33,10 @@
<AdditionalFiles Include="stylecop.json" />
</ItemGroup>

<ItemGroup>
<None Include="..\.editorconfig" Link=".editorconfig" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.13.1" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.10.8" />
Expand Down
138 changes: 138 additions & 0 deletions PlanB.Butler.Admin/PlanB.Butler.Admin/Services/RestaurantService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// Copyright (c) PlanB. GmbH. All Rights Reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;

using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using PlanB.Butler.Admin.Contracts;
using PlanB.Butler.Admin.Models;

namespace PlanB.Butler.Admin.Services
{
/// <summary>
/// RestaurantService.
/// </summary>
/// <seealso cref="PlanB.Butler.Admin.Contracts.IRestaurantService" />
public class RestaurantService : IRestaurantService
{
/// <summary>
/// The HTTP client.
/// </summary>
private readonly HttpClient httpClient;

/// <summary>
/// The configuration.
/// </summary>
private readonly IConfiguration config;

/// <summary>
/// Initializes a new instance of the <see cref="RestaurantService" /> class.
/// </summary>
/// <param name="httpClient">The HTTP client.</param>
/// <param name="configuration">The configuration.</param>
public RestaurantService(HttpClient httpClient, IConfiguration configuration)
{
this.httpClient = httpClient;
this.config = configuration;
}

/// <summary>
/// Creates the meal.
/// </summary>
/// <param name="restaurant">The restaurant.</param>
/// <returns>
/// True or false.
/// </returns>
public async Task<bool> CreateRestaurant(RestaurantViewModel restaurant)
{
Guid correlationId = Guid.NewGuid();
restaurant.CorrelationId = correlationId;
var json = JsonConvert.SerializeObject(restaurant);
StringContent content = Util.CreateStringContent(json, correlationId, null);
var uri = this.config["RestaurantUri"];

HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, uri)
{
Content = content,
};
httpRequestMessage.Headers.Clear();
Util.AddDefaultEsbHeaders(httpRequestMessage, correlationId, this.config["FunctionsKey"]);
var result = await this.httpClient.SendAsync(httpRequestMessage);
result.EnsureSuccessStatusCode();
var success = result.IsSuccessStatusCode;
return success;
}

/// <summary>
/// Gets the restaurant.
/// </summary>
/// <param name="id">The identifier.</param>
/// <returns>
/// Restaurant by Id.
/// </returns>
public async Task<RestaurantViewModel> GetRestaurant(string id)
{
Guid correlationId = Guid.NewGuid();
var uri = this.config["RestaurantUri"].TrimEnd('/') + "/" + id;
HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, uri);
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);
return restaurant;
}

/// <summary>
/// Gets the restaurant.
/// </summary>
/// <returns>
/// Restaurant.
/// </returns>
public async Task<List<RestaurantViewModel>> GetRestaurant()
{
var uri = this.config["RestaurantUri"];
this.httpClient.DefaultRequestHeaders.Add(Constants.FunctionsKeyHeader, this.config["FunctionsKey"]);
var responseString = await this.httpClient.GetStringAsync(uri);

var restaurant = JsonConvert.DeserializeObject<List<RestaurantViewModel>>(responseString);
return restaurant;
}

/// <summary>
/// Updates the restaurant.
/// </summary>
/// <param name="restaurant">The restaurant.</param>
/// <returns>
/// Restaurant.
/// </returns>
public async Task<RestaurantViewModel> UpdateRestaurant(RestaurantViewModel restaurant)
{
Guid correlationId = Guid.NewGuid();
restaurant.CorrelationId = correlationId;
var json = JsonConvert.SerializeObject(restaurant);
StringContent content = Util.CreateStringContent(json, correlationId, null);
var uri = this.config["RestaurantUri"].TrimEnd('/') + "/" + restaurant.Id;

HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Put, uri)
{
Content = content,
};
httpRequestMessage.Headers.Clear();
Util.AddDefaultEsbHeaders(httpRequestMessage, correlationId, this.config["FunctionsKey"]);
var result = await this.httpClient.SendAsync(httpRequestMessage);
result.EnsureSuccessStatusCode();
var responseString = await result.Content.ReadAsStringAsync();

var updatedRestaurant = JsonConvert.DeserializeObject<RestaurantViewModel>(responseString);
return updatedRestaurant;
}
}
}
3 changes: 3 additions & 0 deletions PlanB.Butler.Admin/PlanB.Butler.Admin/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public void ConfigureServices(IServiceCollection services)
services.AddControllersWithViews();
services.AddHttpClient<IMealService, MealService>()
.SetHandlerLifetime(TimeSpan.FromMinutes(5));

services.AddHttpClient<IRestaurantService, RestaurantService>()
.SetHandlerLifetime(TimeSpan.FromMinutes(5));
}

/// <summary>
Expand Down
10 changes: 0 additions & 10 deletions PlanB.Butler.Admin/PlanB.Butler.Admin/Views/Meal/Edit.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,6 @@
<div class="col-md-4">
<form asp-action="Edit">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Id" class="control-label"></label>
<input asp-for="Id" class="form-control" />
<span asp-validation-for="Id" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="CorrelationId" class="control-label"></label>
<input asp-for="CorrelationId" class="form-control" />
<span asp-validation-for="CorrelationId" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Date" class="control-label"></label>
<input asp-for="Date" class="form-control" />
Expand Down
Loading

0 comments on commit 292f6c9

Please sign in to comment.