Esempio n. 1
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
Esempio n. 2
0
    def get_for_update(self, copy_id):
        copy = self.repository.get_by_id(copy_id)

        if copy is None:
            raise ResourceNotFoundException('copy', copy_id)

        super().hydrate_for_update(copy)

        version = self.version_repository.get_by_id(copy.get_version_id())

        if version is None:
            raise ResourceNotFoundException('version', copy.get_version_id())

        return copy
Esempio n. 3
0
    def get_for_update(self, story_id):
        story = self.repository.get_by_id(story_id)

        if story is None:
            raise ResourceNotFoundException('story', story_id)

        super().hydrate_for_update(story)

        version = self.version_repository.get_by_id(story.get_version_id())

        if version is None:
            raise ResourceNotFoundException('version', story.get_version_id())

        return story
Esempio n. 4
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
Esempio n. 5
0
    def get_by_id(self, entity_id):
        object = self.repository.get_by_id(entity_id)

        if object is None:
            raise ResourceNotFoundException(self.resource_type, entity_id)

        return object
Esempio n. 6
0
    def delete(self, transaction_id):
        transaction = self.repository.get_by_id(transaction_id)

        if transaction is None:
            raise ResourceNotFoundException('transaction', transaction_id)

        self.repository.delete(transaction_id)

        return True
Esempio n. 7
0
    def get_for_update(self, transaction_id):
        transaction = self.repository.get_by_id(transaction_id)

        if transaction is None:
            raise ResourceNotFoundException('transaction', transaction_id)

        super().hydrate_for_update(transaction)

        return transaction
Esempio n. 8
0
    def get_for_create(self):

        copy = super().validate_payload_for_creation_and_hydrate(Copy)
        version = self.version_repository.get_by_id(copy.get_version_id())

        if version is None:
            raise ResourceNotFoundException('version', copy.get_version_id())

        return copy
Esempio n. 9
0
    def delete(self, story_id):
        story = self.repository.get_by_id(story_id)

        if story is None:
            raise ResourceNotFoundException('story', story_id)

        self.repository.delete(story_id)

        return True
Esempio n. 10
0
    def get_version(self, transaction):
        version = self.version_repository.get_by_id(
            transaction.get_version_id())

        if version is None:
            raise ResourceNotFoundException('version',
                                            transaction.get_version_id())

        return version
Esempio n. 11
0
    def get_copy(self, transaction):
        if transaction.get_copy_id() is not None:
            copy = self.copy_repository.get_by_id(transaction.get_copy_id())

            if copy is None:
                raise ResourceNotFoundException('copy',
                                                transaction.get_copy_id())

            return copy

        return None
Esempio n. 12
0
    def delete(self, platform_id):
        platform = self.repository.get_by_id(platform_id)

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

        if platform.get_version_count() > 0:
            raise RessourceHasChildrenException('platform', 'version')

        self.repository.delete(platform_id)

        return True
Esempio n. 13
0
    def delete(self, copy_id):
        copy = self.repository.get_by_id(copy_id)

        if copy is None:
            raise ResourceNotFoundException('copy', copy_id)

        if copy.get_transaction_count() > 0:
            raise RessourceHasChildrenException('copy', 'transaction')

        self.repository.delete(copy_id)

        return True
Esempio n. 14
0
    def delete(self, game_id):
        """Delete a game"""
        game = self.repository.get_by_id(game_id)

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

        if game.get_version_count() > 0:
            raise RessourceHasChildrenException('game', 'version')

        self.repository.delete(game_id)

        return True
Esempio n. 15
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
Esempio n. 16
0
    def get_by_filter(self, filter, filter_value):
        if filter == 'id':
            user = self.user_repository.get_by_id(filter_value)
        elif filter == 'email':
            user = self.user_repository.get_by_email(filter_value)
        elif filter == 'username':
            user = self.user_repository.get_by_user_name(filter_value)
        else:
            raise UnsupportedFilterException(filter,
                                             ['id', 'email', 'username'])

        if user is None:
            raise ResourceNotFoundException('user', filter_value, filter)

        return user
Esempio n. 17
0
    def get_authenticated_user(self, username, raw_password):
        """Load a user and check if the credentials are correct"""
        user = self.user_repository.get_by_user_name(username)
        if user is None:
            raise ResourceNotFoundException('user', username, 'username')

        hashed_password = self.get_hashed_password(raw_password,
                                                   user.get_salt())
        if hashed_password != user.get_password():
            raise InvalidCredentialsException()

        if user.get_is_active() is False:
            raise InactiveUserException('username', username)

        return user
Esempio n. 18
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
Esempio n. 19
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
Esempio n. 20
0
    def delete(self, version_id):
        """Delete a version"""
        version = self.repository.get_by_id(version_id)

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

        if version.get_story_count() > 0:
            raise RessourceHasChildrenException('version', 'story')

        if version.get_copy_count() > 0:
            raise RessourceHasChildrenException('version', 'copy')

        # Note (TODO): use an alias in the query instead (as above)
        if self.transaction_repository.get_count_by_version(version_id) > 0:
            raise RessourceHasChildrenException('version', 'transaction')

        self.repository.delete(version_id)

        return True