def update_user(id, body): """ Updates an active user matching a given id with given parameters such as name, email and password. When a parameter is empty it is not updated""" logging.debug("{users_controller} BEGIN function update_user()") if id is '': return RESP.response_400(message='The id parameter is empty!') try: user = CRUD.read_user_by_id(id) except Exception: return RESP.response_500(message='Database is down!') if user is None: return RESP.response_404(message='User not found!') try: if body['email'] is not '' and body['email'] != user.email: user_email = CRUD.read_user_by_email(body['email']) if user_email is not None: return RESP.response_409( message='The given email is already in use!') except Exception: return RESP.response_500(message='Database is down!') try: CRUD.update_user(user, body['name'], body['email'], UTILS.hash_password(body['password'])) CRUD.commit() except Exception: CRUD.rollback() return RESP.response_500(message='Database is down!') return RESP.response_200(message='User updated with success!')
def delete_user(id): """ Deletes an active user given an id""" logging.debug("{users_controller} BEGIN function delete_user()") if id is '': return RESP.response_400(message='The id parameter is empty!') try: user = CRUD.read_user_by_id(id) except Exception: return RESP.response_500(message='Database is down!') if user is None: return RESP.response_404(message='User not found!') try: CRUD.delete_user(user) CRUD.commit() except Exception: CRUD.rollback() return RESP.response_500(message='Database is down!') return RESP.response_200(message='User deleted with success')