コード例 #1
0
        public virtual async Task <StoreSearchResult> SearchStoresAsync(StoreSearchCriteria criteria)
        {
            var cacheKey = CacheKey.With(GetType(), "SearchStoresAsync", criteria.GetCacheKey());

            return(await _platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) =>
            {
                cacheEntry.AddExpirationToken(StoreCacheRegion.CreateChangeToken());
                var result = AbstractTypeFactory <StoreSearchResult> .TryCreateInstance();
                using (var repository = _repositoryFactory())
                {
                    var sortInfos = criteria.SortInfos;
                    if (sortInfos.IsNullOrEmpty())
                    {
                        sortInfos = new[]
                        {
                            new SortInfo
                            {
                                SortColumn = "Name"
                            }
                        };
                    }

                    var query = GetStoresQuery(repository, criteria, sortInfos);

                    result.TotalCount = await query.CountAsync();
                    if (criteria.Take > 0)
                    {
                        var storeIds = await query.Select(x => x.Id).Skip(criteria.Skip).Take(criteria.Take).ToArrayAsync();
                        result.Results = (await _storeService.GetByIdsAsync(storeIds)).AsQueryable().OrderBySortInfos(sortInfos).ToList();
                    }
                }
                return result;
            }));
        }
コード例 #2
0
        public async Task <GenericSearchResult <Store> > SearchStores([FromBody] StoreSearchCriteria criteria)
        {
            //Filter resulting stores correspond to current user permissions
            //first check global permission
            //TODO
            //if (!_securityService.UserHasAnyPermission(User.Identity.Name, null, StorePredefinedPermissions.Read))
            //{
            //    //Get user 'read' permission scopes
            //    criteria.StoreIds = _securityService.GetUserPermissions(User.Identity.Name)
            //                                          .Where(x => x.Id.StartsWith(StorePredefinedPermissions.Read))
            //                                          .SelectMany(x => x.AssignedScopes)
            //                                          .OfType<StoreSelectedScope>()
            //                                          .Select(x => x.Scope)
            //                                          .ToArray();
            //    //Do not return all stores if user don't have corresponding permission
            //    if (criteria.StoreIds.IsNullOrEmpty())
            //    {
            //        throw new HttpResponseException(HttpStatusCode.Unauthorized);
            //    }
            //}

            var result = await _storeSearchService.SearchStoresAsync(criteria);

            return(result);
        }
コード例 #3
0
        public async Task <IActionResult> GetStores()
        {
            var criteria = new StoreSearchCriteria
            {
                Skip = 0,
                Take = int.MaxValue
            };
            var storesSearchResult = await SearchStores(criteria);

            return(Ok(storesSearchResult.Results));
        }
コード例 #4
0
        public async Task <GenericSearchResult <Store> > SearchStoresAsync(StoreSearchCriteria criteria)
        {
            var cacheKey = CacheKey.With(GetType(), "SearchStoresAsync", criteria.GetCacheKey());

            return(await _platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) =>
            {
                cacheEntry.AddExpirationToken(StoreSearchCacheRegion.CreateChangeToken());
                var result = new GenericSearchResult <Store>();
                using (var repository = _repositoryFactory())
                {
                    var query = repository.Stores;
                    if (!string.IsNullOrEmpty(criteria.Keyword))
                    {
                        query = query.Where(x => x.Name.Contains(criteria.Keyword) || x.Id.Contains(criteria.Keyword));
                    }
                    if (!criteria.StoreIds.IsNullOrEmpty())
                    {
                        query = query.Where(x => criteria.StoreIds.Contains(x.Id));
                    }
                    var sortInfos = criteria.SortInfos;
                    if (sortInfos.IsNullOrEmpty())
                    {
                        sortInfos = new[]
                        {
                            new SortInfo
                            {
                                SortColumn = "Name"
                            }
                        };
                    }

                    query = query.OrderBySortInfos(sortInfos);

                    result.TotalCount = await query.CountAsync();
                    var list = await query.Skip(criteria.Skip).Take(criteria.Take).ToArrayAsync();
                    result.Results = list.Select(x => x.ToModel(AbstractTypeFactory <Store> .TryCreateInstance())).ToList();
                }
                return result;
            }));
        }
コード例 #5
0
        protected virtual IQueryable <StoreEntity> GetStoresQuery(IStoreRepository repository, StoreSearchCriteria criteria,
                                                                  IEnumerable <SortInfo> sortInfos)
        {
            var query = repository.Stores;

            if (!string.IsNullOrEmpty(criteria.Keyword))
            {
                query = query.Where(x => x.Name.Contains(criteria.Keyword) || x.Id.Contains(criteria.Keyword));
            }
            if (!criteria.StoreIds.IsNullOrEmpty())
            {
                query = query.Where(x => criteria.StoreIds.Contains(x.Id));
            }

            query = query.OrderBySortInfos(sortInfos);
            return(query);
        }