public async Task <IViewComponentResult> InvokeAsync() { var latestRanking = await _cache.GetOrCreateAsync("~RankSidebar", async e => { e.AbsoluteExpirationRelativeToNow = _cacheInterval; var latestData = await _db.HistoryRankings.AsNoTracking().Where(g => g.RankType != HistoryRanking.Type.RankDaily).GroupBy(g => g.RankType).SelectMany(g => g.OrderByDescending(d => d.RankDate).Take(5)) .Select(h => new { HistoryRanking = h, Deleted = !_db.Blogs.Any(b => b.BlogID == h.BlogID) }) .ToListAsync(); var name2nick = _blogUtil.GetNickNames(latestData.Select(r => r.HistoryRanking.Author)); return(latestData.Where(a => !a.Deleted).Select(h => new RankingDisplay { Author = name2nick[h.HistoryRanking.Author], BlogDate = h.HistoryRanking.BlogDate, BlogID = h.HistoryRanking.BlogID, BlogThumb = h.HistoryRanking.BlogThumb, BlogTitle = h.HistoryRanking.BlogTitle, BlogVisit = h.HistoryRanking.BlogVisit, BlogUrl = Url.Action("Details", "Blog", new { id = h.HistoryRanking.BlogID }), RankDate = h.HistoryRanking.RankDate, Rating = h.HistoryRanking.Rating, RankType = h.HistoryRanking.RankType, PostCount = h.HistoryRanking.PostCount }).ToList()); }); var ids = latestRanking.Select(c => c.BlogID); _visitCounter.PrepareBlogVisits(ids); var ratingUtil = ViewContext.HttpContext.RequestServices.GetService(typeof(RatingUtil)) as RatingUtil; ratingUtil.PrepareRatings(ids); return(View(latestRanking)); }
public EditTagModel(BlogUtil util, BlogContext db, int id, string author, IEnumerable <int> BlackListTags) { BlogID = id; Author = author; TagsInBlog = db.TagsInBlogs.Include("tag").Where(tib => tib.BlogID == id); TagHistories = db.TagHistories.Where(th => th.BlogID == id); NickNames = util.GetNickNames(TagsInBlog.Select(tib => tib.AddBy).Where(b => b != null)); BlackListTagIDs = BlackListTags; }
public IViewComponentResult Invoke(int itemid, string name = "", int pagenum = 1, ItemType idtype = ItemType.Blog, bool hottest = false) { ViewData["idtype"] = idtype; ViewData["itemid"] = itemid; ViewData["hottest"] = hottest; var posts = _db.Posts.Include("replies").Where(p => p.ItemId == itemid && p.IdType == idtype); if (!string.IsNullOrEmpty(name)) { posts = posts.Where(p => p.Author == name); ViewBag.relplyusername = name; } X.PagedList.IPagedList <Post> model; if (idtype == ItemType.Blog) { var query = posts.GroupJoin(_db.BlogRatings, p => p.PostId, r => r.PostId, (p, r) => new { post = p, ratings = r }) .SelectMany(a => a.ratings.DefaultIfEmpty(), (p, r) => new { post = p.post, blograting = r }); if (hottest) { query = query.OrderByDescending(p => p.post.Rating).ThenByDescending(p => p.post.PostDate); } else { query = query.OrderByDescending(p => p.post.PostDate); } var paged = query.ToPagedList(pagenum, ReplyPageSize); model = new X.PagedList.StaticPagedList <Post>(paged.Select(a => a.post), paged.GetMetaData()); IDictionary <int, BlogRating> ratings = query.Where(q => q.blograting != null).ToDictionary(q => q.post.PostId, q => q.blograting); ViewBag.ratings = ratings; } else { IQueryable <Post> query; if (hottest) { query = posts.OrderByDescending(p => p.Rating).ThenByDescending(p => p.PostDate); } else { query = posts.OrderByDescending(p => p.PostDate); } model = posts.OrderByDescending(p => p.PostDate).ToPagedList(pagenum, ReplyPageSize); } ViewBag.nicknames = _blogUtil.GetNickNames(model.Select(p => p.Author).Concat(model.SelectMany(p => p.Replies).Select(r => r.Author))); return(View(model)); }
protected MonthRanking GetMonthRankingInner(int year, int month) { var thisMonth = new DateTime(year, month, 1); var monthEndDate = thisMonth.AddMonths(1).AddDays(-1); var model = new MonthRanking(); var currentMonthData = _db.HistoryRankings.Where(h => DbFunctions.DiffMonths(h.RankDate, thisMonth) == 0 && DbFunctions.DiffYears(h.RankDate, thisMonth) == 0) .Select(h => new { HistoryRanking = h, PostCount = _db.Posts.Count(p => p.ItemId == h.BlogID && p.IdType == ItemType.Blog), Deleted = !_db.Blogs.Any(b => b.BlogID == h.BlogID) }) .ToList(); var name2nick = _blogUtil.GetNickNames(currentMonthData.Select(r => r.HistoryRanking.Author)); var currentMonthRankingData = currentMonthData.Select(h => new RankingDisplay { Author = name2nick[h.HistoryRanking.Author], BlogDate = h.HistoryRanking.BlogDate, BlogID = h.HistoryRanking.BlogID, BlogThumb = h.HistoryRanking.BlogThumb, BlogTitle = h.HistoryRanking.BlogTitle, BlogVisit = h.HistoryRanking.BlogVisit, BlogUrl = Url.Action("Details", "Blog", new { id = h.HistoryRanking.BlogID }), RankDate = h.HistoryRanking.RankDate, Rating = h.HistoryRanking.Rating, PostCount = h.PostCount, RankType = h.HistoryRanking.RankType, Deleted = h.Deleted, }); var dailyRanking = currentMonthRankingData.Where(d => d.RankType == HistoryRanking.Type.RankDaily).ToList(); if (year == DateTime.Today.Year && month == DateTime.Today.Month) { var dayFromMonday = DateTime.Now.DayOfWeek - DayOfWeek.Monday; if (dayFromMonday < 0) dayFromMonday += 7; monthEndDate = DateTime.Today.AddDays(-dayFromMonday); // If don't have today's ranking, read from 24h if (!dailyRanking.Any(h => h.RankDate.Date == DateTime.Today)) { dailyRanking.AddRange(currentMonthRankingData.Where(d => d.RankType == HistoryRanking.Type.Rank24h)); } } model.MonthlyRankings = currentMonthRankingData.Where(d => d.RankType == HistoryRanking.Type.RankMonthly).ToList(); model.DailyRankings = dailyRanking; model.WeeklyRankings = currentMonthRankingData.Where(d => d.RankType == HistoryRanking.Type.RankWeekly && d.RankDate < monthEndDate).ToList(); return model; }