Skip to content

Commit

Permalink
#266 A user should be able to comment and react to a blog (#160)
Browse files Browse the repository at this point in the history
* Fix: added comments and reactions

* Fix: resolved conflicts
  • Loading branch information
Philimuhire authored Nov 27, 2024
1 parent 0055d90 commit 254edca
Show file tree
Hide file tree
Showing 14 changed files with 409 additions and 82 deletions.
8 changes: 6 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ import { jobApplicationTypeDefs } from "./schema/jobApplicationSchema";
import { jobApplicationResolver } from "./resolvers/jobApplicationResolver";
import { blogRelatedResolvers } from "./resolvers/blogRelatedArticlesResolver";
import { blogRelatedArticlesSchema } from "./schema/blogRelatedArticlesSchema";
import { reactionSchema } from "./schema/reactionSchema";
import { reactionResolvers } from "./resolvers/reactionResolvers";
import { DocSchema } from "./schema/doc";
import { docResolver } from "./resolvers/Doc";
const PORT = process.env.PORT || 3000;
Expand Down Expand Up @@ -137,7 +139,8 @@ const resolvers = mergeResolvers([
commentLikeResolvers,
commentReplyResolvers,
blogRelatedResolvers,
docResolver
docResolver,
reactionResolvers
]);
const typeDefs = mergeTypeDefs([
applicationCycleTypeDefs,
Expand Down Expand Up @@ -180,7 +183,8 @@ const typeDefs = mergeTypeDefs([
commentLikeSchema,
jobApplicationTypeDefs,
blogRelatedArticlesSchema,
DocSchema
DocSchema,
reactionSchema
]);

const server = new ApolloServer({
Expand Down
20 changes: 19 additions & 1 deletion src/models/blogModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@ const blogSchema = new Schema({
ref: "Comment",
},
],
commentLikesCount: {
type: Number,
default: 0,
},
reactions: [
{
type: Schema.Types.ObjectId,
ref: "Reaction",
},
],
reactionsCount: {
LIKE: { type: Number, default: 0 },
LOVE: { type: Number, default: 0 },
CELEBRATE: { type: Number, default: 0 },
SUPPORT: { type: Number, default: 0 },
FUNNY: { type: Number, default: 0 },
},

isHidden: {
type: Boolean,
default: false,
Expand All @@ -53,4 +71,4 @@ const blogSchema = new Schema({
},
});

export const BlogModel = model("Blog", blogSchema);
export const BlogModel = model("Blog", blogSchema);
34 changes: 6 additions & 28 deletions src/models/commentModel.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,16 @@
import mongoose, { Schema, model } from "mongoose";

const commentSchema = new Schema({
content: {
type: String,
required: true,
},
user: {
type: Schema.Types.ObjectId,
ref: "LoggedUserModel",
required: true,
},
blog: {
type: Schema.Types.ObjectId,
ref: "Blog",
required: true,
},
content: { type: String, required: true },
user: { type: Schema.Types.ObjectId, ref: "LoggedUserModel", required: true },
blog: { type: Schema.Types.ObjectId, ref: "Blog", required: true },
likes: [
{
type: Schema.Types.ObjectId,
ref: "CommentLike",
required: true,
ref: "CommentLike",
},
],
replies: [
{
type: Schema.Types.ObjectId,
ref: "CommentReplies",
required: true,
},
],
created_at: {
type: Date,
default: Date.now,
},
created_at: { type: Date, default: Date.now },
});

export const CommentModel = model("Comment", commentSchema);
export const CommentModel = model("Comment", commentSchema);
33 changes: 33 additions & 0 deletions src/models/reactionModel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import mongoose, { Schema, model } from "mongoose";

export enum ReactionType {
LIKE = "LIKE",
CELEBRATE = "CELEBRATE",
SUPPORT = "SUPPORT",
LOVE = "LOVE",
FUNNY = "FUNNY",
}

const reactionSchema = new Schema({
user: {
type: Schema.Types.ObjectId,
ref: "LoggedUserModel",
required: true,
},
blog: {
type: Schema.Types.ObjectId,
ref: "Blog",
required: true,
},
type: {
type: String,
enum: Object.values(ReactionType),
required: true,
},
created_at: {
type: Date,
default: Date.now,
},
});

export const ReactionModel = model("Reaction", reactionSchema);
24 changes: 12 additions & 12 deletions src/resolvers/blogResolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export const blogResolvers = {
: null;

const filter = tag ? { tags: tag } : {};
const blogs = await BlogModel.find(filter).populate("author likes comments");
const blogs = await BlogModel.find(filter).populate("author likes comments reactions");

return blogs.filter((blog) => {
if (blog.isHidden) {
Expand Down Expand Up @@ -96,7 +96,7 @@ export const blogResolvers = {
},
resolve: async (_: any, { authorId }: GetBlogsByAuthorArgs) => {
return BlogModel.find({ author: authorId }).populate(
"author likes comments"
"author likes comments reactions"
);
},
},
Expand All @@ -105,26 +105,26 @@ export const blogResolvers = {
type: BlogType,
args: { id: { type: new GraphQLNonNull(GraphQLID) } },
resolve: async (_: any, { id }: GetBlogByIdArgs) => {
return BlogModel.findById(id)
.populate("author likes comments")
return await BlogModel.findById(id)
.populate("author likes comments reactions")
.populate({
path: "comments",
populate: [
{ path: "user", model: "LogedUserModel" },
{ path: "user", model: "LoggedUserModel" },
{
path: "likes",
model: "CommentLike",
populate: { path: "user", model: "LoggedUserModel" },
},
{
path: "replies",
model: "CommentReply",
populate: { path: "user", model: "LoggedUserModel" },
},
// {
// path: "replies",
// model: "CommentReply",
// populate: { path: "user", model: "LoggedUserModel" },
// },
],
});
},
},
},
},

Mutation: {
Expand Down Expand Up @@ -210,4 +210,4 @@ export const blogResolvers = {
return blog;
},
},
};
};
98 changes: 76 additions & 22 deletions src/resolvers/commentLikeResolvers.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,95 @@
import { GraphQLID, GraphQLList, GraphQLNonNull } from "graphql";
import { CommentLikeType } from "../types/commentLikeType";
import {
GraphQLID,
GraphQLList,
GraphQLNonNull,
GraphQLInt,
GraphQLObjectType,
} from "graphql";
import { CommentLikeModel } from "../models/commentLike";
import { CommentModel } from "../models/commentModel";

const CommentLikeDetailsType = new GraphQLObjectType({
name: "CommentLikeDetails",
fields: () => ({
count: { type: GraphQLInt },
likes: { type: new GraphQLList(GraphQLID) },
}),
});

export const commentLikeResolvers = {
Query: {
getCommentLikes: {
type: new GraphQLList(CommentLikeType),
args: { blog: { type: new GraphQLNonNull(GraphQLID) } },
type: new GraphQLObjectType({
name: "CommentLikes",
fields: {
count: { type: GraphQLInt },
},
}),
args: { comment: { type: new GraphQLNonNull(GraphQLID) } },
resolve: async (_: any, { comment }: any) => {
return await CommentLikeModel.find({ comment })
.populate("user")
.populate({
path: "comment",
populate: { path: "user" },
});
try {
const commentData = await CommentModel.findById(comment).select("likes");

if (!commentData) {
throw new Error("Comment not found.");
}

return { count: commentData.likes.length };
} catch (error) {
console.error("Error fetching comment likes:", error);
throw new Error("Failed to fetch comment likes.");
}
},
},
},
Mutation: {
addCommentLike: {
type: CommentLikeType,
type: GraphQLInt,
args: {
user: { type: new GraphQLNonNull(GraphQLID) },
comment: { type: new GraphQLNonNull(GraphQLID) },
},
resolve: async (_: any, { user, comment }: any) => {
const like = new CommentLikeModel({ user, comment });
await CommentModel.findByIdAndUpdate(comment, {
$push: { likes: like._id },
});
const savedLike = await like.save();
return (await savedLike.populate("user")).populate({
path: "comment",
populate: { path: "user" },
});
try {
const existingLike = await CommentLikeModel.findOne({ user, comment });

if (existingLike) {
await CommentLikeModel.findByIdAndDelete(existingLike._id);

const updatedComment = await CommentModel.findByIdAndUpdate(
comment,
{ $pull: { likes: existingLike._id } },
{ new: true }
);

if (!updatedComment) {
throw new Error("Failed to update comment after removing the like.");
}

const updatedLikeCount = updatedComment.likes.length;
return updatedLikeCount;
}

const like = new CommentLikeModel({ user, comment });

const savedLike = await like.save();
const updatedComment = await CommentModel.findByIdAndUpdate(
comment,
{ $push: { likes: savedLike._id } },
{ new: true }
);

if (!updatedComment) {
throw new Error("Failed to update comment with the new like.");
}

const updatedLikeCount = updatedComment.likes.length;
return updatedLikeCount;
} catch (error) {
console.error("Error updating comment like:", error);
throw new Error("Failed to update comment like.");
}
},
},
},
};
},
};
6 changes: 3 additions & 3 deletions src/resolvers/commentReplyResolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const commentReplyResolvers = {
Query: {
getRepliesByComment: {
type: new GraphQLList(CommentReplyType),
args: { blog: { type: new GraphQLNonNull(GraphQLID) } },
args: { comment: { type: new GraphQLNonNull(GraphQLID) } },
resolve: async (_: any, { comment }: GetRepliesByCommentArgs) => {
return await CommentReplyModel.find({ comment })
.populate("user")
Expand All @@ -40,7 +40,7 @@ export const commentReplyResolvers = {
_: any,
{ content, user, comment }: AddCommentReplyArgs
) => {
const commentReply = new CommentModel({ content, user, comment });
const commentReply = new CommentReplyModel({ content, user, comment });
await CommentModel.findByIdAndUpdate(comment, {
$push: { comments: commentReply._id },
});
Expand All @@ -52,4 +52,4 @@ export const commentReplyResolvers = {
},
},
},
};
};
Loading

0 comments on commit 254edca

Please sign in to comment.