コード例 #1
0
        private void CadastroFoto(UserAccount loggedUser)
        {
            var cadastroFoto = _context.UserMerits.FirstOrDefault(x => x.TargetUserId == loggedUser.Id && x.Merit.Name.Equals(MeritsConst.CadastrarFoto));
            if (cadastroFoto == null)
            {
                var hash = HashEmailForGravatar(User.Identity.Name);
                using (var client = new HttpClient())
                {
                    try
                    {
                        var response = client.GetByteArrayAsync("https://secure.gravatar.com/avatar/" + hash + "?d=404").Result;
                        var merit = _context.Merits.First(x => x.Name.Equals(MeritsConst.CadastrarFoto));

                        _context.UserMerits.Add(new UserMerit
                        {
                            MeritId = merit.Id,
                            Value = merit.Value,
                            TargetUserId = loggedUser.Id
                        });
                        _context.SaveChanges();
                    }
                    catch (Exception ex)
                    {

                    }
                }
            }
        }
コード例 #2
0
        public ActionResult Index()
        {
            if (!_context.Users.Any(x => x.Mail.Equals(User.Identity.Name)))
            {
                var user = (ClaimsIdentity)User.Identity;
                var userAccount = new UserAccount() { Mail = user.Name, Name = user.Claims.First(x => x.Type == "name").Value.Replace("(Takenet)", "") };
                _context.Users.Add(userAccount);
                _context.SaveChanges();
            }

            var loggedUser = _context.Users.First(x => x.Mail == User.Identity.Name);

            PrimeiroAcesso(loggedUser);

            PrimeiroAcessoDia(loggedUser);

            CadastroFoto(loggedUser);

            var query = _context.UserMerits.Where(x => x.TargetUser.Mail == User.Identity.Name);
            ViewBag.Score = query.Sum(x => (decimal?)x.Value) ?? 0;
            ViewBag.MeritsCount = query.Count();

            ViewBag.Bronze = _context.UserMedals.Count(x => x.UserId == loggedUser.Id && x.Medal.Type == MedalType.Bronze);
            ViewBag.Silver = _context.UserMedals.Count(x => x.UserId == loggedUser.Id && x.Medal.Type == MedalType.Silver);
            ViewBag.Gold = _context.UserMedals.Count(x => x.UserId == loggedUser.Id && x.Medal.Type == MedalType.Gold);
            return View();
        }
コード例 #3
0
        private void PrimeiroAcesso(UserAccount loggedUser)
        {
            var primeiroAcesso = _context.UserMerits.FirstOrDefault(x => x.TargetUserId == loggedUser.Id && x.Merit.Name.Equals(MeritsConst.PrimeiroAcesso));
            if (primeiroAcesso == null)
            {
                var merit = _context.Merits.First(x => x.Name.Equals(MeritsConst.PrimeiroAcesso));

                _context.UserMerits.Add(new UserMerit
                {
                    MeritId = merit.Id,
                    Value = merit.Value,
                    TargetUserId = loggedUser.Id
                });
                _context.SaveChanges();
            }
        }
コード例 #4
0
        private void GiveMedal(UserAccount user)
        {
            var query = _context.UserMerits.Where(x => x.TargetUserId == user.Id);
            var targetUserScore = query.Sum(x => (decimal?)x.Value) ?? 0;

            var medals = _context.Medals.Where(x => x.Value <= targetUserScore).ToList();

            foreach (var medal in medals)
            {
                if (!_context.UserMedals.Any(x => x.UserId == user.Id && x.MedalId == medal.Id))
                {
                    var userMedal = new UserMedal
                    {
                        MedalId = medal.Id,
                        UserId = user.Id
                    };

                    _context.UserMedals.Add(userMedal);
                    _context.SaveChanges();
                }
            }
        }
コード例 #5
0
        private void PrimeiroAcessoDia(UserAccount loggedUser)
        {
            var primeiroAcessoDia = _context.UserMerits.FirstOrDefault(x => x.TargetUserId == loggedUser.Id
                            && x.Merit.Name.Equals(MeritsConst.PrimeiroAcessoDia)
                            && x.Created.Day == DateTime.Now.Day
                            && x.Created.Month == DateTime.Now.Month
                            && x.Created.Year == DateTime.Now.Year);

            if (primeiroAcessoDia == null)
            {
                var merit = _context.Merits.First(x => x.Name.Equals(MeritsConst.PrimeiroAcessoDia));

                _context.UserMerits.Add(new UserMerit
                {
                    MeritId = merit.Id,
                    Value = merit.Value,
                    TargetUserId = loggedUser.Id
                });
                _context.SaveChanges();
            }
        }