public async Task <TorrentsViewModel> GetTorrents(int pageIndex, int itemsPage, string searchText) { var filterSpecification = new CatalogFilterSpecification(searchText); var filterPaginatedSpecification = new CatalogFilterPaginatedSpecification(itemsPage * pageIndex, itemsPage, searchText); var torrentsOnPage = await _torrentRepository.ListAsync(filterPaginatedSpecification); var totalTorrents = await _torrentRepository.CountAsync(filterSpecification); var ci = new TorrentsViewModel { CatalogTorrents = torrentsOnPage.Select(x => new TorrentViewModel() { Id = x.Id, Title = x.Title, Size = x.Size, RegistredAt = x.RegistredAt }), SearchText = searchText, PaginationInfo = new PaginationInfoViewModel() { ActualPage = pageIndex, TorrentsPerPage = torrentsOnPage.Count, TotalTorrents = totalTorrents, TotalPages = int.Parse(Math.Ceiling((decimal)totalTorrents / itemsPage).ToString()) } }; ci.PaginationInfo.Previous = (ci.PaginationInfo.ActualPage == 0) ? "is-disabled" : ""; ci.PaginationInfo.Next = (ci.PaginationInfo.ActualPage == ci.PaginationInfo.TotalPages - 1) ? "is-disabled" : ""; return(ci); }
public override async Task <ActionResult <ListPagedCatalogItemResponse> > HandleAsync([FromQuery] ListPagedCatalogItemRequest request, CancellationToken cancellationToken) { var response = new ListPagedCatalogItemResponse(request.CorrelationId()); var filterSpec = new CatalogFilterSpecification(request.CatalogBrandId, request.CatalogTypeId); int totalItems = await _itemRepository.CountAsync(filterSpec, cancellationToken); var pagedSpec = new CatalogFilterPaginatedSpecification( skip: request.PageIndex * request.PageSize, take: request.PageSize, brandId: request.CatalogBrandId, typeId: request.CatalogTypeId); var items = await _itemRepository.ListAsync(pagedSpec, cancellationToken); response.CatalogItems.AddRange(items.Select(_mapper.Map <CatalogItemDto>)); foreach (CatalogItemDto item in response.CatalogItems) { item.PictureUri = _uriComposer.ComposePicUri(item.PictureUri); } if (request.PageSize > 0) { response.PageCount = int.Parse(Math.Ceiling((decimal)totalItems / request.PageSize).ToString()); } else { response.PageCount = totalItems > 0 ? 1 : 0; } return(Ok(response)); }
public async Task <CatalogIndexViewModel> GetCatalogItems(int pageIndex, int itemsPage, int?brandId, int?typeId) { _logger.LogInformation("GetCatalogItems called."); var filterSpecification = new CatalogFilterSpecification(brandId, typeId); var filterPaginatedSpecification = new CatalogFilterPaginatedSpecification(itemsPage * pageIndex, itemsPage, brandId, typeId); // the implementation below using ForEach and Count. We need a List. var itemsOnPage = _itemRepository.List(filterPaginatedSpecification).ToList(); var totalItems = _itemRepository.Count(filterSpecification); // Initial implementation itemsOnPage.ForEach(x => { x.PictureUri = _uriComposer.ComposePicUri(x.PictureUri); }); // Adding a reference to use an Azure Storage Blob /* * itemsOnPage.ForEach(x => * { * StorageCredentials credentials = new WindowsAzure.Storage.Auth.StorageCredentials("{YOUR_STORAGE_ACCOUNT_NAME}", "{YOUR_STORAGE_PRIMARY_KEY}"); * CloudStorageAccount storageAccount = new WindowsAzure.Storage.CloudStorageAccount(credentials, true); * Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient blobClient = new WindowsAzure.Storage.Blob.CloudBlobClient(storageAccount.BlobStorageUri.PrimaryUri, credentials); * Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob blockblob = new WindowsAzure.Storage.Blob.CloudBlockBlob(new Uri(x.PictureUri)); * var blob = blockblob.OpenReadAsync(); * x.PictureUri = _uriComposer.ComposePicUri(blockblob.Uri.ToString()); * }); */ var vm = new CatalogIndexViewModel() { CatalogItems = itemsOnPage.Select(i => new CatalogItemViewModel() { Id = i.Id, Name = i.Name, PictureUri = i.PictureUri, Price = i.Price }), Brands = await GetBrands(), Types = await GetTypes(), BrandFilterApplied = brandId ?? 0, TypesFilterApplied = typeId ?? 0, PaginationInfo = new PaginationInfoViewModel() { ActualPage = pageIndex, ItemsPerPage = itemsOnPage.Count, TotalItems = totalItems, TotalPages = int.Parse(Math.Ceiling(((decimal)totalItems / itemsPage)).ToString()) } }; vm.PaginationInfo.Next = (vm.PaginationInfo.ActualPage == vm.PaginationInfo.TotalPages - 1) ? "is-disabled" : ""; vm.PaginationInfo.Previous = (vm.PaginationInfo.ActualPage == 0) ? "is-disabled" : ""; return(vm); }
public async Task <GetCatalogItemsPaginatedResponse> Handle ( GetCatalogItemsPaginatedRequest aGetCatalogItemsPaginatedRequest, CancellationToken aCancellationToken ) { var catalogFilterPaginatedSpecification = new CatalogFilterPaginatedSpecification ( skip: aGetCatalogItemsPaginatedRequest.PageSize * aGetCatalogItemsPaginatedRequest.PageIndex, take: aGetCatalogItemsPaginatedRequest.PageSize, brandId: aGetCatalogItemsPaginatedRequest.CatalogBrandId, typeId: aGetCatalogItemsPaginatedRequest.CatalogTypeId ); IReadOnlyList <CatalogItem> catalogItems = await CatalogItemRepository.ListAsync(catalogFilterPaginatedSpecification); var filterSpecification = new CatalogFilterSpecification( aGetCatalogItemsPaginatedRequest.CatalogBrandId, aGetCatalogItemsPaginatedRequest.CatalogTypeId); int totalItems = await CatalogItemRepository.CountAsync(filterSpecification); var response = new GetCatalogItemsPaginatedResponse(aGetCatalogItemsPaginatedRequest.CorrelationId) { CorrelationId = aGetCatalogItemsPaginatedRequest.CorrelationId, PageCount = int.Parse(Math.Ceiling((decimal)totalItems / aGetCatalogItemsPaginatedRequest.PageSize).ToString()) }; response.CatalogItems.AddRange(catalogItems.Select(Mapper.Map <CatalogItemDto>)); foreach (CatalogItemDto item in response.CatalogItems) { item.PictureUri = UriComposer.ComposePicUri(item.PictureUri); } return(response); }
public async Task <CatalogIndexViewModel> GetCatalogItems( int pageIndex, int itemsPage, string searchText, int?brandId, int?typeId, bool convertPrice = true, CancellationToken cancellationToken = default(CancellationToken)) { _logger.LogInformation("GetCatalogItems called."); var pageItemsOffset = itemsPage * pageIndex; var filterSpecification = new CatalogFilterSpecification(searchText, brandId, typeId); var filterPaginatedSpecification = new CatalogFilterPaginatedSpecification( pageItemsOffset, itemsPage, searchText, brandId, typeId); // the implementation below using ForEach and Count. We need a List. var itemsOnPage = await _itemRepository.ListAsync(filterPaginatedSpecification); // var itemsOnPage = await ListCatalogItems( // itemsPage * pageIndex, itemsPage, brandId, typeId); var totalItems = await _itemRepository.CountAsync(filterSpecification); // var totalItems = await CountCatalogItems(brandId, typeId); foreach (var itemOnPage in itemsOnPage) { itemOnPage.PictureUri = string.IsNullOrEmpty(itemOnPage.PictureUri) ? _configuration.GetValue <string>("ImagePictureUri") : _uriComposer.ComposePicUri(itemOnPage.PictureUri); } var CatalogItemsTask = Task.WhenAll(itemsOnPage.Select( catalogItem => CreateCatalogItemViewModelAsync(catalogItem, convertPrice, cancellationToken))); cancellationToken.ThrowIfCancellationRequested(); var vm = new CatalogIndexViewModel() { CatalogItems = await CatalogItemsTask, // catalogItemsList, Brands = await GetBrands(), Types = await GetTypes(), BrandFilterApplied = brandId ?? 0, TypesFilterApplied = typeId ?? 0, PaginationInfo = new PaginationInfoViewModel() { ActualPage = pageIndex, ItemsPerPage = itemsOnPage.Count, TotalItems = totalItems, TotalPages = int.Parse(Math.Ceiling(((decimal)totalItems / itemsPage)).ToString()) } }; vm.PaginationInfo.Next = (vm.PaginationInfo.ActualPage == vm.PaginationInfo.TotalPages - 1) ? "is-disabled" : ""; vm.PaginationInfo.Previous = (vm.PaginationInfo.ActualPage == 0) ? "is-disabled" : ""; return(vm); }
public async Task <Result <List <CatalogItemViewModel> > > Handle(GetCatalogItemsQuery request, CancellationToken cancellationToken) { var filterSpecification = new CatalogFilterSpecification(request.brandId, request.typeId); var filterPaginatedSpecification = new CatalogFilterPaginatedSpecification(request.itemsPage * request.pageIndex, request.itemsPage, request.brandId, request.typeId); // the implementation below using ForEach and Count. We need a List. var itemsOnPage = await _itemRepository.ListAsync(filterPaginatedSpecification); var totalItems = await _itemRepository.CountAsync(filterSpecification); return(default);
public async Task <CatalogIndexViewModel> GetCatalogItems(int pageIndex, int itemsPage, int?brandId, int?typeId, string searchText = null) { _logger.LogInformation("GetCatalogItems called."); var filterSpecification = new CatalogFilterSpecification(brandId, typeId, searchText); var filterPaginatedSpecification = new CatalogFilterPaginatedSpecification(itemsPage * pageIndex, itemsPage, brandId, typeId, searchText); // the implementation below using ForEach and Count. We need a List. var itemsOnPage = await _itemRepository.ListAsync(filterPaginatedSpecification); var totalItems = await _itemRepository.CountAsync(filterSpecification); foreach (var itemOnPage in itemsOnPage) { itemOnPage.PictureUri = _uriComposer.ComposePicUri(itemOnPage.PictureUri); } var brands = await _brandRepository.ListAllAsync(); var vm = new CatalogIndexViewModel() { CatalogItems = itemsOnPage.Select(i => new CatalogItemViewModel() { Id = i.Id, Name = i.Name, PictureUri = i.PictureUri, Price = i.Price, Brand = brands.FirstOrDefault(b => b.Id == i.CatalogBrandId)?.Brand }), Brands = await GetBrands(), Types = await GetTypes(), BrandFilterApplied = brandId ?? 0, TypesFilterApplied = typeId ?? 0, PaginationInfo = new PaginationInfoViewModel() { ActualPage = pageIndex, ItemsPerPage = itemsOnPage.Count, TotalItems = totalItems, TotalPages = int.Parse(Math.Ceiling(((decimal)totalItems / itemsPage)).ToString()) } }; vm.PaginationInfo.Next = (vm.PaginationInfo.ActualPage == vm.PaginationInfo.TotalPages - 1) ? "is-disabled" : ""; vm.PaginationInfo.Previous = (vm.PaginationInfo.ActualPage == 0) ? "is-disabled" : ""; return(vm); }
public async Task <CatalogIndexViewModel> GetCatalogItems(CatalogPageFiltersViewModel catalogPageFiltersViewModel, bool useCache, bool convertPrice = false, CancellationToken cancellationToken = default(CancellationToken)) { _logger.LogInformation("GetCatalogItems called."); var filterSpecification = new CatalogFilterSpecification(catalogPageFiltersViewModel.BrandFilter, catalogPageFiltersViewModel.TypesFilter, catalogPageFiltersViewModel.SearchTextFilter); var filterPaginatedSpecification = new CatalogFilterPaginatedSpecification(catalogPageFiltersViewModel.ItemsPerPage * catalogPageFiltersViewModel.PageId, catalogPageFiltersViewModel.ItemsPerPage, catalogPageFiltersViewModel.SearchTextFilter, catalogPageFiltersViewModel.OrderBy, catalogPageFiltersViewModel.Order, catalogPageFiltersViewModel.BrandFilter, catalogPageFiltersViewModel.TypesFilter); // the implementation below using ForEach and Count. We need a List. var totalItems = await _itemRepository.CountAsync(filterSpecification); var itemsOnPage = await _itemRepository.ListAsync(filterPaginatedSpecification); foreach (var itemOnPage in itemsOnPage) { itemOnPage.PictureUri = _uriComposer.ComposePicUri(itemOnPage.PictureUri); } var catalogItemsTask = await Task.WhenAll(itemsOnPage.Select(catalogItem => CreateCatalogItemViewModel(catalogItem, convertPrice, cancellationToken))); // em caso de algures haver um cancelamento o código para aqui e devolve um erro. Escusa de proceguir no processamento cancellationToken.ThrowIfCancellationRequested(); var vm = new CatalogIndexViewModel() { CatalogItems = catalogItemsTask, Brands = await GetBrands(cancellationToken), Types = await GetTypes(cancellationToken), //BrandFilterApplied = catalogPageFiltersViewModel.BrandFilter ?? 0, //TypesFilterApplied = catalogPageFiltersViewModel.TypesFilter ?? 0, PaginationInfo = new PaginationInfoViewModel() { ActualPage = catalogPageFiltersViewModel.PageId, ItemsPerPage = itemsOnPage.Count, TotalItems = totalItems, TotalPages = catalogPageFiltersViewModel.ItemsPerPage == 0 ? 1 : int.Parse(Math.Ceiling(((decimal)totalItems / catalogPageFiltersViewModel.ItemsPerPage)).ToString()) } }; vm.PaginationInfo.Next = (vm.PaginationInfo.ActualPage == vm.PaginationInfo.TotalPages - 1) ? "is-disabled" : ""; vm.PaginationInfo.Previous = (vm.PaginationInfo.ActualPage == 0) ? "is-disabled" : ""; return(vm); }
public async Task <CatalogIndexViewModel> GetCatalogItems(int pageIndex, int itemsPage, int?brandId, int?typeId) { _logger.LogInformation("Get CatalogeItems Called.."); var filterSpecification = new CatalogFilterSpecification(brandId, typeId); var filterPaginatedSpecification = new CatalogFilterPaginatedSpecification(itemsPage * pageIndex, itemsPage, brandId, typeId); var itemsOnPage = await _itemRepository.ListAsync(filterPaginatedSpecification); var totalItems = await _itemRepository.CountAsync(filterSpecification); foreach (var itemOnPage in itemsOnPage) { itemOnPage.PictureUri = _uriComposer.ComposePicUri(itemOnPage.PictureUri); } var vm = new CatalogIndexViewModel() { CatalogItems = itemsOnPage.Select(i => new CatalogItemViewModel() { Id = i.Id, Name = i.Name, PictureUri = i.PictureUri, Price = i.Price }), Brands = await GetBrands(), Types = await GetTypes(), BrandFilterApplied = brandId ?? 0, TypesFilterApplied = typeId ?? 0, PaginationInfo = new PaginationInfoViewModel() { ActualPage = pageIndex, ItemsPerPage = itemsOnPage.Count, TotalItems = totalItems, TotalPages = int.Parse(Math.Ceiling(((decimal)totalItems / itemsPage)).ToString()) } }; vm.PaginationInfo.Next = (vm.PaginationInfo.ActualPage == vm.PaginationInfo.TotalPages - 1) ? "is-disabled" : ""; vm.PaginationInfo.Previous = (vm.PaginationInfo.ActualPage == 0) ? "is-disabled" : ""; return(vm); }
public async Task <CatalogIndexViewModel> GetCatalogItems(int pageIndex, int itemsPage, int?brandId, int?typeId) { _logger.LogInformation("GetCatalogItems called."); var filterSpecification = new CatalogFilterSpecification(brandId, typeId); var filterPaginatedSpecification = new CatalogFilterPaginatedSpecification(itemsPage * pageIndex, itemsPage, brandId, typeId); // the implementation below using ForEach and Count. We need a List. var itemsOnPage = _itemRepository.List(filterPaginatedSpecification).ToList(); var totalItems = _itemRepository.Count(filterSpecification); itemsOnPage.ForEach(x => { x.PictureUri = "pic/" + x.PictureUri; }); var vm = new CatalogIndexViewModel() { CatalogItems = itemsOnPage.Select(i => new CatalogItemViewModel() { Id = i.Id, Name = i.Name, PictureUri = i.PictureUri, Price = i.Price }), Brands = await GetBrands(), Types = await GetTypes(), BrandFilterApplied = brandId ?? 0, TypesFilterApplied = typeId ?? 0, PaginationInfo = new PaginationInfoViewModel() { ActualPage = pageIndex, ItemsPerPage = itemsOnPage.Count, TotalItems = totalItems, TotalPages = int.Parse(Math.Ceiling(((decimal)totalItems / itemsPage)).ToString()) } }; vm.PaginationInfo.Next = (vm.PaginationInfo.ActualPage == vm.PaginationInfo.TotalPages - 1) ? "is-disabled" : ""; vm.PaginationInfo.Previous = (vm.PaginationInfo.ActualPage == 0) ? "is-disabled" : ""; return(vm); }
public async Task <CatalogIndexViewModel> GetCatalogItems(int pageIndex, int itemsPage, int?brandId, int?typeId) { // pageIndex is current page _logger.LogInformation("GetCatalogItems called"); var filterSpecification = new CatalogFilterSpecification(brandId, typeId); // this contains all catalogitems with matching brand&type ids var filterPaginatedSpecifition = new CatalogFilterPaginatedSpecification(itemsPage * pageIndex, itemsPage, brandId, typeId); // this contains all catalogItem per page var itemsPerPage = await _itemRepository.ListAsync(filterPaginatedSpecifition); var totalItems = await _itemRepository.CountAsync(filterSpecification); var vm = new CatalogIndexViewModel() { catalogItems = itemsPerPage.Select(i => new CatalogItemViewModel() { id = i.id, name = i.Name, pictureUri = _uriComposer.ComposePictureUri(i.PictureUri), price = i.Price }).ToList(), //for List<SelectListItem> brands brands = (List <SelectListItem>) await GetBrands(), // this is an explicit cast types = (List <SelectListItem>) await GetTypes(), brandFilterApplied = brandId ?? 0, // if brandId not null pass it, else pass 0 typeFilterApplied = typeId ?? 0, paginationInfo = new PaginationInfoViewModel() { totalItems = totalItems, itemsPerPage = itemsPerPage.Count, actualPage = pageIndex, totalPages = int.Parse(Math.Ceiling((decimal)totalItems / itemsPerPage.Count).ToString()) // string next and previous are defined seperately below } }; // "is-disabled" will be render into HTML that disables next/previous button vm.paginationInfo.next = (vm.paginationInfo.actualPage == vm.paginationInfo.totalPages - 1) ? "is-disabled" : ""; vm.paginationInfo.previous = (vm.paginationInfo.actualPage == 0) ? "is-disabled" : ""; return(vm); }
public async Task <Result <ShopViewModel> > Handle(GetShopModelQuery request, CancellationToken cancellationToken) { var filterSpecification = new CatalogFilterSpecification(request.brandId, request.typeId); var filterPaginatedSpecification = new CatalogFilterPaginatedSpecification(request.itemsPage * request.pageIndex, request.itemsPage, request.brandId, request.typeId); // the implementation below using ForEach and Count. We need a List. var itemsOnPage = await _itemRepository.ListAsync(filterPaginatedSpecification); var totalItems = await _itemRepository.CountAsync(filterSpecification); var vm = new ShopViewModel() { CatalogItems = itemsOnPage.Select(i => new CatalogItemViewModel() { Id = i.Id, Name = i.Name, PictureUri = _uriComposer.ComposePicUri(i.PictureUri), Price = i.Price }), Brands = await GetBrands(), Types = await GetTypes(), BrandFilterApplied = request.brandId ?? 0, TypesFilterApplied = request.typeId ?? 0, PaginationInfo = new PaginationInfoViewModel() { ActualPage = request.pageIndex, ItemsPerPage = itemsOnPage.Count, TotalItems = totalItems, TotalPages = int.Parse(Math.Ceiling(((decimal)totalItems / request.itemsPage)).ToString()) } }; vm.PaginationInfo.Next = (vm.PaginationInfo.ActualPage == vm.PaginationInfo.TotalPages - 1) ? "is-disabled" : ""; vm.PaginationInfo.Previous = (vm.PaginationInfo.ActualPage == 0) ? "is-disabled" : ""; return(Result <ShopViewModel> .Success(vm)); }