示例#1
0
        private static RepositoryResponse <PaginationModel <TView> > GetSortedPostByValue <TView>(
            Expression <Func <MixPost, bool> > predicate,
            SearchPostQueryModel searchPostData,
            MixCmsContext context,
            IDbContextTransaction transaction)
            where TView : ViewModelBase <MixCmsContext, MixPost, TView>
        {
            var total      = context.MixPost.Count(predicate);
            var allPostIds = context.MixPost.Where(predicate)
                             .AsEnumerable()
                             .Select(m => m.Id);

            var posts = IQueryableHelper.GetSortedPost(allPostIds, context, searchPostData).ToList();

            return(new RepositoryResponse <PaginationModel <TView> >()
            {
                IsSucceed = true,
                Data = new PaginationModel <TView>()
                {
                    Items = DefaultRepository <MixCmsContext, MixPost, TView> .Instance.GetCachedData(posts, context, transaction),
                    PageSize = searchPostData.PagingData.PageSize,
                    PageIndex = searchPostData.PagingData.PageIndex
                }
            });
        }
示例#2
0
 private static List <int> GetSortedIdsByValue(IQueryable <string> allPostIds, MixCmsContext context, PagingRequest pagingData, string specificulture, string postType)
 {
     return(IQueryableHelper.SortParentIds(allPostIds,
                                           context,
                                           pagingData,
                                           specificulture,
                                           postType)
            .AsEnumerable()
            .Select(p => int.Parse(p))
            .ToList());
 }
示例#3
0
        private static async Task <RepositoryResponse <PaginationModel <TView> > > SearchPostByValue <TView>(
            Expression <Func <MixDatabaseDataValue, bool> > valPredicate,
            PagingRequest pagingData,
            string postType,
            string specificulture,
            MixCmsContext context,
            IDbContextTransaction transaction)
            where TView : ViewModelBase <MixCmsContext, MixPost, TView>
        {
            var allPostIds = IQueryableHelper.GetPostIdsByValue(
                valPredicate,
                context,
                specificulture,
                postType);
            var resultIds = IQueryableHelper.SortParentIds(
                allPostIds.Skip(pagingData.PageIndex * pagingData.PageSize)
                .Take(pagingData.PageSize),
                context,
                pagingData,
                specificulture,
                postType)
                            .AsEnumerable()
                            .Select(p => int.Parse(p))
                            .ToList();

            var getPosts = (await DefaultRepository <MixCmsContext, MixPost, TView> .Instance.GetModelListByAsync(
                                m => resultIds.Any(p => p == m.Id) && m.Specificulture == specificulture,
                                context,
                                transaction));
            var items = getPosts.Data.OrderBy(
                m => resultIds.IndexOf((int)ReflectionHelper.GetPropertyValue(m, "Id"))).ToList();
            var total  = allPostIds.Count();
            var result = new RepositoryResponse <PaginationModel <TView> >()
            {
                IsSucceed = true,
                Data      = new PaginationModel <TView>()
                {
                    Items      = items,
                    PageIndex  = pagingData.PageIndex,
                    PageSize   = pagingData.PageSize,
                    TotalItems = total,
                    TotalPage  = (int)Math.Ceiling((double)total / pagingData.PageSize)
                }
            };

            return(result);
        }
示例#4
0
        private static async Task <RepositoryResponse <PaginationModel <TView> > > SearchPostByValue <TView>(
            Expression <Func <MixDatabaseDataValue, bool> > valPredicate,
            SearchPostQueryModel searchPostData,
            MixCmsContext context,
            IDbContextTransaction transaction)
            where TView : ViewModelBase <MixCmsContext, MixPost, TView>
        {
            var allPostIds = IQueryableHelper.GetPostIdsByValue(
                valPredicate,
                context,
                searchPostData.Specificulture,
                searchPostData.PostType);

            var resultIds = searchPostData.PagingData.OrderBy.StartsWith("additionalData.")
                ? GetSortedIdsByValue(allPostIds, context, searchPostData.PagingData, searchPostData.Specificulture, searchPostData.PostType)
                : searchPostData.PageId.HasValue
                    ? GetSortedIdsByPage(allPostIds, searchPostData, context, transaction)
                    : allPostIds.Skip(searchPostData.PagingData.PageIndex * searchPostData.PagingData.PageSize)
                            .Take(searchPostData.PagingData.PageSize)
                            .Select(p => int.Parse(p)).ToList();


            var getPosts = (await DefaultRepository <MixCmsContext, MixPost, TView> .Instance.GetModelListByAsync(
                                m => resultIds.Any(p => p == m.Id) && m.Specificulture == searchPostData.Specificulture,
                                context,
                                transaction));
            var items = getPosts.Data.OrderBy(
                m => resultIds.IndexOf((int)ReflectionHelper.GetPropertyValue(m, "Id"))).ToList();
            var total  = allPostIds.Count();
            var result = new RepositoryResponse <PaginationModel <TView> >()
            {
                IsSucceed = true,
                Data      = new PaginationModel <TView>()
                {
                    Items      = items,
                    PageIndex  = searchPostData.PagingData.PageIndex,
                    PageSize   = searchPostData.PagingData.PageSize,
                    TotalItems = total,
                    TotalPage  = (int)Math.Ceiling((double)total / searchPostData.PagingData.PageSize)
                }
            };

            return(result);
        }
示例#5
0
        public static async Task <RepositoryResponse <PaginationModel <TView> > > SearchPosts <TView>(
            SearchPostQueryModel searchPostData,
            MixCmsContext _context             = null,
            IDbContextTransaction _transaction = null)
            where TView : ViewModelBase <MixCmsContext, MixPost, TView>
        {
            UnitOfWorkHelper <MixCmsContext> .InitTransaction(_context, _transaction, out MixCmsContext context, out IDbContextTransaction transaction, out bool isRoot);

            try
            {
                Expression <Func <MixDatabaseDataValue, bool> > valPredicate = null;
                valPredicate = valPredicate.AndAlsoIf(
                    !string.IsNullOrEmpty(searchPostData.Category),
                    Expressions.GetMetaExpression(MixDatabaseNames.SYSTEM_CATEGORY, searchPostData.Category, searchPostData.Specificulture));
                valPredicate = valPredicate.AndAlsoIf(
                    !string.IsNullOrEmpty(searchPostData.Tag),
                    Expressions.GetMetaExpression(MixDatabaseNames.SYSTEM_TAG, searchPostData.Tag, searchPostData.Specificulture));

                if (valPredicate != null)
                {
                    return(await SearchPostByValue <TView>(valPredicate, searchPostData.PagingData, searchPostData.PostType, searchPostData.Specificulture, context, transaction));
                }
                else
                {
                    Expression <Func <MixPost, bool> > predicate = BuildPostExpression(searchPostData);
                    if (searchPostData.PagingData.OrderBy.StartsWith("additionalData."))
                    {
                        var total      = context.MixPost.Count(predicate);
                        var allPostIds = context.MixPost.Where(predicate)
                                         .AsEnumerable()
                                         .Select(m => m.Id);

                        var posts = IQueryableHelper.GetSortedPost(allPostIds, context, searchPostData).ToList();
                        return(new RepositoryResponse <PaginationModel <TView> >()
                        {
                            IsSucceed = true,
                            Data = new PaginationModel <TView>()
                            {
                                Items = DefaultRepository <MixCmsContext, MixPost, TView> .Instance.GetCachedData(posts, context, transaction),
                                PageSize = searchPostData.PagingData.PageSize,
                                PageIndex = searchPostData.PagingData.PageIndex
                            }
                        });
                    }
                    return(await DefaultRepository <MixCmsContext, MixPost, TView> .Instance.GetModelListByAsync(
                               predicate,
                               searchPostData.PagingData.OrderBy,
                               searchPostData.PagingData.Direction,
                               searchPostData.PagingData.PageSize,
                               searchPostData.PagingData.PageIndex,
                               null, null,
                               context,
                               transaction));
                }
            }
            catch (Exception ex)
            {
                return(UnitOfWorkHelper <MixCmsContext> .HandleException <PaginationModel <TView> >(ex, isRoot, transaction));
            }
            finally
            {
                if (isRoot)
                {
                    //if current Context is Root
                    UnitOfWorkHelper <MixCmsContext> .CloseDbContext(ref context, ref transaction);
                }
            }
        }