public static User UpdateUser(User user) { try { using (SqlConnection connection = new SqlConnection(DBFunctions.ConnectionString)) { if (user.UserAddresses != null && user.UserAddresses.Count > 0) { UserAddressDB.ClearAddresses(user.UserAddresses, user.Id.Value); } SqlCommand command = connection.CreateCommand(); command.CommandText = String.Format(@" UPDATE [user].[User] SET [Name] = @Name, [LastName] = @LastName, [Email] = @Email, [Address] = @Address, [ZipCode] = @ZipCode, [CityName] = @CityName, [CountryName] = @CountryName, [Phone] = @Phone, [UserTypeId] = @UserTypeId, [Active] = @Active WHERE [Id] = @Id "); FillData(command, user); connection.Open(); command.ExecuteNonQuery(); if (user.UserAddresses != null && user.UserAddresses.Count > 0) { foreach (UserAddress userAddress in user.UserAddresses) { UserAddressDB.UpdateUserAddress(userAddress, user.Id.Value); } } return(GetUser(user.Id.Value)); } } catch (Exception ex) { Logger.WriteLog(ex); throw ErrorResponse.ErrorMessage(HttpStatusCode.BadRequest, ex); } }
public static User GetUser(int userId) { try { User retVal = new User(); using (SqlConnection connection = new SqlConnection(DBFunctions.ConnectionString)) { SqlCommand command = connection.CreateCommand(); command.CommandText = String.Format(@" SELECT {0} FROM [user].[User] WHERE [Id] = @Id ", AllColumnSelect); command.AddParameter("@Id", SqlDbType.Int, userId); connection.Open(); using (SqlDataReader reader = command.ExecuteReader()) { if (reader.Read()) { retVal = ReadRow(reader); retVal.UserAddresses = UserAddressDB.GetUserAddresses(userId); } else { ErrorResponse.ErrorMessage(HttpStatusCode.NotFound); } } } return(retVal); } catch (Exception ex) { Logger.WriteLog(ex); throw ErrorResponse.ErrorMessage(HttpStatusCode.BadRequest, ex); } }
public static User CreateUser(User user) { try { using (SqlConnection connection = new SqlConnection(DBFunctions.ConnectionString)) { SqlCommand command = connection.CreateCommand(); command.CommandText = @" INSERT INTO [user].[User] ( [Name], [LastName], [Email], [Address], [ZipCode], [CityName], [CountryName], [Phone], [UserTypeId], [Password], [Active] ) VALUES ( @Name, @LastName, @Email, @Address, @ZipCode, @CityName, @CountryName, @Phone, @UserTypeId, @Password, @Active ) SET @Id = SCOPE_IDENTITY(); SELECT @Id as Id "; FillData(command, user); connection.Open(); int id = 0; using (SqlDataReader reader = command.ExecuteReader()) { if (reader.Read()) { id = ReadId(reader); } } if (user.UserAddresses != null && user.UserAddresses.Count > 0) { foreach (UserAddress userAddress in user.UserAddresses) { userAddress.UserId = id; UserAddressDB.CreateAddress(userAddress); } } return(GetUser(id)); } } catch (Exception ex) { Logger.WriteLog(ex); throw ErrorResponse.ErrorMessage(HttpStatusCode.BadRequest, ex); } }