From def6a591639ff277cf12993f644cb8e6bf9b413b Mon Sep 17 00:00:00 2001 From: knst-kotov Date: Mon, 30 Aug 2021 10:41:59 +0300 Subject: [PATCH] topShoutsByView and topShoutsByRating --- orm/__init__.py | 2 +- orm/shout.py | 6 +++--- resolvers/zine.py | 26 +++++++++++++++++--------- schema.graphql | 5 +++-- 4 files changed, 24 insertions(+), 15 deletions(-) diff --git a/orm/__init__.py b/orm/__init__.py index aac4e1ac..6d2bda93 100644 --- a/orm/__init__.py +++ b/orm/__init__.py @@ -4,7 +4,7 @@ from orm.user import User from orm.message import Message from orm.topic import Topic from orm.notification import Notification -from orm.shout import Shout, ShoutAuthor, ShoutTopic +from orm.shout import Shout, ShoutAuthor, ShoutTopic, ShoutRating from orm.base import Base, engine __all__ = ["User", "Role", "Operation", "Permission", "Message", "Shout", "Topic", "Notification"] diff --git a/orm/shout.py b/orm/shout.py index 6810d2d6..6a3a4428 100644 --- a/orm/shout.py +++ b/orm/shout.py @@ -19,8 +19,8 @@ class ShoutTopic(Base): shout = Column(ForeignKey('shout.id'), primary_key = True) topic = Column(ForeignKey('topic.id'), primary_key = True) -class ShoutRatings(Base): - __tablename__ = "user_ratings" +class ShoutRating(Base): + __tablename__ = "shout_ratings" id = None rater_id = Column(ForeignKey('user.id'), primary_key = True) @@ -50,5 +50,5 @@ class Shout(Base): authors = relationship(lambda: User, secondary=ShoutAuthor.__tablename__) # NOTE: multiple authors topics = relationship(lambda: Topic, secondary=ShoutTopic.__tablename__) rating: int = Column(Integer, nullable=True, comment="Rating") - ratings = relationship(ShoutRatings, foreign_keys=ShoutRatings.shout_id) + ratings = relationship(ShoutRating, foreign_keys=ShoutRating.shout_id) old_id: str = Column(String, nullable = True) diff --git a/resolvers/zine.py b/resolvers/zine.py index fd01875a..4d845e80 100644 --- a/resolvers/zine.py +++ b/resolvers/zine.py @@ -1,4 +1,4 @@ -from orm import Shout, ShoutAuthor, ShoutTopic, User, Community, Resource +from orm import Shout, ShoutAuthor, ShoutTopic, ShoutRating, User, Community, Resource from orm.base import local_session from resolvers.base import mutation, query @@ -11,6 +11,7 @@ import asyncio from datetime import datetime from pathlib import Path +from sqlalchemy import select, func, desc class GitTask: @@ -66,16 +67,23 @@ class GitTask: print("git task worker error = %s" % (err)) -@query.field("topShouts") -async def top_shouts(_, info): - # TODO: implement top shouts - pass +@query.field("topShoutsByView") +async def top_shouts_by_view(_, info, limit): + with local_session() as session: + shouts = session.query(Shout).order_by(Shout.views.desc()).limit(limit).all() + return shouts -@query.field("topAuthors") -async def top_shouts(_, info): - # TODO: implement top authors - pass +@query.field("topShoutsByRating") +async def top_shouts(_, info, limit): + with local_session() as session: + stmt = select(Shout, func.sum(ShoutRating.value).label("shout_rating")).\ + join(ShoutRating).\ + group_by(Shout.id).\ + order_by(desc("shout_rating")).\ + limit(limit) + shouts = [row.Shout for row in session.execute(stmt)] + return shouts @mutation.field("createShout") diff --git a/schema.graphql b/schema.graphql index 6bfb7c25..7bd8c1a2 100644 --- a/schema.graphql +++ b/schema.graphql @@ -98,9 +98,10 @@ type Query { # shoutsByTags(tags: [String]): [Shout]! # shoutsByTime(time: DateTime): [Shout]! + topShoutsByView(limit: Int): [Shout]! + topShoutsByRating(limit: Int): [Shout]! + # getOnlineUsers: [User!]! - topAuthors: [User]! - topShouts: [Shout]! } ############################################ Subscription