Skip to content

Commit

Permalink
test cases for like compoenent
Browse files Browse the repository at this point in the history
  • Loading branch information
YohanChabiduPerera committed Apr 23, 2024
1 parent 6dc5ec5 commit 8d87869
Show file tree
Hide file tree
Showing 8 changed files with 327 additions and 152 deletions.
156 changes: 78 additions & 78 deletions controller/like.js
Original file line number Diff line number Diff line change
@@ -1,85 +1,85 @@
const Like = require("../model/like");
const mongoose = require('mongoose');
// const Like = require("../model/like");
// const mongoose = require('mongoose');

const addEmoji = async (req, res) => {
try {
const { userId, emoji } = req.body;
const postId = req.params.postId;
const existingReaction = await Like.findOne({ userID: userId, postID: postId });
if (existingReaction) {
await selectEmoji(req, res);
} else {
await Like.create({ userID: userId, postID: postId, emoji: emoji });
res.sendStatus(200);
}
} catch (error) {
res.status(500).json({ error: error.message });
}
};
// const addEmoji = async (req, res) => {
// try {
// const { userId, emoji } = req.body;
// const postId = req.params.postId;
// const existingReaction = await Like.findOne({ _id: postId }, 'likes');
// if (existingReaction) {
// await changeEmoji(req, res);
// } else {
// await Like.({ userID: userId, emoji: emoji });
// res.sendStatus(200);
// }
// } catch (error) {
// res.status(500).json({ error: error.message });
// }
// };

const getTotalReactions = async (req, res) => {
try {
const postId = req.params.postId;
const totalReactions = await Like.countDocuments({ postID: postId });
res.json({ totalReactions: totalReactions || 0 });
} catch (error) {
res.status(500).json({ error: error.message });
}
};
// const getTotalReactions = async (req, res) => {
// try {
// const postId = req.params.postId;
// const totalReactions = await Like.countDocuments({ postID: postId }, 'likes');
// res.json({ totalReactions: totalReactions || 0 });
// } catch (error) {
// res.status(500).json({ error: error.message });
// }
// };

const getEmojiCounts = async (req, res) => {
try {
const postId = new mongoose.Types.ObjectId(req.params.postId);
const emojiCounts = await Like.aggregate([
{ $match: { postID: postId } },
{ $group: { _id: '$emoji', count: { $sum: 1 } } },
]);
res.json(emojiCounts.length ? emojiCounts : [{ _id: null, count: 0 }]);
} catch (error) {
res.status(500).json({ error: error.message });
}
};
// const getEmojiCounts = async (req, res) => {
// try {
// const postId = new mongoose.Types.ObjectId(req.params.postId);
// const emojiCounts = await Like.aggregate([
// { $match: { postID: postId } },
// { $group: { _id: '$emoji', count: { $sum: 1 } } },
// ]);
// res.json(emojiCounts.length ? emojiCounts : [{ _id: null, count: 0 }]);
// } catch (error) {
// res.status(500).json({ error: error.message });
// }
// };

const selectEmoji = async (req, res) => {
try {
const { userId, emoji } = req.body;
const postId = req.params.postId;
await Like.findOneAndUpdate(
{ userID: userId, postID: postId },
{ userID: userId, postID: postId, emoji: emoji },
{ upsert: true }
);
res.sendStatus(200);
} catch (error) {
res.status(500).json({ error: error.message });
}
};
// const changeEmoji = async (req, res) => {
// try {
// const { userId, emoji } = req.body;
// const postId = req.params.postId;
// await Like.findOneAndUpdate(
// { userID: userId, postID: postId },
// { userID: userId, postID: postId, emoji: emoji },
// { upsert: true }
// );
// res.sendStatus(200);
// } catch (error) {
// res.status(500).json({ error: error.message });
// }
// };

const removeEmoji = async (req, res) => {
try {
const { userId } = req.body;
const postId = req.params.postId;
await Like.findOneAndDelete({ userID: userId, postID: postId });
res.sendStatus(200);
} catch (error) {
res.status(500).json({ error: error.message });
}
};
// const removeEmoji = async (req, res) => {
// try {
// const { userId } = req.body;
// const postId = req.params.postId;
// await Like.findOneAndDelete({ userID: userId, postID: postId });
// res.sendStatus(200);
// } catch (error) {
// res.status(500).json({ error: error.message });
// }
// };

const getAllReactions = async (req, res) => {
try {
const allReactions = await Like.find().populate('userID').populate('postID');
res.json(allReactions);
} catch (error) {
res.status(500).json({ error: error.message });
}
};
// const getAllReactions = async (req, res) => {
// try {
// const allReactions = await Like.find().populate('userID').populate('postID');
// res.json(allReactions);
// } catch (error) {
// res.status(500).json({ error: error.message });
// }
// };

module.exports = {
addEmoji,
getTotalReactions,
getEmojiCounts,
selectEmoji,
removeEmoji,
getAllReactions
};
// module.exports = {
// addEmoji,
// getTotalReactions,
// getEmojiCounts,
// changeEmoji,
// removeEmoji,
// getAllReactions
// };
164 changes: 164 additions & 0 deletions controller/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,165 @@ const getPostsByStatus = async (req, res) => {
}
};

// Likes Management

const addEmoji = async (req, res) => {
try {
const { userId, emoji } = req.body;
const postId = req.params.postId;

if (!userId) {
return res.status(400).json({ error: "User ID is required" });
}

const post = await postModel.findById(postId);

if (!post) {
return res.status(404).json({ error: "Post not found" });
}

const existingReaction = post.likes.find((like) => like.userID === userId);

if (existingReaction) {
existingReaction.emoji = emoji;
} else {
post.likes.push({ userID: userId, emoji: emoji });
}

await post.save();

res.sendStatus(200);
} catch (error) {
res.status(500).json({ error: error.message });
}
};

// const changeEmoji = async (req, res) => {
// try {
// const { userId, emoji } = req.body;
// const postId = req.params.postId;

//
// const post = await postModel.findById(postId);

// if (!post) {
// return res.status(404).json({ error: "Post not found" });
// }

//
// const existingReactionIndex = post.likes.findIndex(like => like.userID === userId);

// if (existingReactionIndex !== -1) {
//
// post.likes[existingReactionIndex].emoji = emoji;
// } else {
//
// post.likes.push({ userID: userId, emoji: emoji });
// }

//
// await post.save();

// res.sendStatus(200);
// } catch (error) {
// res.status(500).json({ error: error.messageΒ });
// Β Β }
// };

const getEmojiCounts = async (req, res) => {
try {
const postId = req.params.postId;

const post = await postModel.findById(postId);

if (!post) {
return res.status(404).json({ error: "Post not found" });
}

const emojiCounts = {};

// Iterate over the likes
for (let like of post.likes) {
if (emojiCounts[like.emoji]) {
emojiCounts[like.emoji]++;
} else {
emojiCounts[like.emoji] = 1;
}
}

const emojiCountsArray = Object.keys(emojiCounts).map((emoji) => ({
_id: emoji,
count: emojiCounts[emoji],
}));

res.json(
emojiCountsArray.length ? emojiCountsArray : [{ _id: null, count: 0 }]
);
} catch (error) {
res.status(500).json({ error: error.message });
}
};

const getTotalReactions = async (req, res) => {
try {
const postId = req.params.postId;

const post = await postModel.findById(postId);

if (!post) {
return res.status(404).json({ error: "Post not found" });
}

const totalReactions = post.likes.length;

res.json({ totalReactions: totalReactions });
} catch (error) {
res.status(500).json({ error: error.message });
}
};

const getAllReactions = async (req, res) => {
try {
const allReactions = await postModel.find({}, 'likes');

const flattenedReactions = allReactions.reduce((acc, cur) => {
acc.push(...cur.likes.map(like => ({ postId: cur._id.toString(), userId: like.userID, emoji: like.emoji })));
return acc;
}, []);

res.json(flattenedReactions);
} catch (error) {
res.status(500).json({ error: error.message });
}
};

const removeEmoji = async (req, res) => {
try {
const { userId } = req.body;
const postId = req.params.postId;

const post = await postModel.findById(postId);

if (!post) {
return res.status(404).json({ error: "Post not found" });
}

const likeIndex = post.likes.findIndex(like => like.userID === userId);

if (likeIndex === -1) {
return res.status(404).json({ error: "User reaction not found for this post" });
}

post.likes.splice(likeIndex, 1);

await post.save();

res.sendStatus(200);
} catch (error) {
res.status(500).json({ error: error.message });
}
};

module.exports = {
createPost,
getAllPosts,
Expand All @@ -447,4 +606,9 @@ module.exports = {
updatePostTags,
getPostsByStatus,
deleteTag,
getTotalReactions,
addEmoji,
getEmojiCounts,
getAllReactions,
removeEmoji
};
36 changes: 18 additions & 18 deletions model/like.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
// const mongoose = require('mongoose');
// const Schema = mongoose.Schema;

const validEmojis = ['πŸ‘', '❀️', 'πŸ˜„', '😯', '😒'];
// const validEmojis = ['πŸ‘', '❀️', 'πŸ˜„', '😯', '😒'];

const likeSchema = new Schema({
userID: {
type: Schema.Types.ObjectId,
ref: "User",
},
postID: {
type: Schema.Types.ObjectId,
ref: "Post",
},
emoji: {
type: String,
enum: validEmojis,
},
});
// const likeSchema = new Schema({
// userID: {
// type: Schema.Types.ObjectId,
// ref: "User",
// },
// postID: {
// type: Schema.Types.ObjectId,
// ref: "Post",
// },
// emoji: {
// type: String,
// enum: validEmojis,
// },
// });

module.exports= mongoose.model('Like', likeSchema);
// module.exports= mongoose.model('Like', likeSchema);
Loading

0 comments on commit 8d87869

Please sign in to comment.