From 724e9bd5a038cf5d507b4a418bc6275a1b6ee1e8 Mon Sep 17 00:00:00 2001 From: Untone Date: Sat, 9 Dec 2023 19:22:47 +0300 Subject: [PATCH] add-main_topic --- resolvers/reader.py | 19 +++++++++++++++++++ schemas/core.graphql | 1 + 2 files changed, 20 insertions(+) diff --git a/resolvers/reader.py b/resolvers/reader.py index dcee4552..05980350 100644 --- a/resolvers/reader.py +++ b/resolvers/reader.py @@ -100,6 +100,11 @@ async def get_shout(_, _info, slug=None, shout_id=None): for author in shout.authors: if author.id == author_caption.author: author.caption = author_caption.caption + + shout.main_topic = session.query(ShoutTopics.topic_slug).filter( + ShoutTopics.shout_id == shout.id, + ShoutTopics.main == True + ).first() return shout except Exception: raise HTTPException(status_code=404, detail=f"shout {slug or shout_id} not found") @@ -125,6 +130,7 @@ async def load_shouts_by(_, _info, options): } :return: Shout[] """ + # base q = ( select(Shout) @@ -157,6 +163,11 @@ async def load_shouts_by(_, _info, options): shouts = [] with local_session() as session: for [shout, reacted_stat, commented_stat, rating_stat, _last_comment] in session.execute(q).unique(): + # Query the ShoutTopics table for the main topic + shout.main_topic = session.query(ShoutTopics.topic_slug).filter( + ShoutTopics.shout_id == shout.id, + ShoutTopics.main == True + ).first() shout.stat = { "viewed": await ViewedStorage.get_shout(shout.slug), "reacted": reacted_stat, @@ -190,6 +201,10 @@ async def load_shouts_drafts(_, info): q = q.filter(Shout.created_by == reader.id) q = q.group_by(Shout.id) for [shout] in session.execute(q).unique(): + shout.main_topic = session.query(ShoutTopics.topic_slug).filter( + ShoutTopics.shout_id == shout.id, + ShoutTopics.main == True + ).first() shouts.append(shout) return shouts @@ -242,6 +257,10 @@ async def load_shouts_feed(_, info, options): shouts = [] for [shout, reacted_stat, commented_stat, rating_stat, _last_comment] in session.execute(q).unique(): + shout.main_topic = session.query(ShoutTopics.topic_slug).filter( + ShoutTopics.shout_id == shout.id, + ShoutTopics.main == True + ).first() shout.stat = { "viewed": await ViewedStorage.get_shout(shout.slug), "reacted": reacted_stat, diff --git a/schemas/core.graphql b/schemas/core.graphql index 98d5775d..e3544b46 100644 --- a/schemas/core.graphql +++ b/schemas/core.graphql @@ -115,6 +115,7 @@ type Shout { lead: String description: String created_at: Int! + main_topic: String topics: [Topic] created_by: Author! updated_by: Author