-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6dc5ec5
commit 8d87869
Showing
8 changed files
with
327 additions
and
152 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
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 | ||
// }; |
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,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); |
Oops, something went wrong.