This commit is contained in:
parent
3fde67a87d
commit
a973da5bb4
|
@ -1,7 +1,6 @@
|
||||||
from typing import List
|
from typing import List
|
||||||
from sqlalchemy.orm import aliased, selectinload, joinedload
|
from sqlalchemy.orm import aliased, selectinload, joinedload
|
||||||
from sqlalchemy.sql import union
|
from sqlalchemy.sql import union
|
||||||
from sqlalchemy.dialects.postgresql.types import sqltypes
|
|
||||||
from sqlalchemy.sql.expression import (
|
from sqlalchemy.sql.expression import (
|
||||||
and_,
|
and_,
|
||||||
asc,
|
asc,
|
||||||
|
@ -37,6 +36,41 @@ def query_shouts():
|
||||||
shout_author = aliased(ShoutAuthor)
|
shout_author = aliased(ShoutAuthor)
|
||||||
shout_topic = aliased(ShoutTopic)
|
shout_topic = aliased(ShoutTopic)
|
||||||
|
|
||||||
|
# Подзапросы для получения уникальных авторов и тем
|
||||||
|
authors_subquery = (
|
||||||
|
select(
|
||||||
|
shout_author.shout.label("shout_id"),
|
||||||
|
func.json_agg(
|
||||||
|
func.json_build_object(
|
||||||
|
"id", Author.id,
|
||||||
|
"name", Author.name,
|
||||||
|
"slug", Author.slug,
|
||||||
|
"pic", Author.pic
|
||||||
|
)
|
||||||
|
).label("authors")
|
||||||
|
)
|
||||||
|
.join(Author, Author.id == shout_author.author)
|
||||||
|
.group_by(shout_author.shout)
|
||||||
|
.alias("authors_subquery")
|
||||||
|
)
|
||||||
|
|
||||||
|
topics_subquery = (
|
||||||
|
select(
|
||||||
|
shout_topic.shout.label("shout_id"),
|
||||||
|
func.json_agg(
|
||||||
|
func.json_build_object(
|
||||||
|
"id", Topic.id,
|
||||||
|
"title", Topic.title,
|
||||||
|
"body", Topic.body,
|
||||||
|
"slug", Topic.slug
|
||||||
|
)
|
||||||
|
).label("topics")
|
||||||
|
)
|
||||||
|
.join(Topic, Topic.id == shout_topic.topic)
|
||||||
|
.group_by(shout_topic.shout)
|
||||||
|
.alias("topics_subquery")
|
||||||
|
)
|
||||||
|
|
||||||
# Основной запрос с подзапросами для получения статистики, авторов и тем
|
# Основной запрос с подзапросами для получения статистики, авторов и тем
|
||||||
q = (
|
q = (
|
||||||
select(
|
select(
|
||||||
|
@ -50,42 +84,14 @@ def query_shouts():
|
||||||
)
|
)
|
||||||
).label("rating_stat"),
|
).label("rating_stat"),
|
||||||
func.max(aliased_reaction.created_at).label("last_reacted_at"),
|
func.max(aliased_reaction.created_at).label("last_reacted_at"),
|
||||||
func.array_agg(
|
authors_subquery.c.authors,
|
||||||
func.distinct(
|
topics_subquery.c.topics,
|
||||||
func.json_build_object(
|
|
||||||
"id",
|
|
||||||
Author.id,
|
|
||||||
"name",
|
|
||||||
Author.name,
|
|
||||||
"slug",
|
|
||||||
Author.slug,
|
|
||||||
"pic",
|
|
||||||
Author.pic,
|
|
||||||
).cast(sqltypes.Text) # Преобразуем JSON в текст
|
|
||||||
)
|
|
||||||
).label("authors"),
|
|
||||||
func.array_agg(
|
|
||||||
func.distinct(
|
|
||||||
func.json_build_object(
|
|
||||||
"id",
|
|
||||||
Topic.id,
|
|
||||||
"title",
|
|
||||||
Topic.title,
|
|
||||||
"body",
|
|
||||||
Topic.body,
|
|
||||||
"slug",
|
|
||||||
Topic.slug,
|
|
||||||
).cast(sqltypes.Text) # Преобразуем JSON в текст
|
|
||||||
)
|
|
||||||
).label("topics"),
|
|
||||||
)
|
)
|
||||||
.outerjoin(aliased_reaction, aliased_reaction.shout == Shout.id)
|
.outerjoin(aliased_reaction, aliased_reaction.shout == Shout.id)
|
||||||
.outerjoin(shout_author, shout_author.shout == Shout.id)
|
.outerjoin(authors_subquery, authors_subquery.c.shout_id == Shout.id)
|
||||||
.outerjoin(Author, Author.id == shout_author.author)
|
.outerjoin(topics_subquery, topics_subquery.c.shout_id == Shout.id)
|
||||||
.outerjoin(shout_topic, shout_topic.shout == Shout.id)
|
|
||||||
.outerjoin(Topic, Topic.id == shout_topic.topic)
|
|
||||||
.where(and_(Shout.published_at.is_not(None), Shout.deleted_at.is_(None)))
|
.where(and_(Shout.published_at.is_not(None), Shout.deleted_at.is_(None)))
|
||||||
.group_by(Shout.id)
|
.group_by(Shout.id, authors_subquery.c.authors, topics_subquery.c.topics)
|
||||||
)
|
)
|
||||||
|
|
||||||
return q, aliased_reaction
|
return q, aliased_reaction
|
||||||
|
|
Loading…
Reference in New Issue
Block a user