Permalink
Browse files

Location controller/view/model hooked up to web api

  • Loading branch information...
ehelin committed May 16, 2018
1 parent a3ee098 commit 3ca38182b05d55c8b4a6fee80680236e143cf344
@@ -1,7 +1,9 @@
namespace FullStackDeveloper
namespace Web
{
public class Constants
{
public const string HOST = "Host";
public const string ENDPOINT_GET_LOCATIONS = "api/Locations";
}
}
@@ -1,9 +1,9 @@
using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using FullStackDeveloper.Models;
using Web.Models;
using Microsoft.Extensions.Configuration;
namespace FullStackDeveloper.Controllers
namespace Web.Controllers
{
public class LocationController : Controller
{
@@ -17,12 +17,12 @@ public LocationController(IConfiguration config)
public IActionResult Index()
{
//api/Locations
// TODO - take result from LocationHttpClient and convert to model/view - inside model?
return View();
var model = this.client.GetLocations().Result;
return View(model);
}
public IActionResult Details()
public IActionResult Details(int id)
{
return View();
}
@@ -1,12 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Threading.Tasks;
using DataModels = Data.Models;
using WebModels = FullStackDeveloper.Models;
using WebModels = Web.Models;
using System.Net.Http;
using Newtonsoft.Json;
namespace FullStackDeveloper
namespace Web
{
public class LocationHttpClient
{
@@ -17,16 +15,25 @@ public LocationHttpClient(string host)
this.host = host;
}
public async Task<List<WebModels.Location>> GetLocations()
public async Task<WebModels.LocationsModel> GetLocations()
{
HttpResponseMessage response = await GET("api/Locations");
HttpResponseMessage response = await GET(Constants.ENDPOINT_GET_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
DataModels.Location[] dataLocations = JsonConvert.DeserializeObject<DataModels.Location[]>(result);
WebModels.LocationsModel webLocations = new WebModels.LocationsModel();
webLocations.Locations = new WebModels.LocationModel[dataLocations.Length];
return null;
for (int i=0; i< dataLocations.Length; i++)
{
WebModels.LocationModel webLocation = new WebModels.LocationModel();
webLocation.LocationiId = dataLocations[i].LocationiId;
webLocation.LocationName = dataLocations[i].LocationName;
webLocations.Locations[i] = webLocation;
}
return webLocations;
}
private async Task<HttpResponseMessage> GET(string subUrl, string parameter = "")
@@ -1,6 +1,6 @@
using System;
namespace FullStackDeveloper.Models
namespace Web.Models
{
public class ErrorViewModel
{

This file was deleted.

Oops, something went wrong.
@@ -1,6 +1,6 @@
namespace FullStackDeveloper.Models
namespace Web.Models
{
public partial class LocationDetails
public partial class LocationDetailsModel
{
public int LocationDetailId { get; set; }
public int? LocationId { get; set; }
@@ -0,0 +1,14 @@
using System.Runtime.Serialization;
namespace Web.Models
{
public partial class LocationModel
{
[DataMember]
public int LocationiId { get; set; }
[DataMember]
public string LocationName { get; set; }
[DataMember]
public LocationDetailsModel LocationDetails { get; set; }
}
}
@@ -0,0 +1,7 @@
namespace Web.Models
{
public class LocationsModel
{
public LocationModel[] Locations;
}
}
@@ -8,7 +8,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
namespace FullStackDeveloper
namespace Web
{
public class Program
{
@@ -15,7 +15,7 @@
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"FullStackDeveloper": {
"Web": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
@@ -3,7 +3,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace FullStackDeveloper
namespace Web
{
public class Startup
{
@@ -1,7 +1,7 @@
using Microsoft.Extensions.Configuration;
using System;
namespace FullStackDeveloper
namespace Web
{
public class Utilities
{
@@ -1,7 +1,14 @@
@{
@model LocationsModel
@{
ViewData["Title"] = "Location Summary";
}
<p>Location Summary</p>
<ul>
@foreach (LocationModel location in Model.Locations)
{
<li>@Html.ActionLink(location.LocationName, "Details", "Location", new { id = @location.LocationiId }, null)</li>
}
</ul>
<br/>
@Html.ActionLink("Details", "Details", "Location")
@@ -1,3 +1,3 @@
@using FullStackDeveloper
@using FullStackDeveloper.Models
@using Web
@using Web.Models
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@@ -7,6 +7,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.5" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.0.2" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
</ItemGroup>
<ItemGroup>
View
@@ -12,6 +12,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.5" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.0.2" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
</ItemGroup>
<ItemGroup>

0 comments on commit 3ca3818

Please sign in to comment.