diff --git a/resolvers/stat.py b/resolvers/stat.py index b5e90567..f8670fed 100644 --- a/resolvers/stat.py +++ b/resolvers/stat.py @@ -58,8 +58,8 @@ def get_topic_shouts_stat(topic_id: int): ) ) ) - [shouts_stat] = local_session().execute(q) - return shouts_stat or 0 + result = local_session().execute(q).first() + return result[0] if result else 0 def get_topic_authors_stat(topic_id: int): @@ -76,8 +76,8 @@ def get_topic_authors_stat(topic_id: int): ) ) ) - [authors_stat] = local_session().execute(q) - return authors_stat or 0 + result = local_session().execute(q).first() + return result[0] if result else 0 def get_topic_followers_stat(topic_id: int): @@ -85,9 +85,8 @@ def get_topic_followers_stat(topic_id: int): q = select(func.count(distinct(aliased_followers.follower))).filter( aliased_followers.topic == topic_id ) - with local_session() as session: - [followers_stat] = session.execute(q) - return followers_stat or 0 + result = local_session().execute(q).first() + return result[0] if result else 0 def get_topic_comments_stat(topic_id: int): @@ -113,18 +112,17 @@ def get_topic_comments_stat(topic_id: int): ShoutTopic.topic == topic_id ) q = q.outerjoin(sub_comments, ShoutTopic.shout == sub_comments.c.shout_id) - [comments_stat] = local_session().execute(q) - return comments_stat or 0 + result = local_session().execute(q).first() + return result[0] if result else 0 def get_author_shouts_stat(author_id: int): aliased_shout_author = aliased(ShoutAuthor) q = select(func.count(distinct(aliased_shout_author.shout))).filter( aliased_shout_author.author == author_id ) - with local_session() as session: - [shouts_stat] = session.execute(q) - return shouts_stat or 0 + result = local_session().execute(q).first() + return result[0] if result else 0 def get_author_authors_stat(author_id: int): @@ -135,9 +133,8 @@ def get_author_authors_stat(author_id: int): aliased_authors.author != author_id, ) ) - with local_session() as session: - [authors_stat] = session.execute(q) - return authors_stat or 0 + result = local_session().execute(q).first() + return result[0] if result else 0 def get_author_followers_stat(author_id: int): @@ -145,9 +142,8 @@ def get_author_followers_stat(author_id: int): q = select(func.count(distinct(aliased_followers.follower))).filter( aliased_followers.author == author_id ) - with local_session() as session: - [followers_stat] = session.execute(q) - return followers_stat or 0 + result = local_session().execute(q).first() + return result[0] if result else 0 def get_author_comments_stat(author_id: int): @@ -168,9 +164,9 @@ def get_author_comments_stat(author_id: int): .subquery() ) q = select(sub_comments.c.comments_count).filter(sub_comments.c.id == author_id) - with local_session() as session: - [comments_stat] = session.execute(q) - return comments_stat or 0 + + result = local_session().execute(q).first() + return result[0] if result else 0 def get_with_stat(q):