Commit d0e2e134 by qiuzhi99

createComment deleteComment likePost

1 parent 9b4cad9d
const Post = require("../../models/Post");
const checkAuth = require("../../util/check-auth");
const { AuthenticationError, UserInputError } = require("apollo-server");
module.exports = {
Mutation: {
createComment: async (_, { postId, body }, context) => {
const { username } = checkAuth(context);
if (body.trim() === "") {
throw new UserInputError("Empty comment", {
errors: {
body: "Comment body must not empty"
}
});
}
const post = await Post.findById(postId);
if (post) {
post.comments.unshift({
body,
username,
createdAt: new Date().toISOString()
});
await post.save();
return post;
} else throw new UserInputError("Post not found");
},
deleteComment: async (_, { postId, commentId }, context) => {
const { username } = checkAuth(context);
const post = await Post.findById(postId);
if (post) {
const commentIndex = post.comments.findIndex(c => c.id === commentId);
if (post.comments[commentIndex].username === username) {
post.comments.splice(commentIndex, 1);
await post.save();
return post;
} else {
throw new AuthenticationError("Action not allowed");
}
} else {
throw new UserInputError("Post not found");
}
}
}
};
const postsResolvers = require("./posts");
const usersResolvers = require("./users");
const commentsResolvers = require("./comments");
module.exports = {
Query: {
......@@ -7,6 +8,7 @@ module.exports = {
},
Mutation: {
...usersResolvers.Mutation,
...postsResolvers.Mutation
...postsResolvers.Mutation,
...commentsResolvers.Mutation
}
};
const Post = require("../../models/Post");
const checkAuth = require("../../util/check-auth");
const { AuthenticationError } = require("apollo-server");
const { AuthenticationError, UserInputError } = require("apollo-server");
module.exports = {
Query: {
......@@ -55,6 +55,27 @@ module.exports = {
} catch (err) {
throw new Error(err);
}
},
async likePost(_, { postId }, context) {
const { username } = checkAuth(context);
const post = await Post.findById(postId);
if (post) {
if (post.likes.find(like => like.username === username)) {
post.likes = post.likes.filter(like => like.username !== username);
} else {
post.likes.push({
username,
createdAt: new Date().toISOString()
});
}
await post.save();
return post;
} else {
throw new UserInputError("Post not found");
}
}
}
};
......@@ -6,6 +6,21 @@ module.exports = gql`
body: String!
createdAt: String!
username: String!
comments: [Comment]!
likes: [Like]!
}
type Comment {
id: ID!
body: String!
username: String!
createdAt: String!
}
type Like {
id: ID!
username: String!
createdAt: String!
}
type Query {
......@@ -33,5 +48,8 @@ module.exports = gql`
login(username: String!, password: String!): User!
createPost(body: String!): Post!
deletePost(postId: ID!): String!
createComment(postId: ID!, body: String!): Post!
deleteComment(postId: ID!, commentId: ID!): Post!
likePost(postId: ID!): Post!
}
`;
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!