reaction-api-upgrade

This commit is contained in:
Untone 2024-01-23 01:57:25 +03:00
parent 4cb70d951a
commit 438baeb1a2

View File

@ -19,8 +19,15 @@ def add_stat_columns(q):
aliased_reaction = aliased(Reaction) aliased_reaction = aliased(Reaction)
q = q.outerjoin(aliased_reaction).add_columns( q = q.outerjoin(aliased_reaction).add_columns(
func.sum(aliased_reaction.id).label("reacted_stat"), # func.sum(aliased_reaction.id).label("reacted_stat"),
func.sum(case((aliased_reaction.kind == ReactionKind.COMMENT.value, 1), else_=0)).label("commented_stat"), func.sum(
case(
(aliased_reaction.kind == ReactionKind.COMMENT.value, 1),
(aliased_reaction.kind == ReactionKind.PROOF.value, 1),
(aliased_reaction.kind == ReactionKind.DISPROOF.value, 1),
else_=0
)
).label("commented_stat"),
func.sum( func.sum(
case( case(
(aliased_reaction.kind == ReactionKind.AGREE.value, 1), (aliased_reaction.kind == ReactionKind.AGREE.value, 1),
@ -84,31 +91,33 @@ async def get_shout(_, _info, slug=None, shout_id=None):
q = q.filter(Shout.deleted_at.is_(None)).group_by(Shout.id) q = q.filter(Shout.deleted_at.is_(None)).group_by(Shout.id)
try: try:
[shout, reacted_stat, commented_stat, rating_stat, _last_comment] = session.execute(q).first() results = session.execute(q).first()
if results:
[shout, commented_stat, rating_stat, _last_comment] = results
shout.stat = { shout.stat = {
"viewed": await ViewedStorage.get_shout(shout.slug), "viewed": await ViewedStorage.get_shout(shout.slug),
"reacted": reacted_stat, # "reacted": reacted_stat,
"commented": commented_stat, "commented": commented_stat,
"rating": rating_stat, "rating": rating_stat,
} }
for author_caption in session.query(ShoutAuthor).join(Shout).where(Shout.slug == slug): for author_caption in session.query(ShoutAuthor).join(Shout).where(Shout.slug == slug):
for author in shout.authors: for author in shout.authors:
if author.id == author_caption.author: if author.id == author_caption.author:
author.caption = author_caption.caption author.caption = author_caption.caption
main_topic = ( main_topic = (
session.query(Topic.slug) session.query(Topic.slug)
.join( .join(
ShoutTopic, ShoutTopic,
and_(ShoutTopic.topic == Topic.id, ShoutTopic.shout == shout.id, ShoutTopic.main == True), and_(ShoutTopic.topic == Topic.id, ShoutTopic.shout == shout.id, ShoutTopic.main == True),
)
.first()
) )
.first()
)
if main_topic: if main_topic:
shout.main_topic = main_topic[0] shout.main_topic = main_topic[0]
return shout return shout
except Exception: except Exception:
raise HTTPException(status_code=404, detail=f"shout {slug or shout_id} not found") raise HTTPException(status_code=404, detail=f"shout {slug or shout_id} not found")