Skip to content

Commit 254edca

Browse files
authored
#266 A user should be able to comment and react to a blog (#160)
* Fix: added comments and reactions * Fix: resolved conflicts
1 parent 0055d90 commit 254edca

14 files changed

+409
-82
lines changed

Diff for: src/index.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ import { jobApplicationTypeDefs } from "./schema/jobApplicationSchema";
8888
import { jobApplicationResolver } from "./resolvers/jobApplicationResolver";
8989
import { blogRelatedResolvers } from "./resolvers/blogRelatedArticlesResolver";
9090
import { blogRelatedArticlesSchema } from "./schema/blogRelatedArticlesSchema";
91+
import { reactionSchema } from "./schema/reactionSchema";
92+
import { reactionResolvers } from "./resolvers/reactionResolvers";
9193
import { DocSchema } from "./schema/doc";
9294
import { docResolver } from "./resolvers/Doc";
9395
const PORT = process.env.PORT || 3000;
@@ -137,7 +139,8 @@ const resolvers = mergeResolvers([
137139
commentLikeResolvers,
138140
commentReplyResolvers,
139141
blogRelatedResolvers,
140-
docResolver
142+
docResolver,
143+
reactionResolvers
141144
]);
142145
const typeDefs = mergeTypeDefs([
143146
applicationCycleTypeDefs,
@@ -180,7 +183,8 @@ const typeDefs = mergeTypeDefs([
180183
commentLikeSchema,
181184
jobApplicationTypeDefs,
182185
blogRelatedArticlesSchema,
183-
DocSchema
186+
DocSchema,
187+
reactionSchema
184188
]);
185189

186190
const server = new ApolloServer({

Diff for: src/models/blogModel.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,24 @@ const blogSchema = new Schema({
3131
ref: "Comment",
3232
},
3333
],
34+
commentLikesCount: {
35+
type: Number,
36+
default: 0,
37+
},
38+
reactions: [
39+
{
40+
type: Schema.Types.ObjectId,
41+
ref: "Reaction",
42+
},
43+
],
44+
reactionsCount: {
45+
LIKE: { type: Number, default: 0 },
46+
LOVE: { type: Number, default: 0 },
47+
CELEBRATE: { type: Number, default: 0 },
48+
SUPPORT: { type: Number, default: 0 },
49+
FUNNY: { type: Number, default: 0 },
50+
},
51+
3452
isHidden: {
3553
type: Boolean,
3654
default: false,
@@ -53,4 +71,4 @@ const blogSchema = new Schema({
5371
},
5472
});
5573

56-
export const BlogModel = model("Blog", blogSchema);
74+
export const BlogModel = model("Blog", blogSchema);

Diff for: src/models/commentModel.ts

+6-28
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,16 @@
11
import mongoose, { Schema, model } from "mongoose";
22

33
const commentSchema = new Schema({
4-
content: {
5-
type: String,
6-
required: true,
7-
},
8-
user: {
9-
type: Schema.Types.ObjectId,
10-
ref: "LoggedUserModel",
11-
required: true,
12-
},
13-
blog: {
14-
type: Schema.Types.ObjectId,
15-
ref: "Blog",
16-
required: true,
17-
},
4+
content: { type: String, required: true },
5+
user: { type: Schema.Types.ObjectId, ref: "LoggedUserModel", required: true },
6+
blog: { type: Schema.Types.ObjectId, ref: "Blog", required: true },
187
likes: [
198
{
209
type: Schema.Types.ObjectId,
21-
ref: "CommentLike",
22-
required: true,
10+
ref: "CommentLike",
2311
},
2412
],
25-
replies: [
26-
{
27-
type: Schema.Types.ObjectId,
28-
ref: "CommentReplies",
29-
required: true,
30-
},
31-
],
32-
created_at: {
33-
type: Date,
34-
default: Date.now,
35-
},
13+
created_at: { type: Date, default: Date.now },
3614
});
3715

38-
export const CommentModel = model("Comment", commentSchema);
16+
export const CommentModel = model("Comment", commentSchema);

Diff for: src/models/reactionModel.ts

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import mongoose, { Schema, model } from "mongoose";
2+
3+
export enum ReactionType {
4+
LIKE = "LIKE",
5+
CELEBRATE = "CELEBRATE",
6+
SUPPORT = "SUPPORT",
7+
LOVE = "LOVE",
8+
FUNNY = "FUNNY",
9+
}
10+
11+
const reactionSchema = new Schema({
12+
user: {
13+
type: Schema.Types.ObjectId,
14+
ref: "LoggedUserModel",
15+
required: true,
16+
},
17+
blog: {
18+
type: Schema.Types.ObjectId,
19+
ref: "Blog",
20+
required: true,
21+
},
22+
type: {
23+
type: String,
24+
enum: Object.values(ReactionType),
25+
required: true,
26+
},
27+
created_at: {
28+
type: Date,
29+
default: Date.now,
30+
},
31+
});
32+
33+
export const ReactionModel = model("Reaction", reactionSchema);

Diff for: src/resolvers/blogResolvers.ts

+12-12
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export const blogResolvers = {
6464
: null;
6565

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

6969
return blogs.filter((blog) => {
7070
if (blog.isHidden) {
@@ -96,7 +96,7 @@ export const blogResolvers = {
9696
},
9797
resolve: async (_: any, { authorId }: GetBlogsByAuthorArgs) => {
9898
return BlogModel.find({ author: authorId }).populate(
99-
"author likes comments"
99+
"author likes comments reactions"
100100
);
101101
},
102102
},
@@ -105,26 +105,26 @@ export const blogResolvers = {
105105
type: BlogType,
106106
args: { id: { type: new GraphQLNonNull(GraphQLID) } },
107107
resolve: async (_: any, { id }: GetBlogByIdArgs) => {
108-
return BlogModel.findById(id)
109-
.populate("author likes comments")
108+
return await BlogModel.findById(id)
109+
.populate("author likes comments reactions")
110110
.populate({
111111
path: "comments",
112112
populate: [
113-
{ path: "user", model: "LogedUserModel" },
113+
{ path: "user", model: "LoggedUserModel" },
114114
{
115115
path: "likes",
116116
model: "CommentLike",
117117
populate: { path: "user", model: "LoggedUserModel" },
118118
},
119-
{
120-
path: "replies",
121-
model: "CommentReply",
122-
populate: { path: "user", model: "LoggedUserModel" },
123-
},
119+
// {
120+
// path: "replies",
121+
// model: "CommentReply",
122+
// populate: { path: "user", model: "LoggedUserModel" },
123+
// },
124124
],
125125
});
126126
},
127-
},
127+
},
128128
},
129129

130130
Mutation: {
@@ -210,4 +210,4 @@ export const blogResolvers = {
210210
return blog;
211211
},
212212
},
213-
};
213+
};

Diff for: src/resolvers/commentLikeResolvers.ts

+76-22
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,95 @@
1-
import { GraphQLID, GraphQLList, GraphQLNonNull } from "graphql";
2-
import { CommentLikeType } from "../types/commentLikeType";
1+
import {
2+
GraphQLID,
3+
GraphQLList,
4+
GraphQLNonNull,
5+
GraphQLInt,
6+
GraphQLObjectType,
7+
} from "graphql";
38
import { CommentLikeModel } from "../models/commentLike";
49
import { CommentModel } from "../models/commentModel";
510

11+
const CommentLikeDetailsType = new GraphQLObjectType({
12+
name: "CommentLikeDetails",
13+
fields: () => ({
14+
count: { type: GraphQLInt },
15+
likes: { type: new GraphQLList(GraphQLID) },
16+
}),
17+
});
18+
619
export const commentLikeResolvers = {
720
Query: {
821
getCommentLikes: {
9-
type: new GraphQLList(CommentLikeType),
10-
args: { blog: { type: new GraphQLNonNull(GraphQLID) } },
22+
type: new GraphQLObjectType({
23+
name: "CommentLikes",
24+
fields: {
25+
count: { type: GraphQLInt },
26+
},
27+
}),
28+
args: { comment: { type: new GraphQLNonNull(GraphQLID) } },
1129
resolve: async (_: any, { comment }: any) => {
12-
return await CommentLikeModel.find({ comment })
13-
.populate("user")
14-
.populate({
15-
path: "comment",
16-
populate: { path: "user" },
17-
});
30+
try {
31+
const commentData = await CommentModel.findById(comment).select("likes");
32+
33+
if (!commentData) {
34+
throw new Error("Comment not found.");
35+
}
36+
37+
return { count: commentData.likes.length };
38+
} catch (error) {
39+
console.error("Error fetching comment likes:", error);
40+
throw new Error("Failed to fetch comment likes.");
41+
}
1842
},
1943
},
2044
},
2145
Mutation: {
2246
addCommentLike: {
23-
type: CommentLikeType,
47+
type: GraphQLInt,
2448
args: {
2549
user: { type: new GraphQLNonNull(GraphQLID) },
2650
comment: { type: new GraphQLNonNull(GraphQLID) },
2751
},
2852
resolve: async (_: any, { user, comment }: any) => {
29-
const like = new CommentLikeModel({ user, comment });
30-
await CommentModel.findByIdAndUpdate(comment, {
31-
$push: { likes: like._id },
32-
});
33-
const savedLike = await like.save();
34-
return (await savedLike.populate("user")).populate({
35-
path: "comment",
36-
populate: { path: "user" },
37-
});
53+
try {
54+
const existingLike = await CommentLikeModel.findOne({ user, comment });
55+
56+
if (existingLike) {
57+
await CommentLikeModel.findByIdAndDelete(existingLike._id);
58+
59+
const updatedComment = await CommentModel.findByIdAndUpdate(
60+
comment,
61+
{ $pull: { likes: existingLike._id } },
62+
{ new: true }
63+
);
64+
65+
if (!updatedComment) {
66+
throw new Error("Failed to update comment after removing the like.");
67+
}
68+
69+
const updatedLikeCount = updatedComment.likes.length;
70+
return updatedLikeCount;
71+
}
72+
73+
const like = new CommentLikeModel({ user, comment });
74+
75+
const savedLike = await like.save();
76+
const updatedComment = await CommentModel.findByIdAndUpdate(
77+
comment,
78+
{ $push: { likes: savedLike._id } },
79+
{ new: true }
80+
);
81+
82+
if (!updatedComment) {
83+
throw new Error("Failed to update comment with the new like.");
84+
}
85+
86+
const updatedLikeCount = updatedComment.likes.length;
87+
return updatedLikeCount;
88+
} catch (error) {
89+
console.error("Error updating comment like:", error);
90+
throw new Error("Failed to update comment like.");
91+
}
3892
},
3993
},
40-
},
41-
};
94+
},
95+
};

Diff for: src/resolvers/commentReplyResolvers.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export const commentReplyResolvers = {
1717
Query: {
1818
getRepliesByComment: {
1919
type: new GraphQLList(CommentReplyType),
20-
args: { blog: { type: new GraphQLNonNull(GraphQLID) } },
20+
args: { comment: { type: new GraphQLNonNull(GraphQLID) } },
2121
resolve: async (_: any, { comment }: GetRepliesByCommentArgs) => {
2222
return await CommentReplyModel.find({ comment })
2323
.populate("user")
@@ -40,7 +40,7 @@ export const commentReplyResolvers = {
4040
_: any,
4141
{ content, user, comment }: AddCommentReplyArgs
4242
) => {
43-
const commentReply = new CommentModel({ content, user, comment });
43+
const commentReply = new CommentReplyModel({ content, user, comment });
4444
await CommentModel.findByIdAndUpdate(comment, {
4545
$push: { comments: commentReply._id },
4646
});
@@ -52,4 +52,4 @@ export const commentReplyResolvers = {
5252
},
5353
},
5454
},
55-
};
55+
};

0 commit comments

Comments
 (0)