diff --git a/orm/community.py b/orm/community.py index fcc45168..911e0544 100644 --- a/orm/community.py +++ b/orm/community.py @@ -358,13 +358,13 @@ class CommunityStats: @property def shouts(self) -> int: - return self.community.session.query(func.count(Shout.id)).where(Shout.community == self.community.id).scalar() + return self.community.session.query(func.count(Shout.id)).filter(Shout.community == self.community.id).scalar() @property def followers(self) -> int: return ( self.community.session.query(func.count(CommunityFollower.follower)) - .where(CommunityFollower.community == self.community.id) + .filter(CommunityFollower.community == self.community.id) .scalar() ) @@ -374,7 +374,7 @@ class CommunityStats: return ( self.community.session.query(func.count(distinct(Author.id))) .join(Shout) - .where( + .filter( Shout.community == self.community.id, Shout.featured_at.is_not(None), Author.id.in_(Shout.authors), diff --git a/resolvers/reaction.py b/resolvers/reaction.py index 1fe27a51..7b7ce87f 100644 --- a/resolvers/reaction.py +++ b/resolvers/reaction.py @@ -63,7 +63,7 @@ def add_reaction_stat_columns(q: Select) -> Select: ).add_columns( # Count unique comments func.coalesce( - func.count(aliased_reaction.id).where(aliased_reaction.kind == ReactionKind.COMMENT.value), 0 + func.count(case((aliased_reaction.kind == ReactionKind.COMMENT.value, aliased_reaction.id), else_=None)), 0 ).label("comments_stat"), # Calculate rating as the difference between likes and dislikes func.sum( diff --git a/resolvers/reader.py b/resolvers/reader.py index 7f06a622..0495e388 100644 --- a/resolvers/reader.py +++ b/resolvers/reader.py @@ -157,23 +157,22 @@ def query_with_stat(info: GraphQLResolveInfo) -> Select: stats_subquery = ( select( Reaction.shout, - func.count(func.distinct(Reaction.id)) - .where(Reaction.kind == ReactionKind.COMMENT.value) - .label("comments_count"), + func.count(case((Reaction.kind == ReactionKind.COMMENT.value, Reaction.id), else_=None)).label( + "comments_count" + ), func.sum( case( (Reaction.kind == ReactionKind.LIKE.value, 1), (Reaction.kind == ReactionKind.DISLIKE.value, -1), else_=0, ) - ) - .where(Reaction.reply_to.is_(None)) - .label("rating"), - func.max(Reaction.created_at) - .where(Reaction.kind == ReactionKind.COMMENT.value) - .label("last_commented_at"), + ).label("rating"), + func.max(case((Reaction.kind == ReactionKind.COMMENT.value, Reaction.created_at), else_=None)).label( + "last_commented_at" + ), ) .where(Reaction.deleted_at.is_(None)) + .where(Reaction.reply_to.is_(None)) .group_by(Reaction.shout) .subquery() ) diff --git a/resolvers/stat.py b/resolvers/stat.py index ff874cd9..7796c3f1 100644 --- a/resolvers/stat.py +++ b/resolvers/stat.py @@ -147,7 +147,7 @@ def get_topic_followers_stat(topic_id: int) -> int: :return: Количество уникальных подписчиков темы. """ aliased_followers = aliased(TopicFollower) - q = select(func.count(distinct(aliased_followers.follower))).where(aliased_followers.topic == topic_id) + q = select(func.count(distinct(aliased_followers.follower))).filter(aliased_followers.topic == topic_id) with local_session() as session: result = session.execute(q).scalar() return int(result) if result else 0 @@ -240,7 +240,7 @@ def get_author_followers_stat(author_id: int) -> int: """ Получает количество подписчиков для указанного автора """ - q = select(func.count(AuthorFollower.follower)).where(AuthorFollower.author == author_id) + q = select(func.count(AuthorFollower.follower)).filter(AuthorFollower.author == author_id) with local_session() as session: result = session.execute(q).scalar() @@ -388,19 +388,19 @@ def get_followers_count(entity_type: str, entity_id: int) -> int: with local_session() as session: if entity_type == "topic": result = ( - session.query(func.count(TopicFollower.follower)).where(TopicFollower.topic == entity_id).scalar() + session.query(func.count(TopicFollower.follower)).filter(TopicFollower.topic == entity_id).scalar() ) elif entity_type == "author": # Count followers of this author result = ( session.query(func.count(AuthorFollower.follower)) - .where(AuthorFollower.author == entity_id) + .filter(AuthorFollower.author == entity_id) .scalar() ) elif entity_type == "community": result = ( session.query(func.count(CommunityFollower.follower)) - .where(CommunityFollower.community == entity_id) + .filter(CommunityFollower.community == entity_id) .scalar() ) else: @@ -419,12 +419,12 @@ def get_following_count(entity_type: str, entity_id: int) -> int: if entity_type == "author": # Count what this author follows topic_follows = ( - session.query(func.count(TopicFollower.topic)).where(TopicFollower.follower == entity_id).scalar() + session.query(func.count(TopicFollower.topic)).filter(TopicFollower.follower == entity_id).scalar() or 0 ) community_follows = ( session.query(func.count(CommunityFollower.community)) - .where(CommunityFollower.follower == entity_id) + .filter(CommunityFollower.follower == entity_id) .scalar() or 0 ) @@ -441,7 +441,7 @@ def get_shouts_count( """Получает количество публикаций""" try: with local_session() as session: - query = session.query(func.count(Shout.id)).where(Shout.published_at.isnot(None)) + query = session.query(func.count(Shout.id)).filter(Shout.published_at.isnot(None)) if author_id: query = query.where(Shout.created_by == author_id) @@ -466,12 +466,12 @@ def get_authors_count(community_id: Optional[int] = None) -> int: # Count authors in specific community result = ( session.query(func.count(distinct(CommunityFollower.follower))) - .where(CommunityFollower.community == community_id) + .filter(CommunityFollower.community == community_id) .scalar() ) else: # Count all authors - result = session.query(func.count(Author.id)).where(Author.deleted_at.is_(None)).scalar() + result = session.query(func.count(Author.id)).filter(Author.deleted_at.is_(None)).scalar() return int(result) if result else 0 except Exception as e: