Ejemplo n.º 1
0
        public Result Store(int userId, ResultRequest request)
        {
            var result = new Result()
            {
                UserId                  = (userId > 0) ? userId : null as int?,
                Wpm                     = request.wpm,
                TestId                  = request.test_id,
                GoodKeystrokes          = request.good_keystrokes,
                BadKeystrokes           = request.bad_keystrokes,
                TotalPossibleKeystrokes = request.total_possible_keystrokes,
                Errors                  = request.errors,
                Accuracy                = request.accuracy,
                CreatedAt               = DateTime.Now,
                UpdatedAt               = DateTime.Now
            };

            _context.Results.Add(result);

            _context.SaveChanges();

            //If a registered user did this, check for new achievements.
            if (userId > 0)
            {
                //todo: maybe abstract to achievement service
                var checker = new CheckForAchievements(_context, userId);
                checker.Check();
            }

            return(result);
        }
Ejemplo n.º 2
0
        /**
         * Create a new account.
         */
        public User Register(string email, string password, string username)
        {
            var gravatar = new GravatarHelper();

            var user = new User
            {
                Email     = email,
                Username  = username,
                Password  = password,
                EmailMd5  = gravatar.CalculateMD5Hash(email),
                CreatedAt = DateTime.Now,
                UpdatedAt = DateTime.Now,
            };

            _context.Users.Add(user);
            _context.SaveChanges();

            return(AppendToken(user));
        }
        public async Task <bool> SendResetEmail(SendResetEmailRequest request)
        {
            var user = _context.Users
                       .FirstOrDefault(u => u.Email == request.email);

            if (user == null)
            {
                return(false);
            }

            //Generate token
            var reset = new ForgotPassword();

            reset.Email     = user.Email;
            reset.Token     = Guid.NewGuid().ToString("N");
            reset.CreatedAt = DateTime.Now;
            reset.UpdatedAt = DateTime.Now;

            //Persist changes to db
            _context.ForgotPasswords.Add(reset);
            _context.SaveChanges();

            //Send email
            var to = new List <string> {
                user.Email
            };
            var client = new TransactionalEmailClient(_config);

            await client.SendEmail(
                to,
                "A request to reset your password was initiated",
                "If it was you who requested to reset your password, click the link below to reset your password. If you did not request this change, then you can ignore this email.",
                "/recover/" + reset.Token,
                "Click Here to Reset Your Password");

            return(true);
        }