/// <summary>
        /// Gets a product category mapping collection
        /// </summary>
        /// <param name="productId">Product identifier</param>
        /// <param name="storeId">Store identifier (used in multi-store environment). "showHidden" parameter should also be "true"</param>
        /// <param name="showHidden"> A value indicating whether to show hidden records</param>
        /// <returns>
        /// A task that represents the asynchronous operation
        /// The task result contains the product category mapping collection
        /// </returns>
        protected virtual async Task <IList <ProductCategory> > GetProductCategoriesByProductIdAsync(int productId, int storeId,
                                                                                                     bool showHidden = false)
        {
            if (productId == 0)
            {
                return(new List <ProductCategory>());
            }

            var customer = await _workContext.GetCurrentCustomerAsync();

            return(await _productCategoryRepository.GetAllAsync(async query =>
            {
                if (!showHidden)
                {
                    var categoriesQuery = _categoryRepository.Table.Where(c => c.Published);

                    //apply store mapping constraints
                    categoriesQuery = await _storeMappingService.ApplyStoreMapping(categoriesQuery, storeId);

                    //apply ACL constraints
                    categoriesQuery = await _aclService.ApplyAcl(categoriesQuery, customer);

                    query = query.Where(pc => categoriesQuery.Any(c => !c.Deleted && c.Id == pc.CategoryId));
                }

                return query
                .Where(pc => pc.ProductId == productId)
                .OrderBy(pc => pc.DisplayOrder)
                .ThenBy(pc => pc.Id);
            }, cache => _staticCacheManager.PrepareKeyForDefaultCache(NopCatalogDefaults.ProductCategoriesByProductCacheKey,
                                                                      productId, showHidden, customer, storeId)));
        }
        /// <summary>
        /// Gets all manufacturers
        /// </summary>
        /// <param name="manufacturerName">Manufacturer name</param>
        /// <param name="storeId">Store identifier; 0 if you want to get all records</param>
        /// <param name="pageIndex">Page index</param>
        /// <param name="pageSize">Page size</param>
        /// <param name="showHidden">A value indicating whether to show hidden records</param>
        /// <param name="overridePublished">
        /// null - process "Published" property according to "showHidden" parameter
        /// true - load only "Published" products
        /// false - load only "Unpublished" products
        /// </param>
        /// <returns>Manufacturers</returns>
        public virtual async Task <IPagedList <Manufacturer> > GetAllManufacturersAsync(string manufacturerName = "",
                                                                                        int storeId             = 0,
                                                                                        int pageIndex           = 0,
                                                                                        int pageSize            = int.MaxValue,
                                                                                        bool showHidden         = false,
                                                                                        bool?overridePublished  = null)
        {
            return(await _manufacturerRepository.GetAllPagedAsync(async query =>
            {
                if (!showHidden)
                {
                    query = query.Where(m => m.Published);
                }
                else if (overridePublished.HasValue)
                {
                    query = query.Where(m => m.Published == overridePublished.Value);
                }

                //apply store mapping constraints
                query = await _storeMappingService.ApplyStoreMapping(query, storeId);

                //apply ACL constraints
                if (!showHidden)
                {
                    var customer = await _workContext.GetCurrentCustomerAsync();
                    query = await _aclService.ApplyAcl(query, customer);
                }

                query = query.Where(m => !m.Deleted);

                if (!string.IsNullOrWhiteSpace(manufacturerName))
                {
                    query = query.Where(m => m.Name.Contains(manufacturerName));
                }

                return query.OrderBy(m => m.DisplayOrder).ThenBy(m => m.Id);
            }, pageIndex, pageSize));
        }
Exemple #3
0
        /// <summary>
        /// Gets a topic
        /// </summary>
        /// <param name="systemName">The topic system name</param>
        /// <param name="storeId">Store identifier; pass 0 to ignore filtering by store and load the first one</param>
        /// <param name="showHidden">A value indicating whether to show hidden records</param>
        /// <returns>Topic</returns>
        public virtual async Task <Topic> GetTopicBySystemNameAsync(string systemName, int storeId = 0, bool showHidden = false)
        {
            if (string.IsNullOrEmpty(systemName))
            {
                return(null);
            }

            var customer = await _workContext.GetCurrentCustomerAsync();

            var customerRoleIds = await _customerService.GetCustomerRoleIdsAsync(customer);

            var cacheKey = _staticCacheManager.PrepareKeyForDefaultCache(NopTopicDefaults.TopicBySystemNameCacheKey, systemName, storeId, customerRoleIds);

            var topic = await _staticCacheManager.GetAsync(cacheKey, async() =>
            {
                var query = _topicRepository.Table;

                if (!showHidden)
                {
                    query = query.Where(t => t.Published);
                }

                //apply store mapping constraints
                query = await _storeMappingService.ApplyStoreMapping(query, storeId);

                //apply ACL constraints
                if (!showHidden)
                {
                    query = await _aclService.ApplyAcl(query, customerRoleIds);
                }

                return(query.Where(t => t.SystemName == systemName)
                       .OrderBy(t => t.Id)
                       .FirstOrDefault());
            });

            return(topic);
        }
Exemple #4
0
        /// <summary>
        /// Filter hidden entries according to constraints if any
        /// </summary>
        /// <param name="query">Query to filter</param>
        /// <param name="storeId">A store identifier</param>
        /// <param name="customerRoleIds">Identifiers of customer's roles</param>
        /// <returns>Filtered query</returns>
        protected virtual IQueryable <TEntity> FilterHiddenEntries <TEntity>(IQueryable <TEntity> query, int storeId, int[] customerRoleIds)
            where TEntity : Category
        {
            //filter unpublished entries
            query = query.Where(entry => entry.Published);

            //apply store mapping constraints
            if (!_catalogSettings.IgnoreStoreLimitations && _storeMappingService.IsEntityMappingExists <TEntity>(storeId))
            {
                query = query.Where(_storeMappingService.ApplyStoreMapping <TEntity>(storeId));
            }

            //apply ACL constraints
            if (!_catalogSettings.IgnoreAcl && _aclService.IsEntityAclMappingExist <TEntity>(customerRoleIds))
            {
                query = query.Where(_aclService.ApplyAcl <TEntity>(customerRoleIds));
            }

            return(query);
        }
Exemple #5
0
        /// <summary>
        /// Filter hidden entries according to constraints if any
        /// </summary>
        /// <param name="query">Query to filter</param>
        /// <param name="storeId">A store identifier</param>
        /// <param name="customerRolesIds">Identifiers of customer's roles</param>
        /// <returns>Filtered query</returns>
        protected virtual async Task <IQueryable <TEntity> > FilterHiddenEntriesAsync <TEntity>(IQueryable <TEntity> query,
                                                                                                int storeId, int[] customerRolesIds)
            where TEntity : Manufacturer
        {
            //filter unpublished entries
            query = query.Where(entry => entry.Published);

            //apply store mapping constraints
            if (!_catalogSettings.IgnoreStoreLimitations && await _storeMappingService.IsEntityMappingExistsAsync <TEntity>(storeId))
            {
                query = query.Where(_storeMappingService.ApplyStoreMapping <TEntity>(storeId));
            }

            //apply ACL constraints
            if (!_catalogSettings.IgnoreAcl && await _aclService.IsEntityAclMappingExistAsync <TEntity>(customerRolesIds))
            {
                query = query.Where(_aclService.ApplyAcl <TEntity>(customerRolesIds));
            }

            return(query);
        }
        protected virtual async Task <IQueryable <Product> > GetAvailableProductsQueryAsync()
        {
            var productsQuery =
                from p in _productRepository.Table
                where !p.Deleted && p.Published &&
                (p.ParentGroupedProductId == 0 || p.VisibleIndividually) &&
                (!p.AvailableStartDateTimeUtc.HasValue || p.AvailableStartDateTimeUtc <= DateTime.UtcNow) &&
                (!p.AvailableEndDateTimeUtc.HasValue || p.AvailableEndDateTimeUtc >= DateTime.UtcNow)
                select p;

            var store = await _storeContext.GetCurrentStoreAsync();

            var currentCustomer = await _workContext.GetCurrentCustomerAsync();

            //apply store mapping constraints
            productsQuery = await _storeMappingService.ApplyStoreMapping(productsQuery, store.Id);

            //apply ACL constraints
            productsQuery = await _aclService.ApplyAcl(productsQuery, currentCustomer);

            return(productsQuery);
        }
Exemple #7
0
        /// <summary>
        /// Get product count for every linked tag
        /// </summary>
        /// <param name="storeId">Store identifier</param>
        /// <param name="showHidden">A value indicating whether to show hidden records</param>
        /// <returns>
        /// A task that represents the asynchronous operation
        /// The task result contains the dictionary of "product tag ID : product count"
        /// </returns>
        public virtual async Task <Dictionary <int, int> > GetProductCountAsync(int storeId, bool showHidden = false)
        {
            var customer = await _workContext.GetCurrentCustomerAsync();

            var customerRoleIds = await _customerService.GetCustomerRoleIdsAsync(customer);

            var key = _staticCacheManager.PrepareKeyForDefaultCache(NopCatalogDefaults.ProductTagCountCacheKey, storeId, customerRoleIds, showHidden);

            return(await _staticCacheManager.GetAsync(key, async() =>
            {
                var query = _productProductTagMappingRepository.Table;

                if (!showHidden)
                {
                    var productsQuery = _productRepository.Table.Where(p => p.Published);

                    //apply store mapping constraints
                    productsQuery = await _storeMappingService.ApplyStoreMapping(productsQuery, storeId);

                    //apply ACL constraints
                    productsQuery = await _aclService.ApplyAcl(productsQuery, customerRoleIds);

                    query = query.Where(pc => productsQuery.Any(p => !p.Deleted && pc.ProductId == p.Id));
                }

                var pTagCount = from pt in _productTagRepository.Table
                                join ptm in query on pt.Id equals ptm.ProductTagId
                                group ptm by ptm.ProductTagId into ptmGrouped
                                select new
                {
                    ProductTagId = ptmGrouped.Key,
                    ProductCount = ptmGrouped.Count()
                };

                return pTagCount.ToDictionary(item => item.ProductTagId, item => item.ProductCount);
            }));
        }