From 09944114a8fc2c8afaeca14d76434dbd57a3233c Mon Sep 17 00:00:00 2001 From: dsanz Date: Wed, 1 Mar 2017 12:23:39 +0100 Subject: [PATCH] Add usage of persited data to populate the view. --- .../eShopWeb/Controllers/CatalogController.cs | 77 ++++--------------- .../Infrastructure/CatalogContextSeed.cs | 24 +++--- .../eShopWeb/Services/CatalogService.cs | 56 ++++++++++++-- .../eShopWeb/Services/ICatalogService.cs | 6 +- src/Web/WebMonolithic/eShopWeb/Startup.cs | 4 +- 5 files changed, 81 insertions(+), 86 deletions(-) diff --git a/src/Web/WebMonolithic/eShopWeb/Controllers/CatalogController.cs b/src/Web/WebMonolithic/eShopWeb/Controllers/CatalogController.cs index 50fe9b481..8def61676 100644 --- a/src/Web/WebMonolithic/eShopWeb/Controllers/CatalogController.cs +++ b/src/Web/WebMonolithic/eShopWeb/Controllers/CatalogController.cs @@ -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: // - public IActionResult Index(int? BrandFilterApplied, int? TypesFilterApplied, int? page) + public async Task 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 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 GetPreconfiguredCatalogBrands() - { - return new List() - { - 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 GetPreconfiguredCatalogTypes() - { - return new List() - { - 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 GetPreconfiguredItems() - { - return new List() - { - 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 White Mug", Name = "Cup 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 Sheet", Name = "Cup 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" } - }; - } + } } } diff --git a/src/Web/WebMonolithic/eShopWeb/Infrastructure/CatalogContextSeed.cs b/src/Web/WebMonolithic/eShopWeb/Infrastructure/CatalogContextSeed.cs index 64dd74d5a..212d52285 100644 --- a/src/Web/WebMonolithic/eShopWeb/Infrastructure/CatalogContextSeed.cs +++ b/src/Web/WebMonolithic/eShopWeb/Infrastructure/CatalogContextSeed.cs @@ -85,18 +85,18 @@ { return new List() { - 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 White Mug", Name = "Cup 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 Sheet", Name = "Cup 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 White Mug", Name = "Cup 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 Sheet", Name = "Cup 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" } }; } } diff --git a/src/Web/WebMonolithic/eShopWeb/Services/CatalogService.cs b/src/Web/WebMonolithic/eShopWeb/Services/CatalogService.cs index dc1dda2f1..a5e9934a2 100644 --- a/src/Web/WebMonolithic/eShopWeb/Services/CatalogService.cs +++ b/src/Web/WebMonolithic/eShopWeb/Services/CatalogService.cs @@ -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 GetBrands() + private readonly CatalogContext _context; + public CatalogService(CatalogContext context) { - throw new NotImplementedException(); + _context = context; } - public IList GetCatalogItems(int page, int itemsPage, int? brandFilterApplied, int? typesFilterApplied) + public async Task GetCatalogItems(int pageIndex, int itemsPage, int? brandId, int? typeId) { - throw new NotImplementedException(); + var root = (IQueryable)_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 GetTypes() + public async Task> GetBrands() { - throw new NotImplementedException(); + var brands = await _context.CatalogBrands.ToListAsync(); + var items = new List(); + 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> GetTypes() + { + var types = await _context.CatalogTypes.ToListAsync(); + var items = new List(); + 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; } } } diff --git a/src/Web/WebMonolithic/eShopWeb/Services/ICatalogService.cs b/src/Web/WebMonolithic/eShopWeb/Services/ICatalogService.cs index 85598e34f..e642b2e78 100644 --- a/src/Web/WebMonolithic/eShopWeb/Services/ICatalogService.cs +++ b/src/Web/WebMonolithic/eShopWeb/Services/ICatalogService.cs @@ -9,8 +9,8 @@ namespace eShopWeb.Services { public interface ICatalogService { - IList GetCatalogItems(int page, int itemsPage, int? brandFilterApplied, int? typesFilterApplied); - IEnumerable GetBrands(); - IEnumerable GetTypes(); + Task GetCatalogItems(int pageIndex, int itemsPage, int? brandID, int? typeId); + Task> GetBrands(); + Task> GetTypes(); } } diff --git a/src/Web/WebMonolithic/eShopWeb/Startup.cs b/src/Web/WebMonolithic/eShopWeb/Startup.cs index 5b62ea3a4..ad074f49d 100644 --- a/src/Web/WebMonolithic/eShopWeb/Startup.cs +++ b/src/Web/WebMonolithic/eShopWeb/Startup.cs @@ -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(); services.AddMvc(); }