From 1a563420d3832002a469cc18ce408112d214dfe6 Mon Sep 17 00:00:00 2001 From: Untone Date: Thu, 29 Feb 2024 15:39:55 +0300 Subject: [PATCH] reaction-sort-type --- resolvers/reaction.py | 21 ++++++++++++--------- schema/enum.graphql | 7 +++++++ schema/input.graphql | 2 +- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/resolvers/reaction.py b/resolvers/reaction.py index 0b8fdbbb..a82045db 100644 --- a/resolvers/reaction.py +++ b/resolvers/reaction.py @@ -348,18 +348,18 @@ async def load_reactions_by(_, info, by, limit=50, offset=0): :search - to search by reactions' body :comment - true if body.length > 0 :after - amount of time ago - :sort - a fieldname to sort desc by default, 'like' | 'dislike' | 'rating' + :sort - a fieldname to sort desc by default } :param limit: int amount of shouts :param offset: int offset in this order :return: Reaction[] """ - aliased_reaction = aliased(Reaction) + q = ( - select(aliased_reaction, Author, Shout) + select(Reaction, Author, Shout) .select_from(Reaction) - .join(Author, aliased_reaction.created_by == Author.id) - .join(Shout, aliased_reaction.shout == Shout.id) + .join(Author, Reaction.created_by == Author.id) + .join(Shout, Reaction.shout == Shout.id) ) # calculate counters @@ -374,10 +374,13 @@ async def load_reactions_by(_, info, by, limit=50, offset=0): q = q.group_by(Reaction.id, Author.id, Shout.id, aliased_reaction.id) # order by - order_stat = by.get('sort', '') # 'like' | 'dislike' | 'created_at' | '-created_at' - order_fn = asc if order_stat.startswith('-') else desc - order_field = Reaction.created_at if not order_stat else f'{order_stat}s_stat' - q = q.order_by(order_fn(order_field)) + order_stat = by.get('sort', '').lower() # 'like' | 'dislike' | 'newest' | 'oldest' + order_by_stmt = desc(Reaction.created_at) + if order_stat == 'oldest': + order_by_stmt = asc(Reaction.created_at) + elif order_stat.endswith('like'): + order_by_stmt = desc(f'{order_stat}s_stat') + q = q.order_by(order_by_stmt) # pagination q = q.limit(limit).offset(offset) diff --git a/schema/enum.graphql b/schema/enum.graphql index cbcd8e42..04f22a8c 100644 --- a/schema/enum.graphql +++ b/schema/enum.graphql @@ -6,6 +6,13 @@ enum ReactionStatus { DELETED } +enum ReactionSort { + newest + oldest + like + dislike +} + enum ReactionKind { # collabs diff --git a/schema/input.graphql b/schema/input.graphql index e25a134a..a49a049d 100644 --- a/schema/input.graphql +++ b/schema/input.graphql @@ -77,5 +77,5 @@ input ReactionBy { topic: String created_by: Int after: Int - sort: String + sort: ReactionSort }