Exemplo n.º 1
0
        protected async Task <BioQuery <TEntity> > ConfigureQueryAsync(BioQuery <TEntity> query, int page,
                                                                       Func <BioQuery <TEntity>, Task <BioQuery <TEntity> > >?configureQuery = null)
        {
            if (ControllerContext.HttpContext.Request.Query.ContainsKey("order"))
            {
                query.OrderByString(ControllerContext.HttpContext.Request.Query["order"]);
            }
            else
            {
                ApplyDefaultOrder(query);
            }

            if (page > 0)
            {
                Page = page;
            }
            else if (ControllerContext.HttpContext.Request.Query.ContainsKey("page"))
            {
                Page = int.Parse(ControllerContext.HttpContext.Request.Query["page"]);
                if (Page < 1)
                {
                    Page = 1;
                }
            }

            query.ForSite(Site).Paginate(page, ItemsPerPage);
            if (configureQuery != null)
            {
                await configureQuery(query);
            }

            return(query);
        }
Exemplo n.º 2
0
        public static BioQuery <T> WithTags <T>(this BioQuery <T> query, Tag[] tags)
            where T : class, IEntity, IContentItem
        {
            Expression <Func <T, bool> >?ex = null;

            foreach (var tag in tags)
            {
                ex = ex == null ? post => post.TagIds.Contains(tag.Id) : ex.And(post => post.TagIds.Contains(tag.Id));
            }

            if (ex != null)
            {
                query = query.Where(ex);
            }

            return(query);
        }
        protected BioQuery <TEntity> ConfigureQuery(BioQuery <TEntity> query, int limit, int offset, string order,
                                                    string filter)
        {
            if (!string.IsNullOrEmpty(filter) &&
                filter != "null")
            {
                var mod4 = filter.Length % 4;
                if (mod4 > 0)
                {
                    filter += new string('=', 4 - mod4);
                }

                var data          = Convert.FromBase64String(filter);
                var decodedString = HttpUtility.UrlDecode(Encoding.UTF8.GetString(data));
                if (!string.IsNullOrEmpty(decodedString))
                {
                    query = query.WhereByString(decodedString);
                }
            }

            if (!string.IsNullOrEmpty(order))
            {
                query = query.OrderByString(order);
            }

            if (limit > 0)
            {
                query = query.Take(limit);
            }

            if (offset > 0)
            {
                query = query.Skip(offset);
            }

            return(query);
        }
Exemplo n.º 4
0
 protected override void ApplyDefaultOrder(BioQuery <Post <TUserPk> > query)
 {
     query.OrderByDescending(p => p.IsPublished ? p.DatePublished : p.DateUpdated);
 }
Exemplo n.º 5
0
        protected virtual async Task <(TEntity[] items, bool needCount)> DoGetAllAsync(BioQuery <TEntity> query)
        {
            var dbQuery   = query.BuildQuery();
            var needCount = false;

            if (query.Offset != null)
            {
                dbQuery   = dbQuery.Skip(query.Offset.Value);
                needCount = true;
            }

            if (query.Limit != null)
            {
                dbQuery   = dbQuery.Take(query.Limit.Value);
                needCount = true;
            }

            return(await AddIncludes(dbQuery).ToArrayAsync(), needCount);
        }
Exemplo n.º 6
0
 public static async Task <(T[] items, int itemsCount)> GetAllAsync <T>(this BioQuery <T> query) where T : class
Exemplo n.º 7
0
 public static BioQuery <T> ForSection <T>(this BioQuery <T> query, Section section)
     where T : class, IEntity, ISectionEntity
 {
     return(query.Where(e => e.SectionIds.Contains(section.Id)));
 }
Exemplo n.º 8
0
 public static BioQuery <T> ForSite <T>(this BioQuery <T> query, [NotNull] Site site)
     where T : class, IEntity, ISiteEntity
 {
     return(query.Where(e => e.SiteIds.Contains(site.Id)));
 }
Exemplo n.º 9
0
 protected virtual Task <BioQuery <TEntity> > ApplyPublishConditionsAsync(BioQuery <TEntity> query)
 {
     return(Task.FromResult(query.Where(e => e.IsPublished)));
 }
Exemplo n.º 10
0
 protected virtual Task <BioQuery <TEntity> > ApplyShowConditionsAsync(BioQuery <TEntity> query)
 {
     return(ApplyPublishConditionsAsync(query));
 }
Exemplo n.º 11
0
 protected virtual void ApplyDefaultOrder(BioQuery <TEntity> provider)
 {
     provider.OrderByDescending(e => e.DateAdded);
 }
Exemplo n.º 12
0
        protected override async Task <BioQuery <Post <string> > > ApplyPublishConditionsAsync(BioQuery <Post <string> > query)
        {
            var currentUser = _currentUserProvider.CurrentUser;

            if (currentUser != null)
            {
                if ((await _authorizationService.AuthorizeAsync(User, PostsPolicies.Posts)).Succeeded)
                {
                    return(query);
                }

                return(query.Where(e => e.IsPublished || e.AuthorId == currentUser.Id));
            }

            return(await base.ApplyPublishConditionsAsync(query));
        }