Stub out the code for the catalog service.

This commit is contained in:
Bill Wagner 2017-03-02 14:07:41 -05:00
parent a4a5cd8c45
commit 1c840df8e1
9 changed files with 182 additions and 41 deletions

View File

@ -218,11 +218,14 @@
</Compile>
<Compile Include="Models\CatalogBrand.cs" />
<Compile Include="Models\CatalogItem.cs" />
<Compile Include="Models\CatalogRoot.cs" />
<Compile Include="Models\CatalogType.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Services\CatalogMockService.cs" />
<Compile Include="Services\CatalogService.cs" />
<Compile Include="Services\Common.cs" />
<Compile Include="Services\ICatalogService.cs" />
<Compile Include="Services\IRouteProvider.cs" />
<Compile Include="Site.Master.cs">
<DependentUpon>Site.Master</DependentUpon>
<SubType>ASPXCodeBehind</SubType>

View File

@ -1,43 +1,6 @@
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Microsoft.eShopOnContainers.Catalog.WebForms._Default" Async="true" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<div class="jumbotron">
<h1>ASP.NET</h1>
<p class="lead">ASP.NET is a free web framework for building great Web sites and Web applications using HTML, CSS, and JavaScript.</p>
<p><a href="http://www.asp.net" class="btn btn-primary btn-lg">Learn more &raquo;</a></p>
</div>
<div class="row">
<div class="col-md-4">
<h2>Getting started</h2>
<p>
ASP.NET Web Forms lets you build dynamic websites using a familiar drag-and-drop, event-driven model.
A design surface and hundreds of controls and components let you rapidly build sophisticated, powerful UI-driven sites with data access.
</p>
<p>
<a class="btn btn-default" href="https://go.microsoft.com/fwlink/?LinkId=301948">Learn more &raquo;</a>
</p>
</div>
<div class="col-md-4">
<h2>Get more libraries</h2>
<p>
NuGet is a free Visual Studio extension that makes it easy to add, remove, and update libraries and tools in Visual Studio projects.
</p>
<p>
<a class="btn btn-default" href="https://go.microsoft.com/fwlink/?LinkId=301949">Learn more &raquo;</a>
</p>
</div>
<div class="col-md-4">
<h2>Web Hosting</h2>
<p>
You can easily find a web hosting company that offers the right mix of features and price for your applications.
</p>
<p>
<a class="btn btn-default" href="https://go.microsoft.com/fwlink/?LinkId=301950">Learn more &raquo;</a>
</p>
</div>
</div>
<div class="row">
<asp:ListView ID="catalogList" runat="server"
DataKeyNames="Id" GroupItemCount="4"

View File

@ -20,12 +20,12 @@ namespace Microsoft.eShopOnContainers.Catalog.WebForms
protected override void OnLoad(EventArgs e)
{
RegisterAsyncTask(new PageAsyncTask(LoadCatalogData));
RegisterAsyncTask(new PageAsyncTask(LoadCatalogDataAsync));
base.OnLoad(e);
}
private async Task LoadCatalogData()
private async Task LoadCatalogDataAsync()
{
var container = Application.Get("container") as IContainer;
using (scope = container?.BeginLifetimeScope())

View File

@ -4,6 +4,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.Optimization;
using System.Web.Routing;
using System.Web.Security;
@ -20,10 +21,19 @@ namespace Microsoft.eShopOnContainers.Catalog.WebForms
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
// TODO: CONTENT on this
// Register Containers:
var settings= WebConfigurationManager.AppSettings;
var useFake = settings["usefake"];
bool fake = useFake == "true";
var builder = new ContainerBuilder();
builder.RegisterType<CatalogMockService>().As<ICatalogService>();
if (fake)
{
builder.RegisterType<CatalogMockService>()
.As<ICatalogService>();
} else {
builder.RegisterType<CatalogMockService>()
.As<ICatalogService>();
}
var container = builder.Build();
Application.Add("container", container);
}

View File

@ -0,0 +1,12 @@
using System.Collections.Generic;
namespace eShopOnContainers.Core.Models.Catalog
{
public class CatalogRoot
{
public int PageIndex { get; set; }
public int PageSize { get; set; }
public int Count { get; set; }
public List<CatalogItem> Data { get; set; }
}
}

View File

@ -0,0 +1,131 @@
using System;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using eShopOnContainers.Core.Models.Catalog;
using eShopOnContainers.Core.Services.RequestProvider;
using eShopOnContainers.Core.Extensions;
using System.Collections.Generic;
// from https://github.com/dotnet/eShopOnContainers/blob/vs2017/src/Mobile/eShopOnContainers/eShopOnContainers.Core/Services/Catalog/CatalogService.cs
// TODO: DRY this stuff.
namespace eShopOnContainers.Core.Services.Catalog
{
public class CatalogService : ICatalogService
{
private readonly IRequestProvider _requestProvider;
public CatalogService(IRequestProvider requestProvider)
{
_requestProvider = requestProvider;
}
public async Task<ObservableCollection<CatalogItem>> FilterAsync(int catalogBrandId, int catalogTypeId)
{
try
{
// TODO:
UriBuilder builder = new UriBuilder("" /* GlobalSetting.Instance.CatalogEndpoint */);
builder.Path = string.Format("api/v1/catalog/items/type/{0}/brand/{1}", catalogTypeId, catalogBrandId);
string uri = builder.ToString();
CatalogRoot catalog =
await _requestProvider.GetAsync<CatalogRoot>(uri);
if (catalog?.Data != null)
return catalog?.Data.ToObservableCollection();
else
return new ObservableCollection<CatalogItem>();
}
catch
{
return new ObservableCollection<CatalogItem>();
}
}
public async Task<ObservableCollection<CatalogItem>> GetCatalogAsync()
{
try
{
// TODO:
UriBuilder builder = new UriBuilder("" /* GlobalSetting.Instance.CatalogEndpoint */);
builder.Path = "api/v1/catalog/items";
string uri = builder.ToString();
CatalogRoot catalog =
await _requestProvider.GetAsync<CatalogRoot>(uri);
if (catalog?.Data != null)
{
// TODO: ServicesHelper.FixCatalogItemPictureUri(catalog?.Data);
return catalog?.Data.ToObservableCollection();
}
else
return new ObservableCollection<CatalogItem>();
}
catch
{
return new ObservableCollection<CatalogItem>();
}
}
public Task<CatalogItem> GetCatalogItemAsync(string id)
{
throw new NotImplementedException();
}
public async Task<ObservableCollection<CatalogBrand>> GetCatalogBrandAsync()
{
try
{
// TODO:
UriBuilder builder = new UriBuilder("" /* GlobalSetting.Instance.CatalogEndpoint */);
builder.Path = "api/v1/catalog/catalogbrands";
string uri = builder.ToString();
IEnumerable<CatalogBrand> brands =
await _requestProvider.GetAsync<IEnumerable<CatalogBrand>>(uri);
if (brands != null)
return brands?.ToObservableCollection();
else
return new ObservableCollection<CatalogBrand>();
}
catch
{
return new ObservableCollection<CatalogBrand>();
}
}
public async Task<ObservableCollection<CatalogType>> GetCatalogTypeAsync()
{
try
{
// TODO:
UriBuilder builder = new UriBuilder("" /* GlobalSetting.Instance.CatalogEndpoint */);
builder.Path = "api/v1/catalog/catalogtypes";
string uri = builder.ToString();
IEnumerable<CatalogType> types =
await _requestProvider.GetAsync<IEnumerable<CatalogType>>(uri);
if (types != null)
return types.ToObservableCollection();
else
return new ObservableCollection<CatalogType>();
}
catch
{
return new ObservableCollection<CatalogType>();
}
}
}
}

View File

@ -0,0 +1,19 @@
using System.Threading.Tasks;
namespace eShopOnContainers.Core.Services.RequestProvider
{
public interface IRequestProvider
{
Task<TResult> GetAsync<TResult>(string uri, string token = "");
Task<TResult> PostAsync<TResult>(string uri, TResult data, string token = "");
Task<TResult> PostAsync<TRequest, TResult>(string uri, TRequest data, string token = "");
Task<TResult> PutAsync<TResult>(string uri, TResult data, string token = "");
Task<TResult> PutAsync<TRequest, TResult>(string uri, TRequest data, string token = "");
Task DeleteAsync(string uri, string token = "");
}
}

View File

@ -4,6 +4,9 @@
https://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<appSettings>
<add key="usefake" value="true"/>
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2" />