Skip to content

fixed user editing function to save changes to db #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions class_social/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,25 @@ def get_users(self):
raise UserControllerError('Error trying to load users from DB')

# edit user profile
def edit_user_profile(self, user, changes):
id_ = user.id
if get_user_by_id(id_) is None:
def edit_user_profile(self, user_id, changes):
users = db.load_users()
user_ = get_user_by_id(str(user_id))
updated = []
if user_ is None:
raise HTTPException(status_code=404)
try:
if isinstance(user, User) and type(changes) == dict:
for attribute, new_value in changes.items():
setattr(user, attribute, new_value)
return user
else:
else:
try:
for user in users:
if user.id == user_.id:
for attribute, new_value in changes.items():
setattr(user_, attribute, new_value)
updated.append(user_)
else:
updated.append(user)
db.save_users(updated)
return user_
except UserControllerError:
raise UserControllerError('Error wrong input type')
except UserControllerError:
raise UserControllerError('Error wrong input type')

def is_email_in_database(self, email):
users_list = db.load_users()
Expand Down Expand Up @@ -112,8 +118,8 @@ def get_user_by_id(id: str):

# edit user profile by providing dict with changes
@users_routes.patch('/users/{user_id}/profile')
def edit_user_profile(user: User, changes: dict) -> User:
user = user_controller.edit_user_profile(user, changes)
def edit_user_profile(user_id: str, changes: dict) -> User:
user = user_controller.edit_user_profile(user_id, changes)
return user

@users_routes.get('/users/{id}/is_active')
Expand Down
4 changes: 1 addition & 3 deletions tests/test_user_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,12 @@ def test_if_user_was_edited_it_still_should_be_the_same_object_and_changes_be_in
mocked_load_users.return_value = [expected_user]
controller = UserController()
original_user = Mock(return_value=valid_user)
result = controller.edit_user_profile(valid_user, dict(name='Franz', address='Somestreet 69'))
result = controller.edit_user_profile(valid_user.id, dict(name='Franz', address='Somestreet 69'))

# test if changed user is different from original user
assert result != original_user
# test if changes have been incorporated successfully
assert [result] == mocked_load_users.return_value
# test if it is still the same object
assert result is valid_user


def test_exception_error_404_must_be_raised_if_user_nonexistent():
Expand Down