fixed-coales
Some checks failed
Deploy on push / deploy (push) Failing after 10s

This commit is contained in:
2024-10-15 11:12:09 +03:00
parent 76aeddbde2
commit bf33cdc95c
6 changed files with 47 additions and 48 deletions

View File

@@ -192,7 +192,8 @@ def add_author_rating_columns(q, group_list):
(shout_reaction.kind == ReactionKind.DISLIKE.value, -1),
else_=0,
)
)
),
0,
).label("shouts_rating"),
)
.select_from(shout_reaction)
@@ -226,7 +227,8 @@ def add_author_rating_columns(q, group_list):
(reaction_2.kind == ReactionKind.DISLIKE.value, -1),
else_=0,
)
)
),
0,
).label("comments_rating"),
)
.select_from(reaction_2)

View File

@@ -52,9 +52,9 @@ def add_reaction_stat_columns(q):
),
).add_columns(
# Count unique comments
func.count(aliased_reaction.id)
.filter(aliased_reaction.kind == ReactionKind.COMMENT.value)
.label("comments_stat"),
func.coalesce(
func.count(aliased_reaction.id).filter(aliased_reaction.kind == ReactionKind.COMMENT.value), 0
).label("comments_stat"),
# Calculate rating as the difference between likes and dislikes
func.sum(
case(

View File

@@ -173,7 +173,7 @@ def get_topic_comments_stat(topic_id: int) -> int:
sub_comments = (
select(
Shout.id.label("shout_id"),
func.coalesce(func.count(Reaction.id)).label("comments_count"),
func.coalesce(func.count(Reaction.id), 0).label("comments_count"),
)
.join(ShoutTopic, ShoutTopic.shout == Shout.id)
.join(Topic, ShoutTopic.topic == Topic.id)
@@ -257,17 +257,10 @@ def get_author_followers_stat(author_id: int) -> int:
return result[0] if result else 0
def get_author_comments_stat(author_id: int) -> int:
"""
Получает количество комментариев, оставленных указанным автором.
:param author_id: Идентификатор автора.
:return: Количество комментариев, оставленных автором.
"""
# Подзапрос для получения количества комментариев, оставленных автором
sub_comments = (
select(Author.id, func.coalesce(func.count(Reaction.id)).label("comments_count"))
.select_from(Author) # явно указываем левый элемент join'а
def get_author_comments_stat(author_id):
q = (
select(func.coalesce(func.count(Reaction.id), 0).label("comments_count"))
.select_from(Author)
.outerjoin(
Reaction,
and_(
@@ -276,13 +269,13 @@ def get_author_comments_stat(author_id: int) -> int:
Reaction.deleted_at.is_(None),
),
)
.where(Author.id == author_id)
.group_by(Author.id)
.subquery()
)
q = select(sub_comments.c.comments_count).filter(sub_comments.c.id == author_id)
with local_session() as session:
result = session.execute(q).first()
return result[0] if result else 0
return result.comments_count if result else 0
def get_with_stat(q):