public async Task <DynamicContentItemSearchResult> SearchContentItemsAsync(DynamicContentItemSearchCriteria criteria)
        {
            var cacheKey = CacheKey.With(GetType(), nameof(SearchContentItemsAsync), criteria.GetCacheKey());

            return(await _platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) =>
            {
                cacheEntry.AddExpirationToken(DynamicContentItemCacheRegion.CreateChangeToken());
                var result = AbstractTypeFactory <DynamicContentItemSearchResult> .TryCreateInstance();
                using (var repository = _repositoryFactory())
                {
                    var sortInfos = BuildSortExpression(criteria);
                    var query = BuildQuery(criteria, repository);

                    result.TotalCount = await query.CountAsync();

                    if (criteria.Take > 0)
                    {
                        var ids = await query.OrderBySortInfos(sortInfos).ThenBy(x => x.Id)
                                  .Select(x => x.Id)
                                  .Skip(criteria.Skip).Take(criteria.Take)
                                  .ToArrayAsync();
                        result.Results = (await _dynamicContentService.GetContentItemsByIdsAsync(ids))
                                         .OrderBy(x => Array.IndexOf(ids, x.Id)).ToList();
                    }
                }
                return result;
            }));
        }
예제 #2
0
        public async Task <DynamicContentItem[]> GetContentItemsByIdsAsync(string[] ids)
        {
            var cacheKey = CacheKey.With(GetType(), "GetContentItemsByIdsAsync", string.Join("-", ids));

            return(await _platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) =>
            {
                cacheEntry.AddExpirationToken(DynamicContentItemCacheRegion.CreateChangeToken());
                DynamicContentItem[] retVal = null;
                using (var repository = _repositoryFactory())
                {
                    retVal = (await repository.GetContentItemsByIdsAsync(ids)).Select(x => x.ToModel(AbstractTypeFactory <DynamicContentItem> .TryCreateInstance())).ToArray();
                }
                return retVal;
            }));
        }
예제 #3
0
        public async Task DeleteContentItemsAsync(string[] ids)
        {
            var items = await GetContentItemsByIdsAsync(ids);

            var changedEntries = items.Select(x => new GenericChangedEntry <DynamicContentItem>(x, EntryState.Deleted));

            using (var repository = _repositoryFactory())
            {
                await repository.RemoveContentItemsAsync(ids);

                await repository.UnitOfWork.CommitAsync();
            }
            await _eventPublisher.Publish(new DynamicContentItemChangedEvent(changedEntries));

            DynamicContentItemCacheRegion.ExpireRegion();
        }
예제 #4
0
        public async Task <DynamicContentItemSearchResult> SearchContentItemsAsync(DynamicContentItemSearchCriteria criteria)
        {
            var cacheKey = CacheKey.With(GetType(), "SearchContentItemsAsync", criteria.GetCacheKey());

            return(await _platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) =>
            {
                cacheEntry.AddExpirationToken(DynamicContentItemCacheRegion.CreateChangeToken());
                var retVal = AbstractTypeFactory <DynamicContentItemSearchResult> .TryCreateInstance();
                using (var repository = _repositoryFactory())
                {
                    var query = repository.Items;
                    if (!string.IsNullOrEmpty(criteria.FolderId))
                    {
                        query = query.Where(x => x.FolderId == criteria.FolderId);
                    }
                    if (!string.IsNullOrEmpty(criteria.Keyword))
                    {
                        query = query.Where(q => q.Name.Contains(criteria.Keyword));
                    }

                    var sortInfos = criteria.SortInfos;
                    if (sortInfos.IsNullOrEmpty())
                    {
                        sortInfos = new[] { new SortInfo {
                                                SortColumn = ReflectionUtility.GetPropertyName <DynamicContentItem>(x => x.Name), SortDirection = SortDirection.Ascending
                                            } };
                    }
                    query = query.OrderBySortInfos(sortInfos);

                    retVal.TotalCount = await query.CountAsync();

                    if (criteria.Take > 0)
                    {
                        var ids = await query.Select(x => x.Id).Skip(criteria.Skip).Take(criteria.Take).ToArrayAsync();
                        retVal.Results = await _dynamicContentService.GetContentItemsByIdsAsync(ids);
                    }
                }
                return retVal;
            }));
        }
예제 #5
0
        public async Task SaveContentItemsAsync(DynamicContentItem[] items)
        {
            var pkMap          = new PrimaryKeyResolvingMap();
            var changedEntries = new List <GenericChangedEntry <DynamicContentItem> >();

            using (var repository = _repositoryFactory())
            {
                var existEntities = await repository.GetContentItemsByIdsAsync(items.Where(x => !x.IsTransient()).Select(x => x.Id).ToArray());

                foreach (var item in items)
                {
                    var sourceEntity = AbstractTypeFactory <DynamicContentItemEntity> .TryCreateInstance();

                    if (sourceEntity != null)
                    {
                        sourceEntity = sourceEntity.FromModel(item, pkMap);
                        var targetEntity = existEntities.FirstOrDefault(x => x.Id == item.Id);
                        if (targetEntity != null)
                        {
                            changedEntries.Add(new GenericChangedEntry <DynamicContentItem>(item, targetEntity.ToModel(AbstractTypeFactory <DynamicContentItem> .TryCreateInstance()), EntryState.Modified));
                            sourceEntity.Patch(targetEntity);
                        }
                        else
                        {
                            changedEntries.Add(new GenericChangedEntry <DynamicContentItem>(item, EntryState.Added));
                            repository.Add(sourceEntity);
                        }
                    }
                }

                await repository.UnitOfWork.CommitAsync();

                pkMap.ResolvePrimaryKeys();

                DynamicContentItemCacheRegion.ExpireRegion();

                await _eventPublisher.Publish(new DynamicContentItemChangedEvent(changedEntries));
            }
        }