generated from Kentico/repo-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStoreCategoryController.cs
67 lines (53 loc) · 2.45 KB
/
StoreCategoryController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using CMS.Websites;
using DancingGoat;
using DancingGoat.Controllers.Store;
using DancingGoat.Models;
using Kentico.Content.Web.Mvc;
using Kentico.Content.Web.Mvc.Routing;
using Kentico.Xperience.K13Ecommerce.Products;
using Microsoft.AspNetCore.Mvc;
[assembly:
RegisterWebPageRoute(CategoryPage.CONTENT_TYPE_NAME, typeof(StoreCategoryController),
WebsiteChannelNames = new[] { DancingGoatConstants.WEBSITE_CHANNEL_NAME })]
namespace DancingGoat.Controllers.Store;
public class StoreCategoryController : Controller
{
private readonly CategoryPageRepository categoryPageRepository;
private readonly IWebPageDataContextRetriever webPageDataContextRetriever;
private readonly IWebPageUrlRetriever urlRetriever;
private readonly IProductService productService;
public StoreCategoryController(CategoryPageRepository categoryPageRepository,
IWebPageDataContextRetriever webPageDataContextRetriever,
IWebPageUrlRetriever urlRetriever,
IProductService productService)
{
this.categoryPageRepository = categoryPageRepository;
this.webPageDataContextRetriever = webPageDataContextRetriever;
this.urlRetriever = urlRetriever;
this.productService = productService;
}
// GET
public async Task<IActionResult> Index()
{
var webPage = webPageDataContextRetriever.Retrieve().WebPage;
var categoryPage = await categoryPageRepository.GetCategoryPage(webPage.WebPageItemID, webPage.LanguageName, HttpContext.RequestAborted);
var products = await categoryPageRepository.GetCategoryProducts(categoryPage, webPage.LanguageName,
HttpContext.RequestAborted);
var prices = (await productService.GetProductsPrices(products.Select(p => p.Product.First().SKUID)))
.ToLookup(p => p.ProductSkuId);
var productModels = new List<StoreProductListItemViewModel>();
foreach (var productPage in products)
{
var productSku = productPage.Product.First();
if (!prices[productSku.SKUID].Any())
{
continue;
}
var model = StoreProductListItemViewModel.GetViewModel(productSku,
(await urlRetriever.Retrieve(productPage)).RelativePath, prices[productSku.SKUID].First().Prices);
productModels.Add(model);
}
var viewModel = CategoryPageViewModel.GetViewModel(categoryPage, productModels);
return View(viewModel);
}
}