-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#266 A user should be able to comment and react to a blog (#160)
* Fix: added comments and reactions * Fix: resolved conflicts
- Loading branch information
1 parent
0055d90
commit 254edca
Showing
14 changed files
with
409 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
}, | ||
}, | ||
}, | ||
}; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.