46 lines
1.8 KiB
C#
46 lines
1.8 KiB
C#
using System;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.eShopOnContainers.WebMVC.ViewModels.Pagination;
|
|
using Microsoft.eShopOnContainers.WebMVC.Services;
|
|
using Microsoft.eShopOnContainers.WebMVC.ViewModels.CatalogViewModels;
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
namespace Microsoft.eShopOnContainers.WebMVC.Controllers
|
|
{
|
|
public class CatalogController : Controller
|
|
{
|
|
private ICatalogService _catalogSvc;
|
|
|
|
public CatalogController(ICatalogService catalogSvc) =>
|
|
_catalogSvc = catalogSvc;
|
|
|
|
public async Task<IActionResult> Index(int? BrandFilterApplied, int? TypesFilterApplied, int? page, [FromQuery]string errorMsg)
|
|
{
|
|
var itemsPage = 10;
|
|
var catalog = await _catalogSvc.GetCatalogItems(page ?? 0, itemsPage, BrandFilterApplied, TypesFilterApplied);
|
|
var vm = new IndexViewModel()
|
|
{
|
|
CatalogItems = catalog.Data,
|
|
Brands = await _catalogSvc.GetBrands(),
|
|
Types = await _catalogSvc.GetTypes(),
|
|
BrandFilterApplied = BrandFilterApplied ?? 0,
|
|
TypesFilterApplied = TypesFilterApplied ?? 0,
|
|
PaginationInfo = new PaginationInfo()
|
|
{
|
|
ActualPage = page ?? 0,
|
|
ItemsPerPage = catalog.Data.Count,
|
|
TotalItems = catalog.Count,
|
|
TotalPages = (int)Math.Ceiling(((decimal)catalog.Count / itemsPage))
|
|
}
|
|
};
|
|
|
|
vm.PaginationInfo.Next = (vm.PaginationInfo.ActualPage == vm.PaginationInfo.TotalPages - 1) ? "is-disabled" : "";
|
|
vm.PaginationInfo.Previous = (vm.PaginationInfo.ActualPage == 0) ? "is-disabled" : "";
|
|
|
|
ViewBag.BasketInoperativeMsg = errorMsg;
|
|
|
|
return View(vm);
|
|
}
|
|
}
|
|
} |