public async Task<UserDto> AddAsync(User user)
        {
            //var user = Mapper.Map<UserDto, User>(userDto);
            await _userRepository.AddAsync(user);
            var result = await _userRepository.GetSingleAsync(user.Username, _userInclude);
            var mappedResult = Mapper.Map<User, UserDto>(result);
            await _userElasticsearch.UpdateAsync(mappedResult);
            if (mappedResult.Breweries.Any())
                await ReIndexUserRelationElasticSearch(mappedResult);
            return mappedResult;

        }
        private async Task UpdateUserSocialsAsync(DbConnection context, DbTransaction transaction, User user)
        {
            var userSocials = await context.QueryAsync<UserSocial>("SELECT * FROM UserSocials WHERE Username = @Username",
                new { user.Username }, transaction);

            await context.ExecuteAsync("DELETE FROM UserSocials WHERE Username = @Username and SocialId = @SocialId;",
                userSocials.Where(
                    u => user.Socials.All(s => u.SocialId != s.SocialId)),
                transaction);

            await context.ExecuteAsync(
                "UPDATE UserSocials set Site = @Site, Url = @Url WHERE Username = @Username and SocialId = @SocialId;",
                user.Socials, transaction);

            await context.ExecuteAsync("INSERT UserSocials(Username,Site,Url) VALUES(@Username,@Site,@Url);",
                user.Socials.Where(
                    s => userSocials.All(u => s.Username != u.Username && u.SocialId != s.SocialId)).Select(s => new { user.Username, s.Site, s.Url }),
                transaction);
        }
 public Task RemoveAsync(User user)
 {
     throw new NotImplementedException();
 }
 public async Task<int> UpdateAsync(User user)
 {
     using (var context = DapperHelper.GetOpenConnection())
     {
         using (var transaction = context.BeginTransaction())
         {
             try
             {
                var result = await context.ExecuteAsync(
                      "UPDATE Users set Username = @Username, Email = @Email, Settings = @Settings ,Gravatar = @Gravatar," +
                      " Latitude = @Latitude, Longitude = @Longitude, HeaderImage = @HeaderImage, Avatar = @Avatar " +
                      "WHERE Username = @Username;",
                      user, transaction);
                 await UpdateUserSocialsAsync(context, transaction, user);
                 transaction.Commit();
                 return result;
             }
             catch (Exception e)
             {
                 Log.Error(e.ToString());
                 transaction.Rollback();
                 throw;
             }
         }
     }
 }
        public async Task AddAsync(User user)
        {
            using (var context = DapperHelper.GetConnection())
            {
                context.Open();
                using (var transaction = context.BeginTransaction())
                {
                    try
                    {
                        await context.ExecuteAsync(
                            "INSERT Users(Username,Email,Settings,Gravatar,Latitude,Longitude,HeaderImage,Avatar) " +
                            "VALUES(@Username,@Email,@Settings,@Gravatar,@Latitude,@Longitude,@HeaderImage,@Avatar);",
                            user, transaction);
                        if (user.Socials != null)
                        {
                            await context.ExecuteAsync(
                                "INSERT UserSocials(Username,Site,Url) VALUES(@Username,@Site,@Url);",
                                user.Socials.Select(u => new { user.Username, u.Site, u.Url }), transaction);
                        }

                        var userSocials =
                            await context.QueryAsync<UserSocial>("SELECT * FROM UserSocials WHERE Username = @Username",
                               new { user.Username }, transaction);
                        user.Socials = userSocials.ToList();
                        transaction.Commit();
                    }
                    catch (Exception e)
                    {
                        Log.Error(e.ToString());
                        transaction.Rollback();
                        throw;
                    }
                }
            }
        }
 //TODO: Need to define the logic around delete.
 public void Remove(User user)
 {
     using (var context = DapperHelper.GetOpenConnection())
     {
         using (var transaction = context.BeginTransaction())
         {
             try
             {
                 context.Execute("DELETE FROM Users WHERE Username = @Username", new {user.Username},transaction);
                 transaction.Commit();
             }
             catch (Exception e)
             {
                 Log.Error(e.ToString());
                 transaction.Rollback();
                 throw;
             }
         }
     }
 }
 public void Remove_Gets_Removed()
 {
     var newUser = new User { Username = "******" + DateTime.Now.Ticks, Email = "*****@*****.**" };
     _userRepository.Add(newUser);
     var user = _userRepository.GetSingle(newUser.Username);
     _userRepository.Remove(user);
     var removed = _userRepository.GetSingle(newUser.Username);
     Assert.Null(removed);
 }
 public async Task UpdateAsync_Social_One_Gets_Removed()
 {
     var newUser = new User { Username = "******" + DateTime.Now.Ticks, Email = "*****@*****.**" };
     newUser.Socials = new List<UserSocial>
     {
         new UserSocial {Site="Homepage",Url = "http://test.com/" + newUser.Username},
         new UserSocial {Site="Twitter",Url = "@" + newUser.Username}
     };
     await _userRepository.AddAsync(newUser);
     var user = await _userRepository.GetSingleAsync(newUser.Username);
     user.Socials.Remove(user.Socials.FirstOrDefault());
     await _userRepository.UpdateAsync(user);
     var updated = await _userRepository.GetSingleAsync(user.Username);
     Assert.AreEqual(1, updated.Socials.Count);
 }
 public void Update_Social_All_Gets_Removed()
 {
     var newUser = new User { Username = "******" + DateTime.Now.Ticks, Email = "*****@*****.**" };
     newUser.Socials = new List<UserSocial>
     {
         new UserSocial {Site="Homepage",Url = "http://test.com/" + newUser.Username},
         new UserSocial {Site="Twitter",Url = "@" + newUser.Username}
     };
     _userRepository.Add(newUser);
     var user = _userRepository.GetSingle(newUser.Username);
     user.Socials = new List<UserSocial>();
     _userRepository.Update(user);
     var updated = _userRepository.GetSingle(user.Username);
     Assert.AreEqual(0, updated.Socials.Count);
 }
 public void Update_Social_Gets_Updated()
 {
     var newUser = new User { Username = "******" + DateTime.Now.Ticks, Email = "*****@*****.**" };
     newUser.Socials = new List<UserSocial>
     {
         new UserSocial {Site="Homepage",Url = "http://test.com/" + newUser.Username},
         new UserSocial {Site="Twitter",Url = "@" + newUser.Username}
     };
      _userRepository.Add(newUser);
     var user =  _userRepository.GetSingle(newUser.Username);
     var social = user.Socials.FirstOrDefault();
     social.Url = "@" + DateTime.Now.Ticks;
     _userRepository.Update(user);
     var updated = _userRepository.GetSingle(user.Username);
     Assert.AreEqual(updated.Socials.FirstOrDefault().Url,social.Url);
 }
 public async Task AddAsync_Socials_Gets_Added()
 {
     var newUser = new User { Username = "******" + DateTime.Now.Ticks, Email = "*****@*****.**" };
     newUser.Socials = new List<UserSocial>
     {
         new UserSocial {Site="Homepage",Url = "http://test.com/" + newUser.Username},
         new UserSocial {Site="Twitter",Url = "@" + newUser.Username}
     };
     await _userRepository.AddAsync(newUser);
     var user = await _userRepository.GetSingleAsync(newUser.Username);
     Assert.True(user.Socials.Any());
 }
 public async Task AddAsync_Gets_Added()
 {
     var newUser = new User { Username = "******" + DateTime.Now.Ticks, Email = "*****@*****.**" };
     await _userRepository.AddAsync(newUser);
     var users = await _userRepository.GetAllAsync();
     Assert.True(users.Any(u => u.Username == newUser.Username));
 }
 public void Add_Gets_Added()
 {
     var newUser = new User {Username = "******" + DateTime.Now.Ticks, Email = "*****@*****.**"};
     _userRepository.Add(newUser);
     var users = _userRepository.GetAll();
     Assert.True(users.Any(u => u.Username == newUser.Username));
 }
        //public static byte[] GenerateRandomBytes(int length)
        //{
        //    // Create a buffer
        //    byte[] randBytes;

        //    if (length >= 1)
        //    {
        //        randBytes = new byte[length];
        //    }
        //    else
        //    {
        //        randBytes = new byte[1];
        //    }

        //    // Create a new RNGCryptoServiceProvider.
        //    System.Security.Cryptography.RNGCryptoServiceProvider rand =
        //         new System.Security.Cryptography.RNGCryptoServiceProvider();

        //    // Fill the buffer with random bytes.
        //    rand.GetBytes(randBytes);

        //    // return the bytes.
        //    return randBytes;
        //}

        public static string JWTDecrypt(User user)
        {
            Log.Debug("Username:"******"http://www.w3.org/2001/04/xmldsig-more#hmac-sha256",
                                    "http://www.w3.org/2001/04/xmlenc#sha256");

            var jwtHeader = new JwtHeader(signingCred);


            JwtSecurityToken jwtToken = new JwtSecurityToken
            (
            //issuer: "http://issuer.com", audience: "http://localhost"
            //, claims: new List<Claim>() { new Claim("username", user.Username) }
            //, lifetime: new Lifetime(DateTime.UtcNow, DateTime.UtcNow.AddMinutes(expire))
            //, signingCredentials: signingCred
            );

            string tokenString = tokenhandler.WriteToken(jwtToken);
            return tokenString;
        }