Пример #1
0
 public void Create(User user)
 {
     ValidateUserInfo(user);
     using (var connection = new SqlConnection(_connectionString))
     {
         connection.Open();
         using (var command = new SqlCommand("[dbo].[createUser]", connection))
         {
             command.CommandType = CommandType.StoredProcedure;
             command.Parameters.AddWithValue("@Id", user.Id);
             command.Parameters.AddWithValue("@FirstName", user.FirstName);
             command.Parameters.AddWithValue("@LastName", user.LastName);
             command.Parameters.AddWithValue("@Email", user.Email);
             var returnValue = command.Parameters.Add("@Return", SqlDbType.Int);
             returnValue.Direction = ParameterDirection.ReturnValue;
             _logger.Trace("Trying to create user in database");
             using (new LogWrapper("Adding user in database"))
             {
                 command.ExecuteNonQuery();
             }
             if ((int)returnValue.Value == Sqlerror)
             {
                 _logger.Warn("Attempt to create user with existing e-mail");
                 throw new ArgumentException("User with this e-mail already exists");
             }
         }
     }
 }
Пример #2
0
 public static User GenerateTestUser(Guid id)
 {
     var user = new User
     {
         Id = id,
         FirstName = "FirstName" + ValidCharsForName[_count],
         LastName = "LastName" + ValidCharsForName[_count],
         Email = "test" + _count++ + "@mail.com"
     };
     return user;
 }
Пример #3
0
 public static void AddUser(User user)
 {
     using (var client = new HttpClient { BaseAddress = new Uri("http://localhost:25889/") })
     {
         client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
         var response = client.PostAsJsonAsync("api/users", user).Result;
         if (response.IsSuccessStatusCode)
         {
             MessageBox.Show("User Added");
         }
         else
         {
             MessageBox.Show("Error Code" +
                             response.StatusCode + " : Message - " + response.ReasonPhrase);
         }
     }
 }
 public void ShouldCreateUser()
 {
     //arrange
     var userRepository = new UsersRepository();
     var user = new User
     {
         Email = "*****@*****.**",
         FirstName = "name",
         LastName = "lastname",
         Id = Guid.NewGuid()
     };
     //act
     userRepository.Create(user);
     //asserts
     var resultUser = userRepository.Get(user.Id);
     Assert.AreEqual(user.Email, resultUser.Email);
 }
Пример #5
0
 public void Create(User user)
 {
     if (user == null)
         throw new ArgumentNullException(nameof(user));
     using (var connection = new SqlConnection(ConnectionString))
     {
         connection.Open();
         using (var command = connection.CreateCommand())
         {
             command.CommandText = "insert into [dbo].[User] (Id, FirstName, LastName, Email) values (@Id, @FirstName, @LastName, @Email)";
             command.Parameters.AddWithValue("@id", user.Id);
             command.Parameters.AddWithValue("@firstname", user.FirstName);
             command.Parameters.AddWithValue("@lastname", user.LastName);
             command.Parameters.AddWithValue("@email", user.Email);
             command.ExecuteNonQuery();
         }
     }
 }
Пример #6
0
 public void Test_UserInfoValidate()
 {
     //arrange
     var userRepository = new UsersRepository(_connectionString);
     var badUser = new User
     {
         Id = new Guid(),
         FirstName = "2",
         LastName = " ",
         Email = "login.host.com"
     };
     //act
     userRepository.ValidateUserInfo(badUser);
 }
Пример #7
0
 public UsersViewModel()
 {
     Users = UsersHttpClient.GetAllUsers();
     _selectedUser = new User();
 }
Пример #8
0
 //Проверяет, нужно ли вызывать UsersHttpClient.UpdateUser(User user)
 public void ProveThanUpdateUser()
 {
     //Проверка на заполненность SelectedUser
     if (SelectedUser.FirstName == null || SelectedUser.LastName == null || SelectedUser.Email == null)
         return;
     //Проверка на изменения
     if (UpdatedFirstName.Equals(SelectedUser.FirstName) &&
         UpdatedLastName.Equals(SelectedUser.LastName) &&
         UpdatedEmail.Equals(SelectedUser.Email))
     {
         MessageBox.Show("Nothing to update");
         return;
     }
     var updatedUser = new User()
     {
         FirstName = UpdatedFirstName,
         LastName = UpdatedLastName,
         Email = UpdatedEmail,
         Id = SelectedUser.Id
     };
     Users.Remove(SelectedUser);
     Users.Add(updatedUser); //Обновляем пользователя в коллекции
     UsersHttpClient.UpdateUser(updatedUser);
     OnPropertyChanged(nameof(Users));
 }
Пример #9
0
 public void AddUserInTableThanAddInRest()
 {
     var newUser = new User()
     {
         Id = Guid.NewGuid(),
         FirstName = NewUserFirstName,
         LastName = NewUserLastName,
         Email = NewUserEmail
     };
     Users.Add(newUser);
     UsersHttpClient.AddUser(newUser);
     OnPropertyChanged(nameof(Users));
 }
Пример #10
0
 public void ValidateUserInfo(User user)
 {
     if (user == null || user.Email == null || user.FirstName == null || user.LastName == null)
         throw new ArgumentNullException("Some parameters of user data are null.");
     if (!ValidateUserName(user.FirstName))
         throw new ArgumentException("FirstName is invalid.");
     if (!ValidateUserName(user.LastName))
         throw new ArgumentException("LastName is invalid.");
     if (!ValidateUserEmail(user.Email))
         throw new ArgumentException("Email is invalid.");
 }
Пример #11
0
 public IEnumerable<User> GetAllUsers()
 {
     using (var connection = new SqlConnection(_connectionString))
     {
         connection.Open();
         using (var command = new SqlCommand("[dbo].[getAllUsers]", connection))
         {
             using (new LogWrapper("Getting all users from database"))
             {
                 using (var reader = command.ExecuteReader())
                 {
                     while (reader.Read())
                     {
                         var user = new User
                         {
                             Id = reader.GetGuid(reader.GetOrdinal("Id")),
                             FirstName = reader.GetString(reader.GetOrdinal("FirstName")),
                             LastName = reader.GetString(reader.GetOrdinal("LastName")),
                             Email = reader.GetString(reader.GetOrdinal("Email"))
                         };
                         yield return user;
                     }
                 }
             }
         }
     }
 }