Permalink
Browse files

Set up configuration/add anticipated view models/Utility classes

  • Loading branch information...
ehelin committed May 11, 2018
1 parent 8fa5b50 commit a3ee098f07fcc25eeeea8bae926a0d1d40f039fc
@@ -0,0 +1,7 @@
namespace FullStackDeveloper
{
public class Constants
{
public const string HOST = "Host";
}
}
@@ -1,17 +1,24 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using FullStackDeveloper.Models;
using Microsoft.Extensions.Configuration;
namespace FullStackDeveloper.Controllers
{
public class LocationController : Controller
{
private LocationHttpClient client = null;
private IConfiguration config = null;
public LocationController(IConfiguration config)
{
client = new LocationHttpClient(Utilities.GetConfigValue(Constants.HOST, config));
}
public IActionResult Index()
{
//api/Locations
// TODO - take result from LocationHttpClient and convert to model/view - inside model?
return View();
}
@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DataModels = Data.Models;
using WebModels = FullStackDeveloper.Models;
using System.Net.Http;
namespace FullStackDeveloper
{
public class LocationHttpClient
{
private string host;
public LocationHttpClient(string host)
{
this.host = host;
}
public async Task<List<WebModels.Location>> GetLocations()
{
HttpResponseMessage response = await GET("api/Locations");
var result = await response.Content.ReadAsStringAsync();
// TODO - convert result to List of data.locations
// TODO - convert data.locations to web.locations
// TODO - return result
return null;
}
private async Task<HttpResponseMessage> GET(string subUrl, string parameter = "")
{
HttpClient client = new HttpClient();
HttpResponseMessage response = null;
string url = host + subUrl;
if (!string.IsNullOrEmpty(parameter)){
url += "/" + parameter;
}
response = await client.GetAsync(url).ConfigureAwait(false);
return response;
}
}
}
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
namespace FullStackDeveloper.Models
{
public partial class Location
{
[DataMember]
public int LocationiId { get; set; }
[DataMember]
public string LocationName { get; set; }
[DataMember]
public LocationDetails LocationDetails { get; set; }
}
}
@@ -0,0 +1,11 @@
namespace FullStackDeveloper.Models
{
public partial class LocationDetails
{
public int LocationDetailId { get; set; }
public int? LocationId { get; set; }
public string Weather { get; set; }
public string Food { get; set; }
public string People { get; set; }
}
}
@@ -9,15 +9,24 @@ public class Startup
{
public Startup(IConfiguration configuration)
{
//var builder = new ConfigurationBuilder()
// .SetBasePath(configuration.)
// .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
//Configuration = builder.Build();
Configuration = configuration;
//AppSettings = configuration[
}
public IConfiguration Configuration { get; }
public static IConfiguration AppSettings { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddSingleton<IConfiguration>(Configuration);
//var result = Configuration.GetSection("AppSettings");
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@@ -0,0 +1,20 @@
using Microsoft.Extensions.Configuration;
using System;
namespace FullStackDeveloper
{
public class Utilities
{
public static string GetConfigValue(string key, IConfiguration config)
{
var section = config.GetSection(key);
if (section == null)
{
throw new Exception("Config item " + key + " is null");
}
return section.Value;
}
}
}
@@ -6,6 +6,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.5" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.0.2" />
</ItemGroup>
<ItemGroup>
@@ -19,4 +20,8 @@
<Folder Include="wwwroot\lib\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Data\Data.csproj" />
</ItemGroup>
</Project>
@@ -4,5 +4,6 @@
"LogLevel": {
"Default": "Warning"
}
}
},
"Host": "http://localhost:52451/"
}
View
@@ -11,6 +11,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.5" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.0.2" />
</ItemGroup>
<ItemGroup>

0 comments on commit a3ee098

Please sign in to comment.