CRUD comments
This commit is contained in:
parent
2c5f8335d6
commit
252807b3b1
|
@ -25,8 +25,8 @@ class Comment(Base):
|
||||||
updatedBy = Column(ForeignKey("user.id"), nullable=True, comment="Last Editor")
|
updatedBy = Column(ForeignKey("user.id"), nullable=True, comment="Last Editor")
|
||||||
deletedAt = Column(DateTime, nullable=True, comment="Deleted at")
|
deletedAt = Column(DateTime, nullable=True, comment="Deleted at")
|
||||||
deletedBy = Column(ForeignKey("user.id"), nullable=True, comment="Deleted by")
|
deletedBy = Column(ForeignKey("user.id"), nullable=True, comment="Deleted by")
|
||||||
shout: int = Column(ForeignKey("shout.id"), nullable=True, comment="Shout ID")
|
shout: int = Column(ForeignKey("shout.id"), nullable=False, comment="Shout ID")
|
||||||
rating: int = Column(Integer, nullable=True, comment="Comment Rating")
|
replyTo: int = Column(ForeignKey("comment.id"), nullable=True, comment="comment ID")
|
||||||
ratings = relationship(CommentRating, foreign_keys=CommentRating.comment_id)
|
ratings = relationship(CommentRating, foreign_keys=CommentRating.comment_id)
|
||||||
old_id: str = Column(String, nullable = True)
|
old_id: str = Column(String, nullable = True)
|
||||||
old_thread: str = Column(String, nullable = True)
|
old_thread: str = Column(String, nullable = True)
|
||||||
|
|
|
@ -5,6 +5,7 @@ from resolvers.zine import create_shout, get_shout_by_slug, top_month, top_overa
|
||||||
from resolvers.profile import get_user_by_slug, get_current_user
|
from resolvers.profile import get_user_by_slug, get_current_user
|
||||||
from resolvers.topics import topic_subscribe, topic_unsubscribe, topics_by_author, \
|
from resolvers.topics import topic_subscribe, topic_unsubscribe, topics_by_author, \
|
||||||
topics_by_community, topics_by_slugs
|
topics_by_community, topics_by_slugs
|
||||||
|
from resolvers.comments import create_comment
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"login",
|
"login",
|
||||||
|
|
57
resolvers/comments.py
Normal file
57
resolvers/comments.py
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
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 {}
|
|
@ -46,6 +46,11 @@ type ShoutResult {
|
||||||
shout: Shout
|
shout: Shout
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type CommentResult {
|
||||||
|
error: String
|
||||||
|
comment: Comment
|
||||||
|
}
|
||||||
|
|
||||||
################################### Mutation
|
################################### Mutation
|
||||||
|
|
||||||
type Mutation {
|
type Mutation {
|
||||||
|
@ -79,6 +84,10 @@ type Mutation {
|
||||||
# topics
|
# topics
|
||||||
topicSubscribe(slug: String!): Result!
|
topicSubscribe(slug: String!): Result!
|
||||||
topicUnsubscribe(slug: String!): Result!
|
topicUnsubscribe(slug: String!): Result!
|
||||||
|
|
||||||
|
createComment(body: String!, shout: Int!, replyTo: Int): CommentResult!
|
||||||
|
updateComment(id: Int!, body: String!): CommentResult!
|
||||||
|
deleteComment(id: Int!): Result!
|
||||||
}
|
}
|
||||||
|
|
||||||
################################### Query
|
################################### Query
|
||||||
|
@ -199,7 +208,7 @@ type Comment {
|
||||||
id: Int!
|
id: Int!
|
||||||
author: Int!
|
author: Int!
|
||||||
body: String!
|
body: String!
|
||||||
replyTo: Int!
|
replyTo: Int
|
||||||
createdAt: DateTime!
|
createdAt: DateTime!
|
||||||
updatedAt: DateTime
|
updatedAt: DateTime
|
||||||
updatedBy: Int
|
updatedBy: Int
|
||||||
|
|
Loading…
Reference in New Issue
Block a user