public async Task <Api.Domain.User> GetUserById(int id) { using (MySqlConnection connection = new MySqlConnection(await _connectionInfo.GetConnectionStringAsync())) { await connection.OpenAsync().ConfigureAwait(false); MySqlCommand command = new MySqlCommand(UserDaoResources.SelectUserById, connection); command.Parameters.AddWithValue("id", id); command.Prepare(); Api.Domain.User user = null; using (DbDataReader reader = await command.ExecuteReaderAsync()) { while (await reader.ReadAsync()) { user = new Api.Domain.User( reader.GetInt32("id"), reader.GetString("firstname"), reader.GetString("lastname"), reader.GetString("email"), reader.IsDbNull("global_admin") ? RoleType.Standard : reader.GetBoolean("global_admin") ? RoleType.Admin : RoleType.Standard); } } connection.Close(); return(user); } }
public async Task <Api.Domain.User> CreateUser(UserForCreation user) { using (MySqlConnection connection = new MySqlConnection(await _connectionInfo.GetConnectionStringAsync())) { await connection.OpenAsync().ConfigureAwait(false); MySqlCommand command = new MySqlCommand(UserDaoResources.InsertUser, connection); command.Parameters.AddWithValue("firstname", user.FirstName); command.Parameters.AddWithValue("lastname", user.LastName); command.Parameters.AddWithValue("email", user.Email); command.Parameters.AddWithValue("global_admin", user.IsAdmin); command.Prepare(); ulong userId = (ulong)await command.ExecuteScalarAsync(); Api.Domain.User newUser = new Api.Domain.User( (int)userId, user.FirstName, user.LastName, user.Email, user.IsAdmin ? RoleType.Admin : RoleType.Standard); connection.Close(); return(newUser); } }
public async Task GetUserByIdReturnsUserWhenUserExists() { int userId = TestHelpers.CreateUser(ConnectionString, FirstName1, LastName1, Email1); Api.Domain.User user = await _userDao.GetUserById(userId); Assert.That(user.Id, Is.EqualTo(userId)); Assert.That(user.FirstName, Is.EqualTo(FirstName1)); Assert.That(user.LastName, Is.EqualTo(LastName1)); Assert.That(user.Email, Is.EqualTo(Email1)); }
public async Task CreateUserCorrectlyInsertsUser() { UserForCreation userForCreation = new UserForCreation { FirstName = FirstName1, LastName = LastName1, Email = Email1 }; Api.Domain.User user = await _userDao.CreateUser(userForCreation); List <Api.Domain.User> users = TestHelpers.GetAllUsers(ConnectionString); Assert.That(users.Count, Is.EqualTo(1)); Assert.That(user, Is.EqualTo(users[0])); }
public static List <Api.Domain.User> GetAllUsers(string connectionString) { List <Api.Domain.User> users = new List <Api.Domain.User>(); using (DbDataReader reader = MySqlHelper.ExecuteReader(connectionString, "SELECT * FROM user")) { while (reader.Read()) { Api.Domain.User user = new Api.Domain.User( reader.GetInt32("id"), reader.GetString("firstname"), reader.GetString("lastname"), reader.GetString("email"), reader.GetBoolean("global_admin") ? RoleType.Admin : RoleType.Standard ); users.Add(user); } } return(users); }
async Task GivenGroupInDatabase() { using (var dbContext = new WordkiDbContext(ConnectionStringProvider)) { var user = new Api.Domain.User { Id = 1, Name = "user", Password = Host.EncrypterMock.Object.GetSalt(string.Empty), CreationDate = Host.Time2ProviderMock.Object.GetTime(), LastLoginDate = Host.Time2ProviderMock.Object.GetTime() }; dbContext.Users.Add(user); var group = new Api.Domain.Group { Id = 1, GroupLanguage1 = 1, GroupLanguage2 = 2, Name = "group", GroupCreationDate = Host.Time2ProviderMock.Object.GetTime() }; dbContext.Groups.Add(group); var word = new Api.Domain.Card { Heads = new Side { Value = "word1" }, Tails = new Side { Value = "word2" }, IsVisible = true, WordCreationDate = new DateTime(2020, 01, 01), }; dbContext.Words.Add(word); await dbContext.SaveChangesAsync(); } }
public async Task GetUserByIdReturnsNullUserWhenUserDoesntExist() { Api.Domain.User user = await _userDao.GetUserById(1); Assert.That(user, Is.Null); }