From 36a0770675fae9804e7957e70274e364ce566f92 Mon Sep 17 00:00:00 2001 From: knst-kotov Date: Wed, 24 Nov 2021 09:23:48 +0300 Subject: [PATCH] rate comment --- resolvers/comments.py | 21 +++++++++++++++++++++ schema.graphql | 1 + 2 files changed, 22 insertions(+) diff --git a/resolvers/comments.py b/resolvers/comments.py index 16bfe87a..68344ce6 100644 --- a/resolvers/comments.py +++ b/resolvers/comments.py @@ -55,3 +55,24 @@ async def delete_comment(_, info, id): session.commit() return {} + +@mutation.field("rateComment") +@login_required +async def rate_comment(_, info, id, value): + auth = info.context["request"].auth + user_id = auth.user_id + + with local_session() as session: + rating = session.query(CommentRating).\ + filter(CommentRating.comment_id == id and CommentRating.createdBy == user_id).first() + if rating: + rating.value = value + session.commit() + return {} + + CommentRating.create( + comment_id = id, + createdBy = user_id, + value = value) + + return {} diff --git a/schema.graphql b/schema.graphql index cc81c538..18a37831 100644 --- a/schema.graphql +++ b/schema.graphql @@ -88,6 +88,7 @@ type Mutation { createComment(body: String!, shout: Int!, replyTo: Int): CommentResult! updateComment(id: Int!, body: String!): CommentResult! deleteComment(id: Int!): Result! + rateComment(id: Int!, value: Int!): Result! } ################################### Query