8000 upd query by nhathoang989 · Pull Request #529 · mixcore/mix.core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

upd query #529

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Mix.Cms.Lib/Models/Common/SearchPostQueryModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ public class SearchPostQueryModel: SearchQueryModel
public string Tag { get; set; }
public string PostType { get; set; }
public JObject Query { get; set; }
public int? PageId { get; set; }

public SearchPostQueryModel()
{

}
public SearchPostQueryModel(HttpRequest request, string culture): base(request, culture)
{
PageId = request.Query.TryGetValue("pageId", out var page)
? int.Parse(page) : null;
PostType = request.Query.TryGetValue("postType", out var postType)
? postType : string.Empty;
Category = request.Query.TryGetValue(MixRequestQueryKeywords.Category, out var category)
Expand Down
85 changes: 69 additions & 16 deletions src/Mix.Cms.Lib/ViewModels/MixPosts/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static async Task<RepositoryResponse<PaginationModel<TView>>> SearchPosts
Expression<Func<MixDatabaseDataValue, bool>> tagPredicate = GetTagPredicate(searchPostData);

valPredicate = valPredicate
.AndAlsoIf(catePredicate !=null, catePredicate)
.AndAlsoIf(catePredicate != null, catePredicate)
.AndAlsoIf(tagPredicate != null, tagPredicate);

if (valPredicate != null)
Expand All @@ -46,24 +46,16 @@ public static async Task<RepositoryResponse<PaginationModel<TView>>> SearchPosts
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);
return GetSortedPostByValue<TView>(predicate, searchPostData, context, transaction);

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
}
};
}

if (searchPostData.PageId.HasValue)
{
return GetSortedPostByPage<TView>(predicate, searchPostData, context, transaction);
}

return await DefaultRepository<MixCmsContext, MixPost, TView>.Instance.GetModelListByAsync(
Expand Down Expand Up @@ -91,6 +83,67 @@ public static async Task<RepositoryResponse<PaginationModel<TView>>> SearchPosts
}
}

private static RepositoryResponse<PaginationModel<TView>> GetSortedPostByPage<TView>(Expression<Func<MixPost, bool>> predicate, SearchPostQueryModel searchPostData, MixCmsContext context, IDbContextTransaction transaction) where TView : ViewModelBase<MixCmsContext, MixPost, TView>
{
var allPostIds = context.MixPost.Where(predicate)
.AsEnumerable()
.Select(m => m.Id);
var allPosts =
context.MixPagePost.Where(p =>
p.Specificulture == searchPostData.Specificulture
&& p.PageId == searchPostData.PageId
&& allPostIds.Contains(p.PostId))
.OrderBy(p => p.Priority);
var posts = allPosts.Skip(searchPostData.PagingData.PageIndex * searchPostData.PagingData.PageSize)
.Take(searchPostData.PagingData.PageSize)
.Include(m => m.MixPost)
.Select(m => m.MixPost)
.ToList();
var total = allPosts.Count();
var totalPage = total / searchPostData.PagingData.PageSize;
if (total % searchPostData.PagingData.PageSize > 0)
{
totalPage += 1;
}
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,
TotalItems = total,
TotalPage = totalPage
}
};
}

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
}
};
}

private static Expression<Func<MixDatabaseDataValue, bool>> GetTagPredicate(SearchPostQueryModel searchPostData)
{
Expression<Func<MixDatabaseDataValue, bool>> tagPredicate = null;
Expand Down
Loading
0