Skip to content

Commit 0994411

Browse files
committed
Add usage of persited data to populate the view.
1 parent 37908c3 commit 0994411

5 files changed

Lines changed: 81 additions & 86 deletions

File tree

src/Web/WebMonolithic/eShopWeb/Controllers/CatalogController.cs

Lines changed: 13 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Microsoft.AspNetCore.Mvc.Rendering;
1010
using Microsoft.AspNetCore.Hosting;
1111
using System.IO;
12+
using eShopWeb.Services;
1213

1314
// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
1415

@@ -17,24 +18,26 @@ namespace eShopWeb.Controllers
1718
public class CatalogController : Controller
1819
{
1920
private readonly IHostingEnvironment _env;
21+
private readonly ICatalogService _catalogSvc;
2022

21-
public CatalogController(IHostingEnvironment env)
23+
public CatalogController(IHostingEnvironment env, ICatalogService catalogSvc)
2224
{
23-
_env = env;
25+
_env = env;
26+
_catalogSvc = catalogSvc;
2427
}
2528

2629

2730
// GET: /<controller>/
28-
public IActionResult Index(int? BrandFilterApplied, int? TypesFilterApplied, int? page)
31+
public async Task<IActionResult> Index(int? BrandFilterApplied, int? TypesFilterApplied, int? page)
2932
{
30-
var itemsPage = 10;
31-
var catalog = this.GetCatalogItems(page ?? 0, itemsPage, BrandFilterApplied, TypesFilterApplied);
32-
33+
var itemsPage = 10;
34+
var catalog = await _catalogSvc.GetCatalogItems(page ?? 0, itemsPage, BrandFilterApplied, TypesFilterApplied);
35+
3336
var vm = new IndexViewModel()
3437
{
35-
CatalogItems = catalog,
36-
Brands = GetPreconfiguredCatalogBrands(),
37-
Types = GetPreconfiguredCatalogTypes(),
38+
CatalogItems = catalog.Data,
39+
Brands = await _catalogSvc.GetBrands(),
40+
Types = await _catalogSvc.GetTypes(),
3841
BrandFilterApplied = BrandFilterApplied ?? 0,
3942
TypesFilterApplied = TypesFilterApplied ?? 0,
4043
PaginationInfo = new PaginationInfo()
@@ -62,60 +65,6 @@ public IActionResult GetImage(int id)
6265
Byte[] b = System.IO.File.ReadAllBytes(path);
6366
return File(b, "image/png");
6467

65-
}
66-
67-
private IList<CatalogItem> GetCatalogItems(int page, int itemsPage, int? brandFilterApplied, int? typesFilterApplied)
68-
{
69-
return GetPreconfiguredItems()
70-
.Where(item => brandFilterApplied == null || item.CatalogBrandId == brandFilterApplied)
71-
.Where(item => typesFilterApplied == null || item.CatalogTypeId == typesFilterApplied)
72-
.Skip(page * itemsPage)
73-
.Take(itemsPage)
74-
.ToList();
75-
}
76-
77-
static IEnumerable<SelectListItem> GetPreconfiguredCatalogBrands()
78-
{
79-
return new List<SelectListItem>()
80-
{
81-
new SelectListItem() { Value = null, Text="All", Selected= true},
82-
new SelectListItem() { Value = "1", Text = "Azure", Selected= false},
83-
new SelectListItem() { Value = "2", Text = ".NET", Selected= false },
84-
new SelectListItem() { Value = "3", Text = "Visual Studio", Selected= false },
85-
new SelectListItem() { Value = "4", Text = "SQL Server", Selected= false },
86-
new SelectListItem() { Value = "5", Text = "Other", Selected= false }
87-
};
88-
}
89-
90-
static IEnumerable<SelectListItem> GetPreconfiguredCatalogTypes()
91-
{
92-
return new List<SelectListItem>()
93-
{
94-
new SelectListItem() { Value = null, Text="All", Selected= true},
95-
new SelectListItem() { Value = "1", Text = "Mug", Selected= false },
96-
new SelectListItem() { Value = "2", Text = "T-Shirt", Selected= false },
97-
new SelectListItem() { Value = "3", Text = "Sheet", Selected= false },
98-
new SelectListItem() { Value = "4", Text = "USB Memory Stick", Selected= false }
99-
};
100-
}
101-
102-
static IList<CatalogItem> GetPreconfiguredItems()
103-
{
104-
return new List<CatalogItem>()
105-
{
106-
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" },
107-
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" },
108-
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" },
109-
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Foundation Sweatshirt", Name = ".NET Foundation Sweatshirt", Price = 12, PictureUri = "http://localhost:5106/catalog/pic/4" },
110-
new CatalogItem() { CatalogTypeId=3,CatalogBrandId=5, Description = "Roslyn Red Sheet", Name = "Roslyn Red Sheet", Price = 8.5M, PictureUri = "http://localhost:5106/catalog/pic/5" },
111-
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Blue Sweatshirt", Name = ".NET Blue Sweatshirt", Price = 12, PictureUri = "http://localhost:5106/catalog/pic/6" },
112-
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" },
113-
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Kudu Purple Sweatshirt", Name = "Kudu Purple Sweatshirt", Price = 8.5M, PictureUri = "http://localhost:5106/catalog/pic/8" },
114-
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" },
115-
new CatalogItem() { CatalogTypeId=3,CatalogBrandId=2, Description = ".NET Foundation Sheet", Name = ".NET Foundation Sheet", Price = 12, PictureUri = "http://localhost:5106/catalog/pic/10" },
116-
new CatalogItem() { CatalogTypeId=3,CatalogBrandId=2, Description = "Cup<T> Sheet", Name = "Cup<T> Sheet", Price = 8.5M, PictureUri = "http://localhost:5106/catalog/pic/11" },
117-
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Prism White TShirt", Name = "Prism White TShirt", Price = 12, PictureUri = "http://localhost:5106/catalog/pic/12" }
118-
};
119-
}
68+
}
12069
}
12170
}

src/Web/WebMonolithic/eShopWeb/Infrastructure/CatalogContextSeed.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,18 +85,18 @@ static IEnumerable<CatalogItem> GetPreconfiguredItems()
8585
{
8686
return new List<CatalogItem>()
8787
{
88-
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" },
89-
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" },
90-
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" },
91-
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Foundation Sweatshirt", Name = ".NET Foundation Sweatshirt", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/4" },
92-
new CatalogItem() { CatalogTypeId=3,CatalogBrandId=5, Description = "Roslyn Red Sheet", Name = "Roslyn Red Sheet", Price = 8.5M, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/5" },
93-
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Blue Sweatshirt", Name = ".NET Blue Sweatshirt", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/6" },
94-
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" },
95-
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Kudu Purple Sweatshirt", Name = "Kudu Purple Sweatshirt", Price = 8.5M, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/8" },
96-
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" },
97-
new CatalogItem() { CatalogTypeId=3,CatalogBrandId=2, Description = ".NET Foundation Sheet", Name = ".NET Foundation Sheet", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/10" },
98-
new CatalogItem() { CatalogTypeId=3,CatalogBrandId=2, Description = "Cup<T> Sheet", Name = "Cup<T> Sheet", Price = 8.5M, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/11" },
99-
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Prism White TShirt", Name = "Prism White TShirt", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/12" }
88+
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" },
89+
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" },
90+
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" },
91+
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Foundation Sweatshirt", Name = ".NET Foundation Sweatshirt", Price = 12, PictureUri = "http://localhost:5106/catalog/pic/4" },
92+
new CatalogItem() { CatalogTypeId=3,CatalogBrandId=5, Description = "Roslyn Red Sheet", Name = "Roslyn Red Sheet", Price = 8.5M, PictureUri = "http://localhost:5106/catalog/pic/5" },
93+
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Blue Sweatshirt", Name = ".NET Blue Sweatshirt", Price = 12, PictureUri = "http://localhost:5106/catalog/pic/6" },
94+
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" },
95+
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Kudu Purple Sweatshirt", Name = "Kudu Purple Sweatshirt", Price = 8.5M, PictureUri = "http://localhost:5106/catalog/pic/8" },
96+
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" },
97+
new CatalogItem() { CatalogTypeId=3,CatalogBrandId=2, Description = ".NET Foundation Sheet", Name = ".NET Foundation Sheet", Price = 12, PictureUri = "http://localhost:5106/catalog/pic/10" },
98+
new CatalogItem() { CatalogTypeId=3,CatalogBrandId=2, Description = "Cup<T> Sheet", Name = "Cup<T> Sheet", Price = 8.5M, PictureUri = "http://localhost:5106/catalog/pic/11" },
99+
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Prism White TShirt", Name = "Prism White TShirt", Price = 12, PictureUri = "http://localhost:5106/catalog/pic/12" }
100100
};
101101
}
102102
}

src/Web/WebMonolithic/eShopWeb/Services/CatalogService.cs

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,68 @@
44
using System.Threading.Tasks;
55
using eShopWeb.Models;
66
using Microsoft.AspNetCore.Mvc.Rendering;
7+
using eShopWeb.Infrastructure;
8+
using Microsoft.EntityFrameworkCore;
79

810
namespace eShopWeb.Services
911
{
1012
public class CatalogService : ICatalogService
1113
{
12-
public IEnumerable<SelectListItem> GetBrands()
14+
private readonly CatalogContext _context;
15+
public CatalogService(CatalogContext context)
1316
{
14-
throw new NotImplementedException();
17+
_context = context;
1518
}
1619

17-
public IList<CatalogItem> GetCatalogItems(int page, int itemsPage, int? brandFilterApplied, int? typesFilterApplied)
20+
public async Task<Catalog> GetCatalogItems(int pageIndex, int itemsPage, int? brandId, int? typeId)
1821
{
19-
throw new NotImplementedException();
22+
var root = (IQueryable<CatalogItem>)_context.CatalogItems;
23+
24+
if (typeId.HasValue)
25+
{
26+
root = root.Where(ci => ci.CatalogTypeId == typeId);
27+
}
28+
29+
if (brandId.HasValue)
30+
{
31+
root = root.Where(ci => ci.CatalogBrandId == brandId);
32+
}
33+
34+
var totalItems = await root
35+
.LongCountAsync();
36+
37+
var itemsOnPage = await root
38+
.Skip(itemsPage * pageIndex)
39+
.Take(itemsPage)
40+
.ToListAsync();
41+
42+
return new Catalog() { Data = itemsOnPage, PageIndex = pageIndex, Count = (int)totalItems };
2043
}
2144

22-
public IEnumerable<SelectListItem> GetTypes()
45+
public async Task<IEnumerable<SelectListItem>> GetBrands()
2346
{
24-
throw new NotImplementedException();
47+
var brands = await _context.CatalogBrands.ToListAsync();
48+
var items = new List<SelectListItem>();
49+
items.Add(new SelectListItem() { Value = null, Text = "All", Selected = true });
50+
foreach (CatalogBrand brand in brands)
51+
{
52+
items.Add(new SelectListItem() { Value = brand.Id.ToString(), Text = brand.Brand });
53+
}
54+
55+
return items;
56+
}
57+
58+
public async Task<IEnumerable<SelectListItem>> GetTypes()
59+
{
60+
var types = await _context.CatalogTypes.ToListAsync();
61+
var items = new List<SelectListItem>();
62+
items.Add(new SelectListItem() { Value = null, Text = "All", Selected = true });
63+
foreach (CatalogType type in types)
64+
{
65+
items.Add(new SelectListItem() { Value = type.Id.ToString(), Text = type.Type });
66+
}
67+
68+
return items;
2569
}
2670
}
2771
}

src/Web/WebMonolithic/eShopWeb/Services/ICatalogService.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ namespace eShopWeb.Services
99
{
1010
public interface ICatalogService
1111
{
12-
IList<CatalogItem> GetCatalogItems(int page, int itemsPage, int? brandFilterApplied, int? typesFilterApplied);
13-
IEnumerable<SelectListItem> GetBrands();
14-
IEnumerable<SelectListItem> GetTypes();
12+
Task<Catalog> GetCatalogItems(int pageIndex, int itemsPage, int? brandID, int? typeId);
13+
Task<IEnumerable<SelectListItem>> GetBrands();
14+
Task<IEnumerable<SelectListItem>> GetTypes();
1515
}
1616
}

src/Web/WebMonolithic/eShopWeb/Startup.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using eShopWeb.Infrastructure;
2+
using eShopWeb.Services;
23
using Microsoft.AspNetCore.Builder;
34
using Microsoft.AspNetCore.Hosting;
45
using Microsoft.EntityFrameworkCore;
@@ -43,7 +44,8 @@ public void ConfigureServices(IServiceCollection services)
4344
var message = ex.Message;
4445
}
4546
});
46-
47+
48+
services.AddTransient<ICatalogService, CatalogService>();
4749
services.AddMvc();
4850
}
4951

0 commit comments

Comments
 (0)