Пример #1
0
    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)
Пример #2
0
    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
Пример #3
0
    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)
Пример #4
0
    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)
Пример #5
0
    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)
Пример #7
0
    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())
Пример #8
0
    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)
Пример #9
0
    def handle(self, name: str) -> UserOutput:
        data = User(name=name)

        return UserOutput(**self.user_repository.find_user_by_name(data).dict())