Пример #1
0
        /// <summary>
        /// Gets the modelist by meta.
        /// </summary>
        /// <typeparam name="TView">The type of the view.</typeparam>
        /// <param name="culture">The culture.</param>
        /// <param name="metaName">Name of the meta. Ex: sys_tag / sys_category</param>
        /// <param name="metaValue">The meta value.</param>
        /// <param name="orderByPropertyName">Name of the order by property.</param>
        /// <param name="direction">The direction.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="pageIndex">Index of the page.</param>
        /// <param name="_context">The context.</param>
        /// <param name="_transaction">The transaction.</param>
        /// <returns></returns>
        public static async Task <RepositoryResponse <PaginationModel <TView> > > GetModelistByMeta <TView>(
            string metaName, string metaValue
            , string culture             = null
            , string orderByPropertyName = "CreatedDateTime"
            , Heart.Enums.MixHeartEnums.DisplayDirection direction = Heart.Enums.MixHeartEnums.DisplayDirection.Desc
            , int?pageSize           = null, int?pageIndex = null
            , 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
            {
                culture = culture ?? MixService.GetConfig <string>("DefaultCulture");
                var result = new RepositoryResponse <PaginationModel <TView> >()
                {
                    IsSucceed = true,
                    Data      = new PaginationModel <TView>()
                    {
                        PageIndex = pageIndex.HasValue ? pageIndex.Value : 0,
                        PageSize  = pageSize
                    }
                };
                // Get Tag
                var getVal = await MixAttributeSetValues.ReadViewModel.Repository.GetSingleModelAsync(
                    m => m.Specificulture == culture && m.Status == MixEnums.MixContentStatus.Published.ToString() &&
                    m.AttributeSetName == metaName && m.StringValue == metaValue
                    , context, transaction);

                if (getVal.IsSucceed)
                {
                    result = await GetPostListByDataId <TView>(
                        dataId : getVal.Data.DataId,
                        culture : culture,
                        orderByPropertyName : orderByPropertyName,
                        direction : direction,
                        pageSize : pageSize,
                        pageIndex : pageIndex,
                        _context : context,
                        _transaction : transaction);

                    //var query = context.MixRelatedAttributeData.Where(m=> m.Specificulture == culture
                    //    && m.Id == getVal.Data.DataId && m.ParentId == parentId && m.ParentType == (int) MixEnums.MixAttributeSetDataType.Post)
                    //    .Select(m => m.ParentId).Distinct().ToList();
                }
                return(result);
            }
            catch (Exception ex)
            {
                return(UnitOfWorkHelper <MixCmsContext> .HandleException <PaginationModel <TView> >(ex, isRoot, transaction));
            }
            finally
            {
                if (isRoot)
                {
                    //if current Context is Root
                    context.Database.CloseConnection(); transaction.Dispose(); context.Dispose();
                }
            }
        }
Пример #2
0
        public static async Task <RepositoryResponse <PaginationModel <TView> > > GetListPostByAddictionalField <TView>(
            string fieldName, object fieldValue, string culture, MixEnums.MixDataType dataType
            , MixEnums.CompareType filterType = MixEnums.CompareType.Eq
            , string orderByPropertyName      = null, Heart.Enums.MixHeartEnums.DisplayDirection direction = Heart.Enums.MixHeartEnums.DisplayDirection.Asc, int?pageSize = null, int?pageIndex = null
            , 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
            {
                var result = new RepositoryResponse <PaginationModel <TView> >()
                {
                    IsSucceed = true,
                    Data      = new PaginationModel <TView>()
                    {
                        PageIndex = pageIndex.HasValue ? pageIndex.Value : 0,
                        PageSize  = pageSize
                    }
                };
                // Get Value Predicate By Type
                Expression <Func <MixAttributeSetValue, bool> > valPredicate = m => m.AttributeSetName == MixConstants.AttributeSetName.ADDITIONAL_FIELD_POST &&
                                                                               m.AttributeFieldName == fieldName;

                var pre = GetValuePredicate(fieldValue.ToString(), filterType, dataType);
                if (pre != null)
                {
                    valPredicate = Mix.Heart.Helpers.ReflectionHelper.CombineExpression(valPredicate, pre, Heart.Enums.MixHeartEnums.ExpressionMethod.And);
                }

                var query        = context.MixAttributeSetValue.Where(valPredicate).Select(m => m.DataId).Distinct();
                var dataIds      = query.ToList();
                var relatedQuery = context.MixRelatedAttributeData.Where(
                    m => m.ParentType == MixEnums.MixAttributeSetDataType.Post.ToString() && m.Specificulture == culture &&
                    dataIds.Any(d => d == m.DataId));
                var postIds = relatedQuery.Select(m => int.Parse(m.ParentId)).Distinct().AsEnumerable().ToList();
                result = await DefaultRepository <MixCmsContext, MixPost, TView> .Instance.GetModelListByAsync(
                    m => m.Specificulture == culture && postIds.Any(p => p == m.Id)
                    , orderByPropertyName, direction
                    , pageSize ?? 100, pageIndex ?? 0
                    , null, null
                    , context, transaction);

                return(result);
            }
            catch (Exception ex)
            {
                return(UnitOfWorkHelper <MixCmsContext> .HandleException <PaginationModel <TView> >(ex, isRoot, transaction));
            }
            finally
            {
                if (isRoot)
                {
                    //if current Context is Root
                    context.Dispose();
                }
            }
        }
Пример #3
0
        public static async Task <RepositoryResponse <PaginationModel <ReadViewModel> > > GetModelListByCategoryAsync(
            int pageId, string specificulture
            , string orderByPropertyName, Heart.Enums.MixHeartEnums.DisplayDirection direction
            , int?pageSize           = 1, int?pageIndex = 0, int?skip = null, int?top = null
            , MixCmsContext _context = null, IDbContextTransaction _transaction = null)
        {
            MixCmsContext context     = _context ?? new MixCmsContext();
            var           transaction = _transaction ?? context.Database.BeginTransaction();

            try
            {
                var query = context.MixPagePost.Include(ac => ac.MixPost)
                            .Where(ac =>
                                   ac.PageId == pageId && ac.Specificulture == specificulture &&
                                   ac.Status == MixEnums.MixContentStatus.Published.ToString()).Select(ac => ac.MixPost);
                PaginationModel <ReadViewModel> result = await Repository.ParsePagingQueryAsync(
                    query, orderByPropertyName
                    , direction
                    , pageSize, pageIndex, skip, top
                    , context, transaction
                    );

                return(new RepositoryResponse <PaginationModel <ReadViewModel> >()
                {
                    IsSucceed = true,
                    Data = result
                });
            }
            catch (Exception ex) // TODO: Add more specific exeption types instead of Exception only
            {
                Repository.LogErrorMessage(ex);
                if (_transaction == null)
                {
                    //if current transaction is root transaction
                    transaction.Rollback();
                }

                return(new RepositoryResponse <PaginationModel <ReadViewModel> >()
                {
                    IsSucceed = false,
                    Data = null,
                    Exception = ex
                });
            }
            finally
            {
                if (_context == null)
                {
                    //if current Context is Root
                    context.Database.CloseConnection(); transaction.Dispose(); context.Dispose();
                }
            }
        }
Пример #4
0
 public static async Task <RepositoryResponse <PaginationModel <TView> > > GetAttributeDataByParent <TView>(
     string culture, string attributeSetName,
     string parentId, MixDatabaseParentType parentType,
     string orderBy         = "CreatedDateTime", Heart.Enums.MixHeartEnums.DisplayDirection direction = MixHeartEnums.DisplayDirection.Desc,
     int?pageSize           = null, int?pageIndex = 0,
     MixCmsContext _context = null, IDbContextTransaction _transaction = null)
     where TView : ViewModelBase <MixCmsContext, MixAttributeSetData, TView>
 {
     return(await ViewModels.MixAttributeSetDatas.Helper.GetAttributeDataByParent <TView>(
                culture, attributeSetName,
                parentId, parentType, orderBy, direction, pageSize, pageIndex, _context, _transaction));
 }
Пример #5
0
        public static async Task <RepositoryResponse <PaginationModel <Lib.ViewModels.MixAttributeSetDatas.ReadMvcViewModel> > > GetAttributeDataListBySet(
            HttpContext context
            , string attributeSetName
            , string culture = null
            , Heart.Enums.MixHeartEnums.DisplayDirection direction = MixHeartEnums.DisplayDirection.Desc
            , MixCmsContext _context = null, IDbContextTransaction _transaction = null
            )
        {
            var result = await ViewModels.MixAttributeSetDatas.Helper.FilterByKeywordAsync <ViewModels.MixAttributeSetDatas.ReadMvcViewModel>(
                context.Request, culture, attributeSetName);

            return(result);
        }
Пример #6
0
        public async static Task <RepositoryResponse <PaginationModel <TView> > > GetPostlistByAdditionalField <TView>(

            string fieldName, string value, string culture
            , string orderByPropertyName = null, Heart.Enums.MixHeartEnums.DisplayDirection direction = MixHeartEnums.DisplayDirection.Asc
            , int?pageSize           = null, int?pageIndex = 0
            , MixCmsContext _context = null, IDbContextTransaction _transaction = null)
            where TView : ViewModelBase <MixCmsContext, MixPost, TView>
        {
            int    maxPageSize = MixService.GetConfig <int>("MaxPageSize");
            string orderBy     = MixService.GetConfig <string>("OrderBy");

            pageSize  = (pageSize > 0 && pageSize < maxPageSize) ? pageSize : maxPageSize;
            pageIndex = (pageIndex >= 0) ? pageIndex : 0;

            return(await Mix.Cms.Lib.ViewModels.MixPosts.Helper.SearchPostByField <TView>(
                       fieldName, value,
                       culture, orderByPropertyName ?? orderBy, direction, pageSize, pageIndex - 1, _context, _transaction));
        }
Пример #7
0
        public static async Task <RepositoryResponse <PaginationModel <Lib.ViewModels.MixPagePosts.ReadViewModel> > > GetPostListByPageId(
            HttpContext context
            , int pageId
            , string keyword = null
            , string culture = null
            , string orderBy = "CreatedDateTime"
            , Heart.Enums.MixHeartEnums.DisplayDirection direction = MixHeartEnums.DisplayDirection.Desc
            , MixCmsContext _context = null, IDbContextTransaction _transaction = null
            )
        {
            int.TryParse(context.Request.Query["page"], out int page);
            int.TryParse(context.Request.Query["pageSize"], out int pageSize);
            page = (page > 0) ? page : 1;
            var result = await ViewModels.MixPosts.Helper.GetPostListByPageId <Lib.ViewModels.MixPagePosts.ReadViewModel>(
                pageId, keyword, culture,
                orderBy, direction, pageSize, page - 1, _context, _transaction);

            result.Data.Items.ForEach(m => m.LoadPost(_context, _transaction));
            return(result);
        }
Пример #8
0
        public static async Task <RepositoryResponse <PaginationModel <TView> > > GetPostListByPageId <TView>(
            int pageId
            , string keyword             = null
            , string culture             = null
            , string orderByPropertyName = "CreatedDateTime"
            , Heart.Enums.MixHeartEnums.DisplayDirection direction = Heart.Enums.MixHeartEnums.DisplayDirection.Desc
            , int?pageSize           = null, int?pageIndex = null
            , MixCmsContext _context = null, IDbContextTransaction _transaction = null)
            where TView : ViewModelBase <MixCmsContext, MixPagePost, TView>
        {
            UnitOfWorkHelper <MixCmsContext> .InitTransaction(_context, _transaction, out MixCmsContext context, out IDbContextTransaction transaction, out bool isRoot);

            try
            {
                culture = culture ?? MixService.GetConfig <string>("DefaultCulture");
                var result = await DefaultRepository <MixCmsContext, MixPagePost, TView> .Instance.GetModelListByAsync(
                    m => m.Specificulture == culture && m.PageId == pageId &&
                    (string.IsNullOrEmpty(keyword) ||
                     (EF.Functions.Like(m.MixPost.Title, $"%{keyword}%")) ||
                     (EF.Functions.Like(m.MixPost.Excerpt, $"%{keyword}%")) ||
                     (EF.Functions.Like(m.MixPost.Content, $"%{keyword}%"))
                    )
                    , orderByPropertyName, direction, pageSize, pageIndex
                    , _context : context, _transaction : transaction
                    );

                return(result);
            }
            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);
                }
            }
        }
Пример #9
0
        public async static Task <RepositoryResponse <PaginationModel <TView> > > GetPostlistByMeta <TView>(

            HttpContext context
            , string culture, string type = MixConstants.AttributeSetName.SYSTEM_TAG
            , string orderByPropertyName  = "CreatedDateTime", Heart.Enums.MixHeartEnums.DisplayDirection direction = MixHeartEnums.DisplayDirection.Desc
            , MixCmsContext _context      = null, IDbContextTransaction _transaction = null)
            where TView : ViewModelBase <MixCmsContext, MixPost, TView>
        {
            int    maxPageSize    = MixService.GetConfig <int>("MaxPageSize");
            string orderBy        = MixService.GetConfig <string>("OrderBy");
            int    orderDirection = MixService.GetConfig <int>("OrderDirection");

            int.TryParse(context.Request.Query["page"], out int page);
            int.TryParse(context.Request.Query["pageSize"], out int pageSize);
            pageSize = (pageSize > 0 && pageSize < maxPageSize) ? pageSize : maxPageSize;
            page     = (page > 0) ? page : 1;

            return(await Mix.Cms.Lib.ViewModels.MixPosts.Helper.GetModelistByMeta <TView>(
                       type, context.Request.Query["keyword"],
                       culture, orderByPropertyName, direction, pageSize, page - 1, _context, _transaction));
        }
Пример #10
0
        public static async Task <RepositoryResponse <PaginationModel <TView> > > GetAttributeDataByParent <TView>(
            string culture, string attributeSetName,
            string parentId, MixEnums.MixAttributeSetDataType parentType,
            string orderBy, Heart.Enums.MixHeartEnums.DisplayDirection direction,
            int?pageSize, int?pageIndex,
            MixCmsContext _context = null, IDbContextTransaction _transaction = null)
            where TView : ViewModelBase <MixCmsContext, MixAttributeSetData, TView>
        {
            UnitOfWorkHelper <MixCmsContext> .InitTransaction(_context, _transaction, out MixCmsContext context, out IDbContextTransaction transaction, out bool isRoot);

            try
            {
                var tasks = new List <Task <RepositoryResponse <TView> > >();

                Expression <Func <MixRelatedAttributeData, bool> > predicate = m => m.Specificulture == culture &&
                                                                               (m.AttributeSetName == attributeSetName) &&
                                                                               (m.Status == MixEnums.MixContentStatus.Published.ToString()) &&
                                                                               (string.IsNullOrEmpty(parentId) ||
                                                                                (m.ParentId == parentId && m.ParentType == parentType.ToString())
                                                                               );
                ;
                var query   = context.MixRelatedAttributeData.Where(predicate).Select(m => m.DataId).Distinct();
                var dataIds = query.ToList();
                Expression <Func <MixAttributeSetData, bool> > pre = m => dataIds.Any(id => m.Id == id);
                return(await DefaultRepository <MixCmsContext, MixAttributeSetData, TView> .Instance.GetModelListByAsync(
                           pre, orderBy, direction, pageSize, 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
                    context.Database.CloseConnection(); transaction.Dispose(); context.Dispose();
                }
            }
        }
Пример #11
0
        public static async Task <RepositoryResponse <PaginationModel <TView> > > SearchPostByField <TView>(
            string fieldName, string value
            , string culture             = null
            , string orderByPropertyName = "CreatedDateTime", Heart.Enums.MixHeartEnums.DisplayDirection direction = Heart.Enums.MixHeartEnums.DisplayDirection.Desc
            , int?pageSize           = null, int?pageIndex = 0
            , 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
            {
                culture ??= MixService.GetConfig <string>(MixAppSettingKeywords.DefaultCulture);
                var result = new RepositoryResponse <PaginationModel <TView> >()
                {
                    IsSucceed = true,
                    Data      = new PaginationModel <TView>()
                    {
                        PageIndex = pageIndex.HasValue ? pageIndex.Value : 0,
                        PageSize  = pageSize
                    }
                };
                var tasks = new List <Task <RepositoryResponse <TView> > >();
                // Get Value
                var dataIds = await context.MixDatabaseDataValue.Where(
                    m => m.MixDatabaseName == MixConstants.MixDatabaseName.ADDITIONAL_FIELD_POST && m.Specificulture == culture &&
                    EF.Functions.Like(m.StringValue, value) && m.MixDatabaseColumnName == fieldName)
                              .Select(m => m.DataId)?.ToListAsync();

                if (dataIds != null && dataIds.Count > 0)
                {
                    var getRelatedData = await MixDatabaseDataAssociations.ReadViewModel.Repository.GetModelListByAsync(
                        m => dataIds.Contains(m.DataId)
                        , orderByPropertyName, direction, pageSize, pageIndex
                        , _context : context, _transaction : transaction
                        );

                    if (getRelatedData.IsSucceed)
                    {
                        foreach (var item in getRelatedData.Data.Items)
                        {
                            if (int.TryParse(item.ParentId, out int postId))
                            {
                                var getData = await DefaultRepository <MixCmsContext, MixPost, TView> .Instance.GetSingleModelAsync(
                                    m => m.Specificulture == item.Specificulture && m.Id == postId
                                    , context, transaction);

                                if (getData.IsSucceed)
                                {
                                    result.Data.Items.Add(getData.Data);
                                }
                            }
                        }
                        result.Data.TotalItems = getRelatedData.Data.TotalItems;
                        result.Data.TotalPage  = getRelatedData.Data.TotalPage;
                    }
                }

                return(result);
            }
            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);
                }
            }
        }
Пример #12
0
        public static async Task <RepositoryResponse <PaginationModel <TView> > > SearchPostByIds <TView>(
            string keyword
            , List <string> dataIds
            , List <int> pageIds         = null
            , string culture             = null
            , string orderByPropertyName = "CreatedDateTime"
            , Heart.Enums.MixHeartEnums.DisplayDirection direction = Heart.Enums.MixHeartEnums.DisplayDirection.Desc
            , int?pageSize           = null, int?pageIndex = null
            , 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
            {
                culture = culture ?? MixService.GetConfig <string>("DefaultCulture");
                Expression <Func <MixPost, bool> > postPredicate = m => m.Specificulture == culture &&
                                                                   (string.IsNullOrEmpty(keyword) ||
                                                                    (EF.Functions.Like(m.Title, $"%{keyword}%")) ||
                                                                    (EF.Functions.Like(m.Excerpt, $"%{keyword}%")) ||
                                                                    (EF.Functions.Like(m.Content, $"%{keyword}%")));
                if (dataIds.Count > 0)
                {
                    var searchPostByDataIds = SearchPostByDataIdsPredicate(dataIds, culture, context);
                    postPredicate = searchPostByDataIds.AndAlso(postPredicate);
                }

                if (pageIds != null && pageIds.Count > 0)
                {
                    var searchPostByPageIds = SearchPostByPageIdsPredicate(pageIds, culture, context);
                    postPredicate = searchPostByPageIds.AndAlso(postPredicate);
                }

                if (!typeof(MixPost).GetProperties().Any(p => p.Name.ToLower() == orderByPropertyName.ToLower()))
                {
                    var postIds        = context.MixPost.Where(postPredicate).Select(p => p.Id);
                    var orderedPostIds = context.MixDatabaseDataAssociation.Where(
                        m => m.Specificulture == culture && postIds.Any(p => p.ToString() == m.ParentId))
                                         .Join(context.MixDatabaseDataValue, r => r.DataId, v => v.DataId, (r, v) => new { r, v })
                                         .OrderBy(rv => rv.v.StringValue)
                                         .Select(rv => rv.r.ParentId);
                    postPredicate = p => orderedPostIds.Distinct().Any(o => o == p.Id.ToString() && p.Specificulture == culture);
                }

                return(await DefaultRepository <MixCmsContext, MixPost, TView> .Instance.GetModelListByAsync(
                           postPredicate
                           , orderByPropertyName, direction
                           , pageSize, pageIndex
                           , _context : context, _transaction : 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);
                }
            }
        }
Пример #13
0
        public static async Task <RepositoryResponse <PaginationModel <TView> > > GetPostListByDataIds <TView>(
            List <string> dataIds
            , string culture             = null
            , string orderByPropertyName = "CreatedDateTime"
            , Heart.Enums.MixHeartEnums.DisplayDirection direction = Heart.Enums.MixHeartEnums.DisplayDirection.Desc
            , int?pageSize           = null, int?pageIndex = null
            , 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
            {
                culture = culture ?? MixService.GetConfig <string>("DefaultCulture");
                var result = new RepositoryResponse <PaginationModel <TView> >();
                Expression <Func <MixDatabaseDataAssociation, bool> > predicate = m => m.Specificulture == culture && dataIds.Contains(m.DataId) &&
                                                                                  m.ParentType == MixDatabaseParentType.Post;
                foreach (var id in dataIds)
                {
                    Expression <Func <MixDatabaseDataAssociation, bool> > pre = m => m.DataId == id;
                    predicate = predicate.AndAlso(pre);
                }
                var getRelatedData = await MixDatabaseDataAssociations.ReadViewModel.Repository.GetModelListByAsync(
                    predicate
                    , orderByPropertyName = "CreatedDateTime", direction, pageSize, pageIndex
                    , _context : context, _transaction : transaction
                    );

                if (getRelatedData.IsSucceed)
                {
                    foreach (var item in getRelatedData.Data.Items)
                    {
                        if (int.TryParse(item.ParentId, out int postId))
                        {
                            var getData = await DefaultRepository <MixCmsContext, MixPost, TView> .Instance.GetSingleModelAsync(
                                m => m.Specificulture == item.Specificulture && m.Id == postId
                                , context, transaction);

                            if (getData.IsSucceed)
                            {
                                result = new RepositoryResponse <PaginationModel <TView> >()
                                {
                                    IsSucceed = true,
                                    Data      = new PaginationModel <TView>()
                                    {
                                        Items     = new List <TView>(),
                                        PageIndex = pageIndex ?? 0,
                                        PageSize  = pageSize
                                    }
                                };
                                result.Data.Items.Add(getData.Data);
                            }
                        }
                    }
                    result.Data.TotalItems = getRelatedData.Data.TotalItems;
                    result.Data.TotalPage  = getRelatedData.Data.TotalPage;
                }
                return(result);
            }
            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);
                }
            }
        }
Пример #14
0
        public static async Task <RepositoryResponse <PaginationModel <TView> > > GetPostListByValueIds <TView>(
            List <string> valueIds
            , string culture             = null
            , string orderByPropertyName = "CreatedDateTime"
            , Heart.Enums.MixHeartEnums.DisplayDirection direction = Heart.Enums.MixHeartEnums.DisplayDirection.Desc
            , int?pageSize           = null, int?pageIndex = null
            , 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
            {
                culture = culture ?? MixService.GetConfig <string>("DefaultCulture");
                var result = new RepositoryResponse <PaginationModel <TView> >()
                {
                    IsSucceed = true,
                    Data      = new PaginationModel <TView>()
                    {
                        PageIndex = pageIndex.HasValue ? pageIndex.Value : 0,
                        PageSize  = pageSize
                    }
                };
                // Get Data
                Expression <Func <MixDatabaseDataValue, bool> > predicate = m => m.Specificulture == culture &&
                                                                            m.Status == MixContentStatus.Published;
                foreach (var item in valueIds)
                {
                    Expression <Func <MixDatabaseDataValue, bool> > pre = m => m.Id == item;
                    predicate = predicate.AndAlso(pre);
                }
                var getVal = await MixDatabaseDataValues.ReadViewModel.Repository.GetModelListByAsync(predicate, context, transaction);

                if (getVal.IsSucceed)
                {
                    var dataIds = getVal.Data.Select(m => m.DataId).Distinct();
                    if (dataIds.Count() == 1)
                    {
                        result = await GetPostListByDataIds <TView>(
                            dataIds : dataIds.ToList(),
                            culture : culture,
                            orderByPropertyName : orderByPropertyName,
                            direction : direction,
                            pageSize : pageSize,
                            pageIndex : pageIndex,
                            _context : context,
                            _transaction : transaction);
                    }
                    //var query = context.MixRelatedAttributeData.Where(m=> m.Specificulture == culture
                    //    && m.Id == getVal.Data.DataId && m.ParentId == parentId && m.ParentType == (int) MixEnums.MixDatabaseDataType.Post)
                    //    .Select(m => m.ParentId).Distinct().ToList();
                }
                return(result);
            }
            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);
                }
            }
        }
Пример #15
0
        public static async Task <RepositoryResponse <PaginationModel <TView> > > SearchPost <TView>(
            string keyword
            , List <string> dataIds
            , string culture             = null
            , string orderByPropertyName = "CreatedDateTime"
            , Heart.Enums.MixHeartEnums.DisplayDirection direction = Heart.Enums.MixHeartEnums.DisplayDirection.Desc
            , int?pageSize           = null, int?pageIndex = null
            , 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
            {
                culture = culture ?? MixService.GetConfig <string>("DefaultCulture");
                var result = new RepositoryResponse <PaginationModel <TView> >()
                {
                    IsSucceed = true,
                    Data      = new PaginationModel <TView>()
                };
                List <int> postIds = null;
                Expression <Func <MixRelatedAttributeData, bool> > predicate =
                    m => m.Specificulture == culture && m.ParentType == MixEnums.MixAttributeSetDataType.Post.ToString();

                foreach (var id in dataIds)
                {
                    // Get list related post ids by data id
                    Expression <Func <MixRelatedAttributeData, bool> > dataPredicate =
                        m => m.Specificulture == culture && m.ParentType == MixEnums.MixAttributeSetDataType.Post.ToString() &&
                        m.DataId == id;
                    var ids = context.MixRelatedAttributeData.Where(dataPredicate).Select(m => int.Parse(m.ParentId)).ToList();

                    // if first id in list => return ids
                    if (postIds == null)
                    {
                        postIds = ids.Distinct().ToList();
                    }
                    else
                    {
                        // filter ids by new data id ( for 'AND' condition)
                        postIds = postIds.Where(m => ids.Contains(m)).Distinct().ToList();
                        // if there is no items => no need to filter more
                        if (postIds.Count == 0)
                        {
                            break;
                        }
                    }
                }

                // Load Posts
                Expression <Func <MixPost, bool> > postPredicate = m => m.Specificulture == culture &&
                                                                   (string.IsNullOrEmpty(keyword) ||
                                                                    (EF.Functions.Like(m.Title, $"%{keyword}%")) ||
                                                                    (EF.Functions.Like(m.Excerpt, $"%{keyword}%")) ||
                                                                    (EF.Functions.Like(m.Content, $"%{keyword}%")));

                if (postIds != null && postIds.Count > 0)
                {
                    postPredicate = m => m.Specificulture == culture &&
                                    (string.IsNullOrEmpty(keyword) ||
                                     (EF.Functions.Like(m.Title, $"%{keyword}%")) ||
                                     (EF.Functions.Like(m.Excerpt, $"%{keyword}%")) ||
                                     (EF.Functions.Like(m.Content, $"%{keyword}%"))) &&
                                    postIds.Any(n => n == m.Id);
                }

                var getPosts = await DefaultRepository <MixCmsContext, MixPost, TView> .Instance.GetModelListByAsync(
                    postPredicate
                    , orderByPropertyName, direction
                    , pageSize, pageIndex
                    , _context : context, _transaction : transaction);

                result = getPosts;
                return(result);
            }
            catch (Exception ex)
            {
                return(UnitOfWorkHelper <MixCmsContext> .HandleException <PaginationModel <TView> >(ex, isRoot, transaction));
            }
            finally
            {
                if (isRoot)
                {
                    //if current Context is Root
                    context.Database.CloseConnection(); transaction.Dispose(); context.Dispose();
                }
            }
        }
Пример #16
0
        public static async Task <RepositoryResponse <PaginationModel <TView> > > SearchPost <TView>(
            string keyword
            , List <string> dataIds
            , List <int> pageIds         = null
            , string culture             = null
            , string orderByPropertyName = "CreatedDateTime"
            , Heart.Enums.MixHeartEnums.DisplayDirection direction = Heart.Enums.MixHeartEnums.DisplayDirection.Desc
            , int?pageSize           = null, int?pageIndex = null
            , 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
            {
                culture = culture ?? MixService.GetConfig <string>("DefaultCulture");
                var result = new RepositoryResponse <PaginationModel <TView> >()
                {
                    IsSucceed = true,
                    Data      = new PaginationModel <TView>()
                };
                List <int> postIds = SearchPostByDataIds(dataIds, culture, context);
                if (pageIds != null && pageIds.Count > 0)
                {
                    postIds.AddRange(SearchPostByPageIds(pageIds, culture, context));
                    postIds = postIds.Distinct().ToList();
                }
                // Load Posts

                if (postIds != null && postIds.Count > 0)
                {
                    Expression <Func <MixPost, bool> > postPredicate = m => m.Specificulture == culture &&
                                                                       (string.IsNullOrEmpty(keyword) ||
                                                                        (EF.Functions.Like(m.Title, $"%{keyword}%")) ||
                                                                        (EF.Functions.Like(m.Excerpt, $"%{keyword}%")) ||
                                                                        (EF.Functions.Like(m.Content, $"%{keyword}%"))) &&
                                                                       postIds.Any(n => n == m.Id);

                    var getPosts = await DefaultRepository <MixCmsContext, MixPost, TView> .Instance.GetModelListByAsync(
                        postPredicate
                        , orderByPropertyName, direction
                        , pageSize, pageIndex
                        , _context : context, _transaction : transaction);

                    result = getPosts;
                }
                return(result);
            }
            catch (Exception ex)
            {
                return(UnitOfWorkHelper <MixCmsContext> .HandleException <PaginationModel <TView> >(ex, isRoot, transaction));
            }
            finally
            {
                if (isRoot)
                {
                    //if current Context is Root
                    context.Database.CloseConnection(); transaction.Dispose(); context.Dispose();
                }
            }
        }
Пример #17
0
        /// <summary>
        /// Gets the modelist by meta.
        /// </summary>
        /// <typeparam name="TView">The type of the view.</typeparam>
        /// <param name="culture">The culture.</param>
        /// <param name="metaName">Name of the meta. Ex: sys_tag / sys_category</param>
        /// <param name="metaValue">The meta value.</param>
        /// <param name="orderByPropertyName">Name of the order by property.</param>
        /// <param name="direction">The direction.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="pageIndex">Index of the page.</param>
        /// <param name="_context">The context.</param>
        /// <param name="_transaction">The transaction.</param>
        /// <returns></returns>
        public static async Task <RepositoryResponse <PaginationModel <TView> > > GetModelistByMeta <TView>(
            string metaName, string metaValue, string culture
            , string orderByPropertyName, Heart.Enums.MixHeartEnums.DisplayDirection direction, int?pageSize, int?pageIndex
            , 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
            {
                var result = new RepositoryResponse <PaginationModel <TView> >()
                {
                    IsSucceed = true,
                    Data      = new PaginationModel <TView>()
                    {
                        PageIndex = pageIndex.HasValue ? pageIndex.Value : 0,
                        PageSize  = pageSize
                    }
                };
                var tasks = new List <Task <RepositoryResponse <TView> > >();
                // Get Tag
                var getVal = await MixAttributeSetValues.ReadViewModel.Repository.GetSingleModelAsync(m => m.AttributeSetName == metaName && m.StringValue == metaValue
                                                                                                      , context, transaction);

                if (getVal.IsSucceed)
                {
                    var getRelatedData = await MixRelatedAttributeDatas.ReadViewModel.Repository.GetModelListByAsync(
                        m => m.Specificulture == culture && m.DataId == getVal.Data.DataId &&
                        m.ParentType == MixEnums.MixAttributeSetDataType.Post.ToString()
                        , orderByPropertyName, direction, pageIndex, pageSize
                        , _context : context, _transaction : transaction
                        );

                    if (getRelatedData.IsSucceed)
                    {
                        foreach (var item in getRelatedData.Data.Items)
                        {
                            if (int.TryParse(item.ParentId, out int postId))
                            {
                                var getData = await DefaultRepository <MixCmsContext, MixPost, TView> .Instance.GetSingleModelAsync(
                                    m => m.Specificulture == item.Specificulture && m.Id == postId
                                    , context, transaction);

                                if (getData.IsSucceed)
                                {
                                    result.Data.Items.Add(getData.Data);
                                }
                            }
                        }
                        result.Data.TotalItems = getRelatedData.Data.TotalItems;
                        result.Data.TotalPage  = getRelatedData.Data.TotalPage;
                    }
                    //var query = context.MixRelatedAttributeData.Where(m=> m.Specificulture == culture
                    //    && m.Id == getVal.Data.DataId && m.ParentId == parentId && m.ParentType == (int) MixEnums.MixAttributeSetDataType.Post)
                    //    .Select(m => m.ParentId).Distinct().ToList();
                }
                Expression <Func <MixAttributeSetValue, bool> > valPredicate = m => m.Specificulture == culture && m.AttributeSetName == metaName && m.StringValue == metaValue;

                return(result);
            }
            catch (Exception ex)
            {
                return(UnitOfWorkHelper <MixCmsContext> .HandleException <PaginationModel <TView> >(ex, isRoot, transaction));
            }
            finally
            {
                if (isRoot)
                {
                    //if current Context is Root
                    context.Database.CloseConnection(); transaction.Dispose(); context.Dispose();
                }
            }
        }