topShoutsByView and topShoutsByRating

This commit is contained in:
knst-kotov 2021-08-30 10:41:59 +03:00
parent 862c19ed15
commit def6a59163
4 changed files with 24 additions and 15 deletions

View File

@ -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"]

View File

@ -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)

View File

@ -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")

View File

@ -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