Skip to content

Commit

Permalink
Changes for reactions (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
AuraOfDivinity authored Aug 7, 2020
1 parent 581d7f0 commit 4b33b4b
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 4 deletions.
83 changes: 79 additions & 4 deletions app/controllers/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,19 @@ module.exports = {
.json({ message: 'No post exists' })
}
// permission check for admin and creator || edit allowed or not
if (!permission.check(req, res, post.userId) || (!settingsHelper.canEdit())) {
return res.status(HttpStatus.FORBIDDEN).json({ message: 'Bad update request' })
if (
!permission.check(req, res, post.userId) ||
!settingsHelper.canEdit()
) {
return res
.status(HttpStatus.FORBIDDEN)
.json({ message: 'Bad update request' })
}
// if allowed edit limit check
if (!settingsHelper.isEditAllowedNow(post.createdAt)) {
return res.status(HttpStatus.BAD_REQUEST).json({ msg: 'Edit limit expired!' })
return res
.status(HttpStatus.BAD_REQUEST)
.json({ msg: 'Edit limit expired!' })
}
updates.forEach((update) => {
post[update] = req.body[update]
Expand Down Expand Up @@ -141,6 +148,8 @@ module.exports = {
upvote: async (req, res, next) => {
const { id } = req.params
const userId = req.user.id.toString()
const reactionType = req.body.reactionType

try {
const post = await PostModel.findById(id)
if (!post) {
Expand All @@ -156,7 +165,73 @@ module.exports = {
.json({ error: 'Bad request' })
}
})
post.votes.upVotes.user.unshift(userId)
switch (reactionType) {
case 'like':
post.votes.upVotes.user.unshift(userId)
break
case 'heart':
post.votes.heart.user.unshift(userId)
break
case 'happy':
post.votes.happy.user.unshift(userId)
break
case 'donut':
post.votes.donut.user.unshift(userId)
break
default:
}
await post.save()
res.status(HttpStatus.OK).json({ post: post })
} catch (error) {
HANDLER.handleError(res, error)
}
},

// REMOVE REACTION
removeReaction: async (req, res, next) => {
const { id } = req.params
const userId = req.user.id.toString()
const reactionType = req.body.reactionType

try {
const post = await PostModel.findById(id)
if (!post) {
return res
.status(HttpStatus.NOT_FOUND)
.json({ error: 'No post found' })
}
// CHECKS IF THE USER HAS ALREADY UPVOTED THE COMMENT
post.votes.upVotes.user.filter((user) => {
if (JSON.stringify(user) === JSON.stringify(userId)) {
return res
.status(HttpStatus.BAD_REQUEST)
.json({ error: 'Bad request' })
}
})
switch (reactionType) {
case 'like':
post.votes.upVotes.user = post.votes.upVotes.user.filter(item =>
item !== userId
)
break
case 'heart':
post.votes.heart.user = post.votes.heart.user.filter(item =>
item !== userId
)
console.log(post.votes.heart.user)
break
case 'happy':
post.votes.happy.user = post.votes.happy.user.filter(item =>
item !== userId
)
break
case 'donut':
post.votes.donut.user = post.votes.donut.user.filter(item =>
item !== userId
)
break
default:
}
await post.save()
res.status(HttpStatus.OK).json({ post: post })
} catch (error) {
Expand Down
18 changes: 18 additions & 0 deletions app/models/Post.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@ const PostSchema = new Schema({
type: Schema.Types.ObjectId,
ref: 'User'
}]
},
heart: {
user: [{
type: Schema.Types.ObjectId,
ref: 'User'
}]
},
happy: {
user: [{
type: Schema.Types.ObjectId,
ref: 'User'
}]
},
donut: {
user: [{
type: Schema.Types.ObjectId,
ref: 'User'
}]
}
},
comments: {
Expand Down
8 changes: 8 additions & 0 deletions app/routes/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ router.patch(
postController.upvote
)

// REMOVE REACTION FROM POST
router.patch(
'/removereaction/:id',
isUnderMaintenance,
auth,
postController.removeReaction
)

// GET POST PER USER
router.get(
'/:id/all',
Expand Down

0 comments on commit 4b33b4b

Please sign in to comment.