def handle(self, id: int, user_input: UserUpdateInputData) -> Optional[UserOutputData]: """ handle """ user = User( id=id, name=user_input.name, password=user_input.password, email=user_input.email, ) # Valueが存在する要素のみアップデート対象として抽出する data_to_be_updated = user.dict(exclude_none=True) # idしかない場合はバリデーションエラー if len(data_to_be_updated) == 1: raise ValueError( f"Specify at least one field to be updated: {', '.join(user_input.dict().keys())}" ) data = injector.user_repository().update_user(data_to_be_updated) return parse_obj_as(UserOutputData, data)
def test_user(self, input, expected): now = datetime.now() user = User(name=input["name"], password=input["password"], email=input["email"], created_at=now, updated_at=now) assert user.name == expected["name"] assert user.password != expected["password"] assert user.get_raw_password() == expected["password"] assert user.email == expected["email"] assert user.created_at == now assert user.updated_at == now
def __find_user_by_id(self, id: int) -> Optional[UserOutputData]: """ __find_user_by_id """ user = User(id=id) data = injector.user_repository().find_user_by_id(user.id) return parse_obj_as(UserOutputData, data)
def handle(self, id: int) -> Optional[UserOutputData]: """ handle """ user = User(id=id) data = injector.user_repository().delete_user(user.id) return parse_obj_as(UserOutputData, data)
def create_user(self, user: UserDomain) -> UserDomain: now = datetime.now() data = User( name=user.name, password=user.get_raw_password(), email=user.email, created_at=now, updated_at=now, ) try: db_session.add(data) db_session.commit() except IntegrityError as e: logger.info(e) raise DuplicateException response = User.query.filter(User.name == user.name).first() logger.info(f"user create success: {response}") return UserDomain.from_orm(response)
def handle(self, user_input: UserCreateInputputData) -> Optional[UserOutputData]: """ handle """ user = User( name=user_input.name, password=user_input.password, email=user_input.email, ) data = injector.user_repository().create_user(user.name, user.password, user.email) return parse_obj_as(UserOutputData, data)
def handle(self, input: UserInput) -> UserOutput: data = User(name=input.name, password=input.password1, email=input.email) return UserOutput(**self.user_repository.create_user(data).dict())
def find_user_by_name(self, user: UserDomain) -> UserDomain: response = User.query.filter(User.name == user.name).first() logger.info(f"get user data: {response}") return UserDomain.from_orm(response)
def handle(self, name: str) -> UserOutput: data = User(name=name) return UserOutput(**self.user_repository.find_user_by_name(data).dict())