Ejemplo n.º 1
0
        //Add a record in the database of the user who has been top ranked at the end of the month
        private void AwardUserJob(Object source)
        {
            //join all competitions where the user participates, but only the active competitions

            var query = (from User in _context.Users
                         join UserCompetitionTotalScore in _context.UserCompetitionTotalScores on User.Id
                         equals UserCompetitionTotalScore.UserId
                         join Competition in _context.Competitions on UserCompetitionTotalScore.CompetitionId
                         equals Competition.Id
                         where (Competition.Status == true && UserCompetitionTotalScore.Year == DateTime.Today.Year)
                         orderby Competition.Id, UserCompetitionTotalScore.Total descending
                         select new{
                User,
                Competition.Id,
                Competition,
                UserCompetitionTotalScore.Total
            }).Distinct();

            //award the users who ranked first in their respective competitions
            var allCompIds = query.Select(x => x.Id).Distinct();

            foreach (var i in allCompIds)
            {
                var   top   = query.Where(x => x.Id == i).First();
                var   comp  = _context.Competitions.Find(top.Id);
                var   user  = _context.Users.Find(top.User.Id);
                Award award = new Award {
                    Competition = comp,
                    User        = user,
                    Description = $"Best shooter in {GetMonthNameByMonthNumber(DateTime.Today.Month)}" + " " +
                                  $"{DateTime.Today.Year}"
                };
                _notificationMessage.MonthAwardNotification(award.Description);
                _context.Awards.Add(award);
            }

            _context.SaveChanges();

            //run the updateAwardTimer once again after 20 days
            updateAwardTimer.Change((int)TimeSpan.FromDays(20).TotalMilliseconds, 0);
        }