From 8ff0e6786b996ce2d38ba80efc055ee92458728a Mon Sep 17 00:00:00 2001 From: Untone Date: Thu, 28 Mar 2024 23:33:56 +0300 Subject: [PATCH] author-stat-fix-6 --- resolvers/stat.py | 45 ++++++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/resolvers/stat.py b/resolvers/stat.py index 6b93518a..c35ff1c2 100644 --- a/resolvers/stat.py +++ b/resolvers/stat.py @@ -83,30 +83,28 @@ def add_author_stat_columns(q, with_rating=False): func.count(distinct(aliased_followers.follower)).label('followers_stat') ) - if not with_rating: - # Create a subquery for comments count - select_list = [ - Author.id, - func.coalesce(func.count(Reaction.id)).label('comments_count'), - ] + # Create a subquery for comments count + select_list = [ + Author.id, + func.coalesce(func.count(Reaction.id)).label('comments_count'), + ] - sub_comments = ( - select(*select_list) - .outerjoin( - Reaction, - and_( - Reaction.created_by == Author.id, - Reaction.kind == ReactionKind.COMMENT.value, - Reaction.deleted_at.is_(None), - ), - ) - .group_by(Author.id) - .subquery() + sub_comments = ( + select(*select_list) + .outerjoin( + Reaction, + and_( + Reaction.created_by == Author.id, + Reaction.kind == ReactionKind.COMMENT.value, + Reaction.deleted_at.is_(None), + ), ) + .group_by(Author.id) + .subquery() + ) - q = q.outerjoin(sub_comments, Author.id == sub_comments.c.id) - q = q.add_columns(sub_comments.c.comments_count) - q = q.group_by(Author.id, sub_comments.c.comments_count) + q = q.outerjoin(sub_comments, Author.id == sub_comments.c.id) + q = q.add_columns(sub_comments.c.comments_count) if with_rating: # Create a subquery for ratings counters @@ -146,6 +144,11 @@ def add_author_stat_columns(q, with_rating=False): sub_rating.c.shouts_likes, sub_rating.c.shouts_dislikes, ) + else: + q = q.group_by( + Author.id, + sub_comments.c.comments_count + ) return q