topShoutsByView and topShoutsByRating
This commit is contained in:
parent
862c19ed15
commit
def6a59163
|
@ -4,7 +4,7 @@ from orm.user import User
|
||||||
from orm.message import Message
|
from orm.message import Message
|
||||||
from orm.topic import Topic
|
from orm.topic import Topic
|
||||||
from orm.notification import Notification
|
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
|
from orm.base import Base, engine
|
||||||
|
|
||||||
__all__ = ["User", "Role", "Operation", "Permission", "Message", "Shout", "Topic", "Notification"]
|
__all__ = ["User", "Role", "Operation", "Permission", "Message", "Shout", "Topic", "Notification"]
|
||||||
|
|
|
@ -19,8 +19,8 @@ class ShoutTopic(Base):
|
||||||
shout = Column(ForeignKey('shout.id'), primary_key = True)
|
shout = Column(ForeignKey('shout.id'), primary_key = True)
|
||||||
topic = Column(ForeignKey('topic.id'), primary_key = True)
|
topic = Column(ForeignKey('topic.id'), primary_key = True)
|
||||||
|
|
||||||
class ShoutRatings(Base):
|
class ShoutRating(Base):
|
||||||
__tablename__ = "user_ratings"
|
__tablename__ = "shout_ratings"
|
||||||
|
|
||||||
id = None
|
id = None
|
||||||
rater_id = Column(ForeignKey('user.id'), primary_key = True)
|
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
|
authors = relationship(lambda: User, secondary=ShoutAuthor.__tablename__) # NOTE: multiple authors
|
||||||
topics = relationship(lambda: Topic, secondary=ShoutTopic.__tablename__)
|
topics = relationship(lambda: Topic, secondary=ShoutTopic.__tablename__)
|
||||||
rating: int = Column(Integer, nullable=True, comment="Rating")
|
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)
|
old_id: str = Column(String, nullable = True)
|
||||||
|
|
|
@ -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 orm.base import local_session
|
||||||
|
|
||||||
from resolvers.base import mutation, query
|
from resolvers.base import mutation, query
|
||||||
|
@ -11,6 +11,7 @@ import asyncio
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from sqlalchemy import select, func, desc
|
||||||
|
|
||||||
class GitTask:
|
class GitTask:
|
||||||
|
|
||||||
|
@ -66,16 +67,23 @@ class GitTask:
|
||||||
print("git task worker error = %s" % (err))
|
print("git task worker error = %s" % (err))
|
||||||
|
|
||||||
|
|
||||||
@query.field("topShouts")
|
@query.field("topShoutsByView")
|
||||||
async def top_shouts(_, info):
|
async def top_shouts_by_view(_, info, limit):
|
||||||
# TODO: implement top shouts
|
with local_session() as session:
|
||||||
pass
|
shouts = session.query(Shout).order_by(Shout.views.desc()).limit(limit).all()
|
||||||
|
return shouts
|
||||||
|
|
||||||
|
|
||||||
@query.field("topAuthors")
|
@query.field("topShoutsByRating")
|
||||||
async def top_shouts(_, info):
|
async def top_shouts(_, info, limit):
|
||||||
# TODO: implement top authors
|
with local_session() as session:
|
||||||
pass
|
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")
|
@mutation.field("createShout")
|
||||||
|
|
|
@ -98,9 +98,10 @@ type Query {
|
||||||
# shoutsByTags(tags: [String]): [Shout]!
|
# shoutsByTags(tags: [String]): [Shout]!
|
||||||
# shoutsByTime(time: DateTime): [Shout]!
|
# shoutsByTime(time: DateTime): [Shout]!
|
||||||
|
|
||||||
|
topShoutsByView(limit: Int): [Shout]!
|
||||||
|
topShoutsByRating(limit: Int): [Shout]!
|
||||||
|
|
||||||
# getOnlineUsers: [User!]!
|
# getOnlineUsers: [User!]!
|
||||||
topAuthors: [User]!
|
|
||||||
topShouts: [Shout]!
|
|
||||||
}
|
}
|
||||||
|
|
||||||
############################################ Subscription
|
############################################ Subscription
|
||||||
|
|
Loading…
Reference in New Issue
Block a user