Пример #1
0
        public async Task <ContentModel> GetContentAsync(ContentModel model)
        {
            IQueryable <Content> content = _db.Content.Include(p => p.Author)
                                           .Include(p => p.Media)
                                           .Include(p => p.Metadata)
                                           .Include(p => p.Categories).ThenInclude(c => c.Category)
                                           .AsNoTracking();

            // filter posts by type
            if (model.ContentType != null)
            {
                content = content.Where(c => c.ContentType == model.ContentType.Type);
            }
            else if (model.Type.IsSet())
            {
                content = content.Where(c => c.ContentType == model.Type);
            }

            // published?
            if (model.Status.HasValue)
            {
                content = content.Where(p => p.Status == model.Status.Value);
            }

            // Featured?
            if (model.Featured)
            {
                content = content.Where(p => p.Featured);
            }

            // posts by author
            if (!string.IsNullOrEmpty(model.AuthorName))
            {
                content = content.Where(n => n.Author.UserName == model.AuthorName);
            }

            if (model.Categories != null && model.Categories.Count > 0)
            {
                content = content.Where(c => c.Categories.Any(cc => model.Categories.Any(mc => cc.Category.Slug == mc)));
            }
            if (!string.IsNullOrEmpty(model.Category))
            {
                content = content.Where(c => c.Categories.Any(cc => cc.Category.Slug == model.Category));
            }

            // search the collection

            if (!string.IsNullOrEmpty(model.Search))
            {
                content = content.Where(c =>
                                        c.Title.Contains(model.Search) ||
                                        c.Body.Contains(model.Search) ||
                                        c.Excerpt.Contains(model.Search) ||
                                        c.Metadata.Any(m => m.BaseValue.Contains(model.Search))
                                        );
            }

            if (!string.IsNullOrEmpty(model.Filter))
            {
                // search for a specific meta value
                if (model.Filter.Contains("<"))
                {
                    content = ProcessFilterByOperator(content, model.Filter, '<');
                }
                else if (model.Filter.Contains(">"))
                {
                    content = ProcessFilterByOperator(content, model.Filter, '>');
                }
                else if (model.Filter.Contains("="))
                {
                    content = ProcessFilterByOperator(content, model.Filter, '=');
                }
            }

            // sort the collection and then content it.
            if (!string.IsNullOrEmpty(model.Order))
            {
                if (model.Order.StartsWith("Meta"))
                {
                    string sortVal = model.Order.Replace("Meta:", "").Replace(":Desc", "");
                    if (model.Order.EndsWith("Desc"))
                    {
                        content = content.Where(n => n.Metadata != null && n.Metadata.SingleOrDefault(m => m.Name == sortVal) != null);
                        content = content.OrderByDescending(n => n.Metadata.Single(m => m.Name == sortVal).BaseValue);
                    }
                    else
                    {
                        content = content.Where(n => n.Metadata != null && n.Metadata.SingleOrDefault(m => m.Name == sortVal) != null);
                        content = content.OrderBy(n => n.Metadata.Single(m => m.Name == sortVal).BaseValue);
                    }
                }
                else
                {
                    switch (model.Order)
                    {
                    case "Title":
                        content = content.OrderBy(n => n.Title);
                        break;

                    case "Date":
                        content = content.OrderBy(n => n.CreatedOn);
                        break;

                    case "Publish":
                        content = content.OrderBy(n => n.PublishDate);
                        break;

                    case "Views":
                        content = content.OrderBy(n => n.Views);
                        break;

                    case "TitleDesc":
                        content = content.OrderByDescending(n => n.Title);
                        break;

                    case "DateDesc":
                        content = content.OrderByDescending(n => n.CreatedOn);
                        break;

                    case "PublishDesc":
                        content = content.OrderByDescending(n => n.PublishDate);
                        break;

                    case "ViewsDesc":
                        content = content.OrderByDescending(n => n.Views);
                        break;

                    default:
                        content = content.OrderByDescending(n => n.PublishDate).ThenByDescending(n => n.CreatedOn);
                        break;
                    }
                }
            }
            else
            {
                content = content.OrderByDescending(n => n.PublishDate).ThenByDescending(n => n.CreatedOn);
            }
            await model.ReloadAsync(content);

            return(model);
        }