Esempio n. 1
0
        public string TryRateBlog(int id, int rating)
        {
            string credential = ExpUtil.GetIPAddress(Context);
            bool   rated      = false;

            if (RateWithAccount)
            {
                if (!Context.User.Identity.IsAuthenticated)
                {
                    return("login");
                }
                credential = Context.User.Identity.Name;
                rated      = _db.BlogRatings.Any(r => r.BlogID == id && r.credential == credential);
            }
            else
            {
                rated = _db.BlogRatings.Any(r => r.BlogID == id && r.credential == credential && DbFunctions.DiffDays(r.ratetime, DateTime.Today) < 1);
            }
            if (rated)
            {
                return("rated" + (RateWithAccount ? "" : "_today"));
            }
            var Rate = AddBlogRating(id, rating, credential);

            if (Rate == null)
            {
                return("error");
            }
            TriggerRateBlog(Rate);
            return("ok");
        }
Esempio n. 2
0
        public UsersRating GetUsersRating(int blogid)
        {
            var        credential = RateWithAccount ? Context.User.Identity.Name : ExpUtil.GetIPAddress(Context);
            BlogRating rating     = null;

            if (RateWithAccount)
            {
                rating = _db.BlogRatings.FirstOrDefault(r => r.BlogID == blogid && r.credential == credential);
            }
            else
            {
                rating = _db.BlogRatings.FirstOrDefault(r => r.BlogID == blogid && r.credential == credential && DbFunctions.DiffDays(r.ratetime, DateTime.Today) == 0);
            }
            var self = new UsersRating
            {
                BlogID          = blogid,
                credential      = credential,
                RateWithAccount = RateWithAccount
            };

            if (rating != null)
            {
                self.Rating  = rating;
                self.HasPost = rating.PostId.HasValue;
            }
            return(self);
        }
Esempio n. 3
0
 private void WeeklyRewardTask()
 {
     using (var scope = _scopeFactory.CreateScope())
     {
         var db = scope.ServiceProvider.GetRequiredService <BlogContext>();
         // today should be first day of new week
         var firstday = DateTime.Today.AddDays(-7);
         var rankings = GetRankingSinceDate(db, firstday, firstday.AddDays(6), HistoryRanking.Type.RankWeekly);
         db.HistoryRankings.RemoveRange(db.HistoryRankings.Where(h => h.RankType == HistoryRanking.Type.RankWeekly && firstday <= h.RankDate));
         db.HistoryRankings.AddRange(rankings);
         db.SaveChanges();
         ExpUtil expUtil = scope.ServiceProvider.GetService <ExpUtil>();
         expUtil.addRankExp(rankings, WeeklyReward, "周榜奖励");
     }
 }
Esempio n. 4
0
        public Dictionary <int, int> GetUsersRatingValues(IEnumerable <int> blogids)
        {
            var credential = RateWithAccount ? Context.User.Identity.Name : ExpUtil.GetIPAddress(Context);
            IQueryable <BlogRating> ratings = null;

            if (RateWithAccount)
            {
                ratings = _db.BlogRatings.Where(r => blogids.Contains(r.BlogID) && r.credential == credential);
            }
            else
            {
                ratings = _db.BlogRatings.Where(r => blogids.Contains(r.BlogID) && r.credential == credential && DbFunctions.DiffDays(r.ratetime, DateTime.Today) == 0);
            }
            return(ratings.ToDictionary(v => v.BlogID, v => v.value));
        }
Esempio n. 5
0
 public BlogUtil(
     BlogContext db,
     UsersContext udb,
     IMemoryCache cache,
     IHttpContextAccessor contextAccessor,
     IUrlHelper urlHelper,
     MessageUtil msgUtil,
     AdminUtil adminUtil,
     UploadUtil uploadUtil,
     ExpUtil expUtil,
     IVisitCounter visitCounter,
     INickNameProvider nicknameProvider) : base(db, udb, cache, visitCounter, nicknameProvider)
 {
     _msgUtil         = msgUtil;
     _adminUtil       = adminUtil;
     _contextAccessor = contextAccessor;
     _urlHelper       = urlHelper;
     _uploadUtil      = uploadUtil;
     _expUtil         = expUtil;
     _visitCounter    = visitCounter;
 }
Esempio n. 6
0
 private void MonthlyRewardTask()
 {
     using (var scope = _scopeFactory.CreateScope())
     {
         var db = scope.ServiceProvider.GetRequiredService <BlogContext>();
         // today should be first day of new month
         DateTime firstday = DateTime.Today.AddMonths(-1);
         DateTime lastDay  = new DateTime(firstday.Year, firstday.Month, DateTime.DaysInMonth(firstday.Year, firstday.Month));
         var      rankings = GetRankingSinceDate(db, firstday, lastDay, HistoryRanking.Type.RankMonthly);
         db.HistoryRankings.RemoveRange(db.HistoryRankings.Where(h => h.RankType == HistoryRanking.Type.RankMonthly && DbFunctions.DiffMonths(h.RankDate, firstday) == 0));
         db.HistoryRankings.AddRange(rankings);
         db.SaveChanges();
         // Remove any key at rankdate as we just updated.
         IDictionary <DateTime, MonthRanking> cache = _cache.Get <System.Collections.Concurrent.ConcurrentDictionary <DateTime, MonthRanking> >("~MonthRanking");
         if (cache != null && cache.ContainsKey(firstday))
         {
             cache.Remove(firstday);
         }
         ExpUtil expUtil = scope.ServiceProvider.GetService <ExpUtil>();
         expUtil.addRankExp(rankings, MonthlyReward, "月榜奖励");
     }
 }
Esempio n. 7
0
        private void DailyRewardTask()
        {
            using (var scope = _scopeFactory.CreateScope())
            {
                var db       = scope.ServiceProvider.GetRequiredService <BlogContext>();
                var rankings = db.Blogs.Where(b => DbFunctions.DiffMinutes(b.BlogDate, DateTime.Now) < 1440 && b.isApproved == true && !NoRankCategories.Contains(b.CategoryID))
                               .Join(db.BlogRatings.GroupBy(r => r.BlogID).Select(g => new { blogId = g.Key, rating = g.Sum(r => r.value) })
                                     , b => b.BlogID, r => r.blogId, (b, r) => new
                {
                    blog      = b,
                    Rating    = r.rating,
                    PostCount = db.Posts.Count(p => p.IdType == ItemType.Blog && p.ItemId == b.BlogID)
                })
                               .OrderByDescending(r => r.Rating)
                               .ThenByDescending(r => r.blog.BlogDate)
                               .Take(RankSize)
                               .ToArray()
                               .Select(rank => new HistoryRanking
                {
                    BlogID    = rank.blog.BlogID,
                    RankDate  = DateTime.Now.Date,
                    Rating    = rank.Rating,
                    Author    = rank.blog.Author,
                    BlogDate  = rank.blog.BlogDate,
                    BlogThumb = BlogHelper.firstImgPath(rank.blog, true),
                    BlogTitle = rank.blog.BlogTitle,
                    BlogVisit = rank.blog.BlogVisit,
                    PostCount = rank.PostCount,
                    RankType  = HistoryRanking.Type.RankDaily,
                });

                db.HistoryRankings.AddRange(rankings);
                db.SaveChanges();
                ExpUtil expUtil = scope.ServiceProvider.GetService <ExpUtil>();
                expUtil.addRankExp(rankings, DailyReward, "日榜奖励");
            }
        }
Esempio n. 8
0
 public void ApplyBonus(UserProfile u, ExpUtil expUtil)
 {
     expUtil.AddPoint(u, Points);
     expUtil.addExp(u, Exp);
 }
Esempio n. 9
0
 public GachaBonusService(UsersContext db, IHttpContextAccessor accessor, ExpUtil expUtil)
 {
     db_      = db;
     context_ = accessor.HttpContext;
     expUtil_ = expUtil;
 }