Browse Source

Add usage of persited data to populate the view.

pull/73/head
dsanz 8 years ago
parent
commit
09944114a8
5 changed files with 81 additions and 86 deletions
  1. +13
    -64
      src/Web/WebMonolithic/eShopWeb/Controllers/CatalogController.cs
  2. +12
    -12
      src/Web/WebMonolithic/eShopWeb/Infrastructure/CatalogContextSeed.cs
  3. +50
    -6
      src/Web/WebMonolithic/eShopWeb/Services/CatalogService.cs
  4. +3
    -3
      src/Web/WebMonolithic/eShopWeb/Services/ICatalogService.cs
  5. +3
    -1
      src/Web/WebMonolithic/eShopWeb/Startup.cs

+ 13
- 64
src/Web/WebMonolithic/eShopWeb/Controllers/CatalogController.cs View File

@ -9,6 +9,7 @@ using eShopWeb.Models.Pagination;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Hosting;
using System.IO;
using eShopWeb.Services;
// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
@ -17,24 +18,26 @@ namespace eShopWeb.Controllers
public class CatalogController : Controller
{
private readonly IHostingEnvironment _env;
private readonly ICatalogService _catalogSvc;
public CatalogController(IHostingEnvironment env)
public CatalogController(IHostingEnvironment env, ICatalogService catalogSvc)
{
_env = env;
_env = env;
_catalogSvc = catalogSvc;
}
// GET: /<controller>/
public IActionResult Index(int? BrandFilterApplied, int? TypesFilterApplied, int? page)
public async Task<IActionResult> Index(int? BrandFilterApplied, int? TypesFilterApplied, int? page)
{
var itemsPage = 10;
var catalog = this.GetCatalogItems(page ?? 0, itemsPage, BrandFilterApplied, TypesFilterApplied);
var itemsPage = 10;
var catalog = await _catalogSvc.GetCatalogItems(page ?? 0, itemsPage, BrandFilterApplied, TypesFilterApplied);
var vm = new IndexViewModel()
{
CatalogItems = catalog,
Brands = GetPreconfiguredCatalogBrands(),
Types = GetPreconfiguredCatalogTypes(),
CatalogItems = catalog.Data,
Brands = await _catalogSvc.GetBrands(),
Types = await _catalogSvc.GetTypes(),
BrandFilterApplied = BrandFilterApplied ?? 0,
TypesFilterApplied = TypesFilterApplied ?? 0,
PaginationInfo = new PaginationInfo()
@ -62,60 +65,6 @@ namespace eShopWeb.Controllers
Byte[] b = System.IO.File.ReadAllBytes(path);
return File(b, "image/png");
}
private IList<CatalogItem> GetCatalogItems(int page, int itemsPage, int? brandFilterApplied, int? typesFilterApplied)
{
return GetPreconfiguredItems()
.Where(item => brandFilterApplied == null || item.CatalogBrandId == brandFilterApplied)
.Where(item => typesFilterApplied == null || item.CatalogTypeId == typesFilterApplied)
.Skip(page * itemsPage)
.Take(itemsPage)
.ToList();
}
static IEnumerable<SelectListItem> GetPreconfiguredCatalogBrands()
{
return new List<SelectListItem>()
{
new SelectListItem() { Value = null, Text="All", Selected= true},
new SelectListItem() { Value = "1", Text = "Azure", Selected= false},
new SelectListItem() { Value = "2", Text = ".NET", Selected= false },
new SelectListItem() { Value = "3", Text = "Visual Studio", Selected= false },
new SelectListItem() { Value = "4", Text = "SQL Server", Selected= false },
new SelectListItem() { Value = "5", Text = "Other", Selected= false }
};
}
static IEnumerable<SelectListItem> GetPreconfiguredCatalogTypes()
{
return new List<SelectListItem>()
{
new SelectListItem() { Value = null, Text="All", Selected= true},
new SelectListItem() { Value = "1", Text = "Mug", Selected= false },
new SelectListItem() { Value = "2", Text = "T-Shirt", Selected= false },
new SelectListItem() { Value = "3", Text = "Sheet", Selected= false },
new SelectListItem() { Value = "4", Text = "USB Memory Stick", Selected= false }
};
}
static IList<CatalogItem> GetPreconfiguredItems()
{
return new List<CatalogItem>()
{
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Bot Black Sweatshirt", Name = ".NET Bot Black Sweatshirt", Price = 19.5M, PictureUri = "http://localhost:5106/catalog/pic/1" },
new CatalogItem() { CatalogTypeId=1,CatalogBrandId=2, Description = ".NET Black & White Mug", Name = ".NET Black & White Mug", Price= 8.50M, PictureUri = "http://localhost:5106/catalog/pic/2" },
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Prism White T-Shirt", Name = "Prism White T-Shirt", Price = 12, PictureUri = "http://localhost:5106/catalog/pic/3" },
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Foundation Sweatshirt", Name = ".NET Foundation Sweatshirt", Price = 12, PictureUri = "http://localhost:5106/catalog/pic/4" },
new CatalogItem() { CatalogTypeId=3,CatalogBrandId=5, Description = "Roslyn Red Sheet", Name = "Roslyn Red Sheet", Price = 8.5M, PictureUri = "http://localhost:5106/catalog/pic/5" },
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Blue Sweatshirt", Name = ".NET Blue Sweatshirt", Price = 12, PictureUri = "http://localhost:5106/catalog/pic/6" },
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Roslyn Red T-Shirt", Name = "Roslyn Red T-Shirt", Price = 12, PictureUri = "http://localhost:5106/catalog/pic/7" },
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Kudu Purple Sweatshirt", Name = "Kudu Purple Sweatshirt", Price = 8.5M, PictureUri = "http://localhost:5106/catalog/pic/8" },
new CatalogItem() { CatalogTypeId=1,CatalogBrandId=5, Description = "Cup<T> White Mug", Name = "Cup<T> White Mug", Price = 12, PictureUri = "http://localhost:5106/catalog/pic/9" },
new CatalogItem() { CatalogTypeId=3,CatalogBrandId=2, Description = ".NET Foundation Sheet", Name = ".NET Foundation Sheet", Price = 12, PictureUri = "http://localhost:5106/catalog/pic/10" },
new CatalogItem() { CatalogTypeId=3,CatalogBrandId=2, Description = "Cup<T> Sheet", Name = "Cup<T> Sheet", Price = 8.5M, PictureUri = "http://localhost:5106/catalog/pic/11" },
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Prism White TShirt", Name = "Prism White TShirt", Price = 12, PictureUri = "http://localhost:5106/catalog/pic/12" }
};
}
}
}
}

+ 12
- 12
src/Web/WebMonolithic/eShopWeb/Infrastructure/CatalogContextSeed.cs View File

@ -85,18 +85,18 @@
{
return new List<CatalogItem>()
{
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Bot Black Sweatshirt", Name = ".NET Bot Black Sweatshirt", Price = 19.5M, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/1" },
new CatalogItem() { CatalogTypeId=1,CatalogBrandId=2, Description = ".NET Black & White Mug", Name = ".NET Black & White Mug", Price= 8.50M, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/2" },
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Prism White T-Shirt", Name = "Prism White T-Shirt", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/3" },
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Foundation Sweatshirt", Name = ".NET Foundation Sweatshirt", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/4" },
new CatalogItem() { CatalogTypeId=3,CatalogBrandId=5, Description = "Roslyn Red Sheet", Name = "Roslyn Red Sheet", Price = 8.5M, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/5" },
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Blue Sweatshirt", Name = ".NET Blue Sweatshirt", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/6" },
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Roslyn Red T-Shirt", Name = "Roslyn Red T-Shirt", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/7" },
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Kudu Purple Sweatshirt", Name = "Kudu Purple Sweatshirt", Price = 8.5M, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/8" },
new CatalogItem() { CatalogTypeId=1,CatalogBrandId=5, Description = "Cup<T> White Mug", Name = "Cup<T> White Mug", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/9" },
new CatalogItem() { CatalogTypeId=3,CatalogBrandId=2, Description = ".NET Foundation Sheet", Name = ".NET Foundation Sheet", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/10" },
new CatalogItem() { CatalogTypeId=3,CatalogBrandId=2, Description = "Cup<T> Sheet", Name = "Cup<T> Sheet", Price = 8.5M, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/11" },
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Prism White TShirt", Name = "Prism White TShirt", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/12" }
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Bot Black Sweatshirt", Name = ".NET Bot Black Sweatshirt", Price = 19.5M, PictureUri = "http://localhost:5106/catalog/pic/1" },
new CatalogItem() { CatalogTypeId=1,CatalogBrandId=2, Description = ".NET Black & White Mug", Name = ".NET Black & White Mug", Price= 8.50M, PictureUri = "http://localhost:5106/catalog/pic/2" },
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Prism White T-Shirt", Name = "Prism White T-Shirt", Price = 12, PictureUri = "http://localhost:5106/catalog/pic/3" },
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Foundation Sweatshirt", Name = ".NET Foundation Sweatshirt", Price = 12, PictureUri = "http://localhost:5106/catalog/pic/4" },
new CatalogItem() { CatalogTypeId=3,CatalogBrandId=5, Description = "Roslyn Red Sheet", Name = "Roslyn Red Sheet", Price = 8.5M, PictureUri = "http://localhost:5106/catalog/pic/5" },
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Blue Sweatshirt", Name = ".NET Blue Sweatshirt", Price = 12, PictureUri = "http://localhost:5106/catalog/pic/6" },
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Roslyn Red T-Shirt", Name = "Roslyn Red T-Shirt", Price = 12, PictureUri = "http://localhost:5106/catalog/pic/7" },
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Kudu Purple Sweatshirt", Name = "Kudu Purple Sweatshirt", Price = 8.5M, PictureUri = "http://localhost:5106/catalog/pic/8" },
new CatalogItem() { CatalogTypeId=1,CatalogBrandId=5, Description = "Cup<T> White Mug", Name = "Cup<T> White Mug", Price = 12, PictureUri = "http://localhost:5106/catalog/pic/9" },
new CatalogItem() { CatalogTypeId=3,CatalogBrandId=2, Description = ".NET Foundation Sheet", Name = ".NET Foundation Sheet", Price = 12, PictureUri = "http://localhost:5106/catalog/pic/10" },
new CatalogItem() { CatalogTypeId=3,CatalogBrandId=2, Description = "Cup<T> Sheet", Name = "Cup<T> Sheet", Price = 8.5M, PictureUri = "http://localhost:5106/catalog/pic/11" },
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Prism White TShirt", Name = "Prism White TShirt", Price = 12, PictureUri = "http://localhost:5106/catalog/pic/12" }
};
}
}


+ 50
- 6
src/Web/WebMonolithic/eShopWeb/Services/CatalogService.cs View File

@ -4,24 +4,68 @@ using System.Linq;
using System.Threading.Tasks;
using eShopWeb.Models;
using Microsoft.AspNetCore.Mvc.Rendering;
using eShopWeb.Infrastructure;
using Microsoft.EntityFrameworkCore;
namespace eShopWeb.Services
{
public class CatalogService : ICatalogService
{
public IEnumerable<SelectListItem> GetBrands()
private readonly CatalogContext _context;
public CatalogService(CatalogContext context)
{
throw new NotImplementedException();
_context = context;
}
public IList<CatalogItem> GetCatalogItems(int page, int itemsPage, int? brandFilterApplied, int? typesFilterApplied)
public async Task<Catalog> GetCatalogItems(int pageIndex, int itemsPage, int? brandId, int? typeId)
{
throw new NotImplementedException();
var root = (IQueryable<CatalogItem>)_context.CatalogItems;
if (typeId.HasValue)
{
root = root.Where(ci => ci.CatalogTypeId == typeId);
}
if (brandId.HasValue)
{
root = root.Where(ci => ci.CatalogBrandId == brandId);
}
var totalItems = await root
.LongCountAsync();
var itemsOnPage = await root
.Skip(itemsPage * pageIndex)
.Take(itemsPage)
.ToListAsync();
return new Catalog() { Data = itemsOnPage, PageIndex = pageIndex, Count = (int)totalItems };
}
public IEnumerable<SelectListItem> GetTypes()
public async Task<IEnumerable<SelectListItem>> GetBrands()
{
throw new NotImplementedException();
var brands = await _context.CatalogBrands.ToListAsync();
var items = new List<SelectListItem>();
items.Add(new SelectListItem() { Value = null, Text = "All", Selected = true });
foreach (CatalogBrand brand in brands)
{
items.Add(new SelectListItem() { Value = brand.Id.ToString(), Text = brand.Brand });
}
return items;
}
public async Task<IEnumerable<SelectListItem>> GetTypes()
{
var types = await _context.CatalogTypes.ToListAsync();
var items = new List<SelectListItem>();
items.Add(new SelectListItem() { Value = null, Text = "All", Selected = true });
foreach (CatalogType type in types)
{
items.Add(new SelectListItem() { Value = type.Id.ToString(), Text = type.Type });
}
return items;
}
}
}

+ 3
- 3
src/Web/WebMonolithic/eShopWeb/Services/ICatalogService.cs View File

@ -9,8 +9,8 @@ namespace eShopWeb.Services
{
public interface ICatalogService
{
IList<CatalogItem> GetCatalogItems(int page, int itemsPage, int? brandFilterApplied, int? typesFilterApplied);
IEnumerable<SelectListItem> GetBrands();
IEnumerable<SelectListItem> GetTypes();
Task<Catalog> GetCatalogItems(int pageIndex, int itemsPage, int? brandID, int? typeId);
Task<IEnumerable<SelectListItem>> GetBrands();
Task<IEnumerable<SelectListItem>> GetTypes();
}
}

+ 3
- 1
src/Web/WebMonolithic/eShopWeb/Startup.cs View File

@ -1,4 +1,5 @@
using eShopWeb.Infrastructure;
using eShopWeb.Services;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
@ -43,7 +44,8 @@ namespace eShopWeb
var message = ex.Message;
}
});
services.AddTransient<ICatalogService, CatalogService>();
services.AddMvc();
}


Loading…
Cancel
Save