コード例 #1
0
ファイル: BlogController.cs プロジェクト: bakaest/gmgard
        public async Task <IActionResult> List(int?id, bool?harmony, string sort = "", int limit = 10, int skip = 0)
        {
            limit = Math.Min(Math.Max(limit, 1), 100);
            int page  = skip <= 0 ? 1 : (skip / limit + 1);
            var query = db_.Blogs.AsNoTracking().Where(b => b.isApproved == true);

            if (IsHarmony || harmony.HasValue)
            {
                query = query.Where(b => b.isHarmony == (IsHarmony || (harmony ?? false)));
            }
            if (id.HasValue)
            {
                var flatCategories = categoryUtil_.GetCategoryWithSubcategories(id.Value);
                query = query.Where(b => flatCategories.Contains(b.CategoryID));
            }
            IQueryable <BlogDetailDisplay> detailQuery;

            if (User.Identity.IsAuthenticated)
            {
                detailQuery = query
                              .GroupJoin(db_.TagsInBlogs.DefaultIfEmpty(),
                                         b => b.BlogID,
                                         tib => tib.BlogID,
                                         (blog, tib) => new { blog, tag = tib.Select(t => t.tag) })
                              .GroupJoin(db_.Favorites.Where(f => f.Username == User.Identity.Name),
                                         b => b.blog.BlogID,
                                         f => f.BlogID,
                                         (b, f) => new BlogDetailDisplay
                {
                    blog       = b.blog,
                    tag        = b.tag,
                    Option     = b.blog.option,
                    IsFavorite = f.Count() > 0,
                });
            }
            else
            {
                detailQuery = query.GroupJoin(db_.TagsInBlogs.DefaultIfEmpty(),
                                              b => b.BlogID,
                                              tib => tib.BlogID,
                                              (b, tib) => new BlogDetailDisplay
                {
                    blog   = b,
                    tag    = tib.Select(t => t.tag),
                    Option = b.option
                });
            }

            detailQuery = Sorted(sort, detailQuery);
            var blogs = await detailQuery.ToPagedListAsync(page, limit);

            return(Json(ToPaged(blogs)));
        }
コード例 #2
0
        public ActionResult Random(int?id)
        {
            IQueryable <Blog> blogs = _db.Blogs;

            if (id.HasValue)
            {
                var catids = _catUtil.GetCategoryWithSubcategories(id.Value);
                blogs = blogs.Where(b => catids.Contains(b.CategoryID));
            }
            var blog = blogs.OrderBy(_ => Guid.NewGuid()).First();

            return(RedirectToAction("Details", new { id = blog.BlogID }));
        }
コード例 #3
0
        public ActionResult Index(int page = 1)
        {
            int    pagesize    = homepagesize;
            var    query       = _db.Blogs.Include("posts").Where(b => b.isApproved == true);
            string categoryIds = string.Empty;
            bool   hideHarmony = false;
            bool   cachable    = true;

            if (isHarmony)
            {
                query = query.Where(b => b.isHarmony);
            }
            else
            {
                var opt = _blogUtil.GetUserOption(User.Identity.Name, o => new { o.homepageCategories, o.homepageHideHarmony, o.homepageTagBlacklist });
                categoryIds = opt.homepageCategories;
                hideHarmony = opt.homepageHideHarmony;
                List <int> catIdList    = new List <int>();
                List <int> tagBlacklist = new List <int>();
                if (!string.IsNullOrEmpty(categoryIds))
                {
                    catIdList = JsonConvert.DeserializeObject <List <int> >(categoryIds);
                }
                if (!string.IsNullOrEmpty(opt.homepageTagBlacklist))
                {
                    tagBlacklist = JsonConvert.DeserializeObject <List <int> >(opt.homepageTagBlacklist);
                    cachable     = false;
                }
                if (catIdList.Count > 0 || tagBlacklist.Count > 0)
                {
                    catIdList = _catUtil.GetCategoryWithSubcategories(catIdList);
                    query     = BlogHelper.getFilteredQuery(_db, query, tagBlacklist, catIdList, featuredBlogId);
                }
                if (hideHarmony == true)
                {
                    query = query.Where(b => !b.isHarmony);
                }
            }

            if (featuredBlogId != null && featuredBlogId.Count > 0)
            {
                ViewBag.featuredBlog = featuredBlogId;
                query = query.OrderByDescending(b => featuredBlogId.Contains(b.BlogID)).ThenByDescending(b => b.BlogDate);
            }
            else
            {
                query = query.OrderByDescending(b => b.BlogDate);
            }

            int itemCount = -1, pageCount = -1, pageNumber = 1;
            ConcurrentDictionary <string, X.PagedList.IPagedList <BlogDisplay> > cache = null;

            if (cachable)
            {
                var firstPageKey = CacheService.GetHomePageListKey(1, isHarmony, hideHarmony, categoryIds);
                cache = _cache.Get <ConcurrentDictionary <string, X.PagedList.IPagedList <BlogDisplay> > >(CacheService.HomePageCacheKey)
                        ?? new ConcurrentDictionary <string, X.PagedList.IPagedList <BlogDisplay> >();
                if (cache.Count > 0 && cache.ContainsKey(firstPageKey))
                {
                    itemCount = cache[firstPageKey].TotalItemCount;
                    pageCount = cache[firstPageKey].PageCount;
                }
            }
            if (pageCount < 0)
            {
                itemCount = query.Count();
                pageCount = (int)Math.Ceiling(itemCount / (double)pagesize);
            }
            if (page > 0 && page <= pageCount)
            {
                pageNumber = page;
            }

            var cachekey = CacheService.GetHomePageListKey(pageNumber, isHarmony, hideHarmony, categoryIds);

            X.PagedList.IPagedList <BlogDisplay> model;
            if (cache == null || !cache.TryGetValue(cachekey, out model))
            {
                if (cache != null)
                {
                    _cache.Set(CacheService.HomePageCacheKey, cache, new MemoryCacheEntryOptions()
                    {
                        Priority = CacheItemPriority.NeverRemove
                    });
                }
                var harmonysettings = _appSettings.HarmonySettings;
                if (!isHarmony || harmonysettings.WhitelistTags == null)
                {
                    model = query.Select(b => new BlogDisplay {
                        blog = b,
                        tag  = _db.TagsInBlogs.Where(t => t.BlogID == b.BlogID).Select(t => t.tag)
                    }).ToPagedList(pageNumber, pagesize, itemCount);
                }
                else
                {
                    model = query.Select(b => new BlogDisplay {
                        blog = b,
                        tag  = _db.TagsInBlogs.Where(t => t.BlogID == b.BlogID &&
                                                     !harmonysettings.WhitelistTags.Contains(t.TagID)).Select(t => t.tag)
                    }).ToPagedList(pageNumber, pagesize, itemCount);
                }
                if (cache != null)
                {
                    cache.TryAdd(cachekey, model);
                }
            }

            if (Request.IsAjaxRequest())
            {
                return(PartialView(model));
            }
            return(View(model));
        }