core/resolvers/comments.py

58 lines
1.4 KiB
Python
Raw Normal View History

2021-11-21 11:04:38 +00:00
from orm import Comment, CommentRating
from orm.base import local_session
from resolvers.base import mutation, query, subscription
from auth.authenticate import login_required
import asyncio
from datetime import datetime
@mutation.field("createComment")
@login_required
async def create_comment(_, info, body, shout, replyTo = None):
auth = info.context["request"].auth
user_id = auth.user_id
comment = Comment.create(
author = user_id,
body = body,
shout = shout,
replyTo = replyTo
)
return {"comment": comment}
@mutation.field("updateComment")
@login_required
async def update_comment(_, info, id, body):
auth = info.context["request"].auth
user_id = auth.user_id
with local_session() as session:
comment = session.query(Comment).filter(Comment.id == id).first()
if not comment:
return {"error": "invalid comment id"}
if comment.author != user_id:
return {"error": "access denied"}
comment.body = body
comment.updatedAt = datetime.now()
session.commit()
@mutation.field("deleteComment")
@login_required
async def delete_comment(_, info, id):
auth = info.context["request"].auth
user_id = auth.user_id
with local_session() as session:
comment = session.query(Comment).filter(Comment.id == id).first()
if not comment:
return {"error": "invalid comment id"}
if comment.author != user_id:
return {"error": "access denied"}
comment.deletedAt = datetime.now()
session.commit()
return {}