public static async Task <PagingResult <Friend> > PagingFriendsAsync(this IRepository <User> repository, PagingByIdModel model) { FilterDefinition <User> filter = Builders <User> .Filter.Where(e => e.Id == model.Id && !e.IsDelete); var searchFilter = !string.IsNullOrEmpty(model.SearchTextFormated) ? Builders <UnwindUserFriend> .Filter.Where(e => e.Friends.Name.ToLower().Contains(model.SearchTextFormated)) : Builders <UnwindUserFriend> .Filter.Empty; var queryable = repository.Collection.Aggregate() .Match(filter) .Unwind <User, UnwindUserFriend>(e => e.Friends) .Match(searchFilter); var total = (await queryable.Group <UnwindUserFriend, string, TotalResult>(e => null, e => new TotalResult() { Result = e.Count() }).FirstOrDefaultAsync())?.Result ?? 0; var result = new PagingResult <Friend> { Items = (await queryable.Skip(model.Skip).Limit(model.Take).ToListAsync())?.Select(e => e.Friends).ToList() ?? new List <Friend>(), CanLoadMore = total > model.Skip + model.Take, Total = total }; return(result); }
public static async Task <PagingResult <Game> > PagingAsync(this IRepository <Game> repository, PagingByIdModel model) { var userCollection = repository.Collection.Database.GetCollection <User>("User"); var user = await userCollection.Find(Builders <User> .Filter.Eq(e => e.Id, model.Id)).FirstOrDefaultAsync(); if (user == null) { return(null); } var keys = user.Logins?.Select(e => e.ProviderKey); var availableFilter = Builders <Game> .Filter.Where(e => e.IsRemoved != true); var statusFilter = Builders <Game> .Filter.Where(e => e.Status != GameStatus.Ended || e.Status != GameStatus.Rejected); var belongsToUserFilter = Builders <Game> .Filter.Where(e => e.CreatedBy == model.Id || e.OpponentId == model.Id); var socialInviteFilter = Builders <Game> .Filter.And(Builders <Game> .Filter.Where(e => e.OpponentExtInfo != null), Builders <Game> .Filter.In(e => e.OpponentExtInfo.Key, keys)); var queryable = repository.Collection.Find(Builders <Game> .Filter.And(availableFilter, statusFilter, Builders <Game> .Filter.Or(belongsToUserFilter, socialInviteFilter))); var pagingResult = new PagingResult <Game> { Total = await queryable.CountAsync(), Items = await queryable.Sort(Builders <Game> .Sort.Descending(e => e.CreatedAt)).Skip(model.Skip).Limit(model.Take).ToListAsync() ?? new List <Game>() }; pagingResult.CanLoadMore = pagingResult.Total > model.Skip + model.Take; return(pagingResult); }