add rateShout

This commit is contained in:
knst-kotov 2021-09-25 14:40:37 +03:00
parent 9a5ec80b12
commit f23d375b4d
4 changed files with 35 additions and 19 deletions

View File

@ -7,17 +7,7 @@ from sqlalchemy.sql.schema import Table
from settings import DB_URL
# engine = create_engine(DB_URL, convert_unicode=True, echo=False)
engine = create_engine(DB_URL,
convert_unicode=True,
echo=False,
#pool_size=10,
#max_overflow=2,
#pool_recycle=300,
pool_pre_ping=True,
#pool_use_lifo=True
future=True
)
engine = create_engine(DB_URL, convert_unicode=True, echo=False)
T = TypeVar("T")

View File

@ -35,7 +35,7 @@ class ShoutRating(Base):
id = None
rater_id = Column(ForeignKey('user.id'), primary_key = True)
shout_id = Column(ForeignKey('shout.id'), primary_key = True)
ts: str = Column(DateTime, nullable=False, default = datetime.now, comment="Timestamp")
ts = Column(DateTime, nullable=False, default = datetime.now, comment="Timestamp")
value = Column(Integer)
class ShoutRatingStorage:
@ -48,8 +48,8 @@ class ShoutRatingStorage:
return reduce((lambda x, y: x + y.value), shout_ratings, 0)
def update_rating(self, new_rating):
rating = next(x for x in self.ratings \
if x.rater_id == new_rating.rater_id and x.shout_id == new_rating.shout_id)
rating = next((x for x in self.ratings \
if x.rater_id == new_rating.rater_id and x.shout_id == new_rating.shout_id), None)
if rating:
rating.value = new_rating.value
rating.ts = new_rating.ts

View File

@ -12,7 +12,7 @@ import asyncio
from datetime import datetime, timedelta
from pathlib import Path
from sqlalchemy import select, func, desc
from sqlalchemy import select, func, desc, and_
from sqlalchemy.orm import selectinload
class GitTask:
@ -260,13 +260,39 @@ async def update_shout(_, info, id, input):
"shout" : shout
}
@mutation.field("rateShout")
@login_required
async def rate_shout(_, info, shout_id, value):
auth = info.context["request"].auth
user_id = auth.user_id
with local_session() as session:
rating = session.query(ShoutRating).\
filter(and_(ShoutRating.rater_id == user_id, ShoutRating.shout_id == shout_id)).first()
if rating:
rating.value = value;
rating.ts = datetime.now()
session.commit()
else:
rating = ShoutRating.create(
rater_id = user_id,
shout_id = shout_id,
value = value
)
rating_storage.update_rating(rating)
return {"error" : ""}
@query.field("getShoutBySlug")
async def get_shout_by_slug(_, info, slug):
slug_fields = [node.name.value for node in info.field_nodes[0].selection_set.selections]
slug_fields = set(["authors", "comments", "topics"]).intersection(slug_fields)
select_options = [selectinload(getattr(Shout, field)) for field in slug_fields]
with local_session() as session:
shout = session.query(Shout).\
options(selectinload(Shout.authors)).\
options(selectinload(Shout.comments)).\
options(selectinload(Shout.topics)).\
options(select_options).\
filter(Shout.slug == slug).first()
shout.rating = rating_storage.get_rating(shout.id)
shout.views = view_storage.get_view(shout.id)

View File

@ -68,7 +68,7 @@ type Mutation {
createShout(input: ShoutInput!): ShoutResult!
updateShout(id: Int!, input: ShoutInput!): ShoutResult!
deleteShout(id: Int!): Result!
rateShout(id: Int!, value: Int!): Result!
rateShout(shout_id: Int!, value: Int!): Result!
# user profile
# rateUser(value: Int!): Result!