Add usage of persited data to populate the view.

This commit is contained in:
dsanz 2017-03-01 12:23:39 +01:00
parent 37908c3f83
commit 09944114a8
5 changed files with 81 additions and 86 deletions

View File

@ -9,6 +9,7 @@ using eShopWeb.Models.Pagination;
using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using System.IO; using System.IO;
using eShopWeb.Services;
// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 // 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 public class CatalogController : Controller
{ {
private readonly IHostingEnvironment _env; 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>/ // 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 itemsPage = 10;
var catalog = this.GetCatalogItems(page ?? 0, itemsPage, BrandFilterApplied, TypesFilterApplied); var catalog = await _catalogSvc.GetCatalogItems(page ?? 0, itemsPage, BrandFilterApplied, TypesFilterApplied);
var vm = new IndexViewModel() var vm = new IndexViewModel()
{ {
CatalogItems = catalog, CatalogItems = catalog.Data,
Brands = GetPreconfiguredCatalogBrands(), Brands = await _catalogSvc.GetBrands(),
Types = GetPreconfiguredCatalogTypes(), Types = await _catalogSvc.GetTypes(),
BrandFilterApplied = BrandFilterApplied ?? 0, BrandFilterApplied = BrandFilterApplied ?? 0,
TypesFilterApplied = TypesFilterApplied ?? 0, TypesFilterApplied = TypesFilterApplied ?? 0,
PaginationInfo = new PaginationInfo() PaginationInfo = new PaginationInfo()
@ -62,60 +65,6 @@ namespace eShopWeb.Controllers
Byte[] b = System.IO.File.ReadAllBytes(path); Byte[] b = System.IO.File.ReadAllBytes(path);
return File(b, "image/png"); 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" }
};
}
} }
} }

View File

@ -85,18 +85,18 @@
{ {
return new List<CatalogItem>() 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=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://externalcatalogbaseurltobereplaced/api/v1/pic/2" }, 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://externalcatalogbaseurltobereplaced/api/v1/pic/3" }, 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://externalcatalogbaseurltobereplaced/api/v1/pic/4" }, 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://externalcatalogbaseurltobereplaced/api/v1/pic/5" }, 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://externalcatalogbaseurltobereplaced/api/v1/pic/6" }, 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://externalcatalogbaseurltobereplaced/api/v1/pic/7" }, 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://externalcatalogbaseurltobereplaced/api/v1/pic/8" }, 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://externalcatalogbaseurltobereplaced/api/v1/pic/9" }, 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://externalcatalogbaseurltobereplaced/api/v1/pic/10" }, 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://externalcatalogbaseurltobereplaced/api/v1/pic/11" }, 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://externalcatalogbaseurltobereplaced/api/v1/pic/12" } new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Prism White TShirt", Name = "Prism White TShirt", Price = 12, PictureUri = "http://localhost:5106/catalog/pic/12" }
}; };
} }
} }

View File

@ -4,24 +4,68 @@ using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using eShopWeb.Models; using eShopWeb.Models;
using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.AspNetCore.Mvc.Rendering;
using eShopWeb.Infrastructure;
using Microsoft.EntityFrameworkCore;
namespace eShopWeb.Services namespace eShopWeb.Services
{ {
public class CatalogService : ICatalogService 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;
} }
} }
} }

View File

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

View File

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