Пример #1
0
    def validate_payload_for_creation_and_hydrate(self):
        values = []
        values.append(None)

        for api_field, data in User.expected_fields.items():
            value = JsonHelper.get_value_from_request(api_field, None)

            if value is None:
                if data['required'] is True:
                    raise MissingFieldException(api_field)
                values.append(data['default'])
            else:
                values.append(value)

        values.append(None)
        values.append(None)

        user = User(*values)  # pylint: disable=E1120

        check_user = self.user_repository.get_by_email(user.get_email())
        if check_user is not None:
            raise ResourceAlreadyExistsException('user', user.get_email(),
                                                 'email')

        check_user = self.user_repository.get_by_user_name(
            user.get_user_name())
        if check_user is not None:
            raise ResourceAlreadyExistsException('user', user.get_user_name(),
                                                 'username')

        return user
Пример #2
0
    def update(self, user_id):
        # Verification
        user = self.user_repository.get_by_id(user_id)

        if user is None:
            raise ResourceNotFoundException('user', user_id)

        # Hydratation
        for api_field, data in User.expected_fields.items():
            value = JsonHelper.get_value_from_request(api_field, None)

            if value is not None:
                method_to_call = getattr(user, 'set' + data['method'])
                method_to_call(value)

        password = JsonHelper.get_value_from_request('password', None)
        if password is not None:
            user.set_password(
                self.get_hashed_password(password, user.get_salt()))

        check_user = self.user_repository.get_by_email(user.get_email())
        if check_user is not None and check_user.get_id() != user_id:
            raise ResourceAlreadyExistsException('user', user.get_email(),
                                                 'email')

        check_user = self.user_repository.get_by_user_name(
            user.get_user_name())
        if check_user is not None and check_user.get_id() != user_id:
            raise ResourceAlreadyExistsException('user', user.get_user_name(),
                                                 'username')

        return user
Пример #3
0
    def get_for_update(self, version_id):
        # Verification
        version = self.repository.get_by_id(version_id)

        if version is None:
            raise ResourceNotFoundException('version', version_id)

        platform_id = JsonHelper.get_value_from_request('platformId', version.get_platform_id())
        game_id = JsonHelper.get_value_from_request('gameId', version.get_game_id())

        game = self.game_repository.get_by_id(game_id)
        platform = self.platform_repository.get_by_id(platform_id)

        if game is None:
            raise ResourceNotFoundException('game', game_id)

        if platform is None:
            raise ResourceNotFoundException('platform', platform_id)

        existing_version = self.repository.get_by_unique_index(platform_id, game_id)

        if existing_version is not None and existing_version.get_id() != version.get_id():
            raise ResourceAlreadyExistsException('platform-game couple', f"{game_id}-{platform_id}")

        # Hydratation
        super().hydrate_for_update(version)

        return version
Пример #4
0
    def get_for_create(self):
        game = super().validate_payload_for_creation_and_hydrate(Game)

        existing_version = self.repository.get_by_title(game.get_title())

        if existing_version is not None:
            raise ResourceAlreadyExistsException('game', game.get_title(),
                                                 'title')

        return game
Пример #5
0
    def get_for_create(self):
        platform = super().validate_payload_for_creation_and_hydrate(Platform)

        existing_version = self.repository.get_by_name(platform.get_name())

        if existing_version is not None:
            raise ResourceAlreadyExistsException('platform',
                                                 platform.get_name(), 'name')

        return platform
Пример #6
0
    def get_for_update(self, game_id):
        # Verification
        game = self.repository.get_by_id(game_id)

        if game is None:
            raise ResourceNotFoundException('game', game_id)

        title = JsonHelper.get_value_from_request('title', game.get_title())
        existing_version = self.repository.get_by_title(title)

        if existing_version is not None and existing_version.get_id(
        ) != game.get_id():
            raise ResourceAlreadyExistsException('game', game.get_title(),
                                                 'title')

        super().hydrate_for_update(game)

        return game
Пример #7
0
    def get_for_update(self, platform_id):
        # Verification
        platform = self.repository.get_by_id(platform_id)

        if platform is None:
            raise ResourceNotFoundException('platform', platform_id)

        name = JsonHelper.get_value_from_request('name', platform.get_name())
        existing_version = self.repository.get_by_name(name)

        if existing_version is not None and existing_version.get_id(
        ) != platform.get_id():
            raise ResourceAlreadyExistsException('platform',
                                                 platform.get_name(), 'name')

        super().hydrate_for_update(platform)

        return platform
Пример #8
0
    def get_for_create(self):
        version = super().validate_payload_for_creation_and_hydrate(Version)

        platform = self.platform_repository.get_by_id(version.get_platform_id())
        game = self.game_repository.get_by_id(version.get_game_id())

        if platform is None:
            raise ResourceNotFoundException('platform', version.get_platform_id())

        if game is None:
            raise ResourceNotFoundException('game', version.get_game_id())

        existing_version = self.repository.get_by_unique_index(
            version.get_platform_id(),
            version.get_game_id()
        )

        if existing_version is not None:
            raise ResourceAlreadyExistsException(
                'platform-game couple',
                str(version.get_platform_id()) + ':' + str(version.get_game_id())
            )

        return version