Skip to content

Commit

Permalink
Merge pull request #123 from fga-eps-mds/i119_Modificar_Funcionamento…
Browse files Browse the repository at this point in the history
…_Do_Like

Modificar funcionamento do like
  • Loading branch information
rafaelmakaha authored Dec 2, 2020
2 parents cea945e + c5cadc1 commit fd213e9
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 94 deletions.
3 changes: 1 addition & 2 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
node_modules
npm-debug.log
Dockerfile
.dockerignore
.env
.dockerignore
14 changes: 6 additions & 8 deletions src/models/Comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ const CommentSchema = new mongoose.Schema({
ref: 'Topic',
require: true,
},
likes: {
type: Number,
default: 0,
},
dislikes: {
type: Number,
default: 0,
},
likes: [
{
type: Schema.Types.ObjectId,
ref: 'Like',
},
],
});

const Comment = mongoose.model('Comment', CommentSchema);
Expand Down
23 changes: 23 additions & 0 deletions src/models/Likes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const mongoose = require('mongoose');

const { Schema } = mongoose;

const likeSchema = new Schema({
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
require: true,
},
topic: {
type: Schema.Types.ObjectId,
ref: 'Topic',
},
comment: {
type: Schema.Types.ObjectId,
ref: 'Comment',
},
});

const Like = mongoose.model('Like', likeSchema);

module.exports = Like;
14 changes: 6 additions & 8 deletions src/models/Topic.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@ const topicSchema = new Schema({
ref: 'User',
require: true,
},
likes: {
type: Number,
default: 0,
},
dislikes: {
type: Number,
default: 0,
},
likes: [
{
type: Schema.Types.ObjectId,
ref: 'Like',
},
],
comments: [
{
type: Schema.Types.ObjectId,
Expand Down
6 changes: 3 additions & 3 deletions src/routes/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ function auth(req, res, next) {
}

try {
const { userId } = jwt.verify(token, authConfig.secret);
req.userId = userId;
jwt.verify(token, authConfig.secret, (err, decoded) => {
req.userId = decoded.id;
});
return next();
} catch (err) {
req.redirect('/login_page');
return res.status(400);
}
}
Expand Down
117 changes: 81 additions & 36 deletions src/routes/commentRoutes.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
const express = require('express');

const router = express.Router();

const User = require('../models/User');
const Like = require('../models/Likes');
const Topic = require('../models/Topic');
const Comment = require('../models/Comment');
const { auth } = require('./auth');

router.post('/create/:topicId/:userId', async (req, res) => {
router.post('/create/:topicId', auth, async (req, res) => {
try {
if (!req.body.text)
return res.status(400).send({ error: 'Comment should not be empty' });

const comment = await Comment.create({
...req.body,
user: req.params.userId,
text: req.body.text,
user: req.userId,
topic: req.params.topicId,
});
const topic = await Topic.findById(req.params.topicId).populate([
Expand All @@ -22,19 +24,17 @@ router.post('/create/:topicId/:userId', async (req, res) => {
]);

await comment.save();

topic.comments.push(comment);
await topic.save();

return res.send(topic);
} catch (err) {
return res.status(400).send({ error: `Error while commenting.${err}` });
}
});

router.put('/update/:commentId', async (req, res) => {
router.put('/update/:commentId', auth, async (req, res) => {
try {
await Comment.findById(req.params.commentId);
const comment = await Comment.findById(req.params.commentId);
const newData = req.body;

if (!newData.text)
Expand All @@ -43,17 +43,23 @@ router.put('/update/:commentId', async (req, res) => {
await Comment.findOneAndUpdate({ _id: req.params.commentId }, req.body, {
useFindAndModify: false,
});
return res.send({ message: 'Comment updated successfully.' });
const newTopic = await Topic.findById(comment.topic).populate([
{ path: 'comments', populate: 'user' },
{ path: 'user' },
{ path: 'plant' },
]);
return res.send(newTopic);
} catch (err) {
return res
.status(400)
.send({ error: `Error while updating comment.${err}` });
}
});

router.delete('/delete/:commentId', async (req, res) => {
router.delete('/delete/:commentId', auth, async (req, res) => {
try {
const topic = Topic.findById(req.body.topicId);
const comment = Comment.findById(req.params.commentId);
const topic = Topic.findById(comment.topic);
const index = topic.comments.indexOf(req.params.commentId);

if (index > -1) {
Expand All @@ -62,43 +68,82 @@ router.delete('/delete/:commentId', async (req, res) => {

topic.save();

await Comment.findByIdAndRemove(req.params.commentId).populate('user');

return res.send({
message: 'Comment successfully removed.',
});
await Comment.findByIdAndRemove(req.params.commentId);
const newTopic = await Topic.findById(comment.topic).populate([
{ path: 'comments', populate: 'user' },
{ path: 'user' },
{ path: 'plant' },
]);
return res.send(newTopic);
} catch (err) {
return res.status(400).send({ error: `Error while deleting topic.${err}` });
}
});

router.post('/like/:commentId', async (req, res) => {
router.post('/like/:commentId', auth, async (req, res) => {
try {
await Comment.findOneAndUpdate(
{ _id: req.params.commentId },
{ $inc: { likes: 1 } },
{ useFindAndModify: false }
);

return res.send({ message: 'Liked!' });
const user = await User.findById(req.userId);
const comment = await Comment.findById(req.params.commentId);
const topic = await Topic.findById(comment.topic).populate([
{ path: 'comments', populate: 'user' },
{ path: 'user' },
{ path: 'plant' },
]);
const isLiked = await Like.findOne({
user: req.userId,
comment: req.params.commentId,
});
if (isLiked == null) {
const like = await Like.create({
user,
comment,
});
await like.save();
comment.likes.push(like);
await comment.save();
const topicTrue = await Topic.findById(comment.topic).populate([
{ path: 'comments', populate: 'user' },
{ path: 'user' },
{ path: 'plant' },
]);
return res.send(topicTrue);
}
return res.send(topic);
} catch (err) {
return res.status(400).send({ error: `Error while liking comment.${err}` });
return res.status(400).send({ error: `Error while commenting.${err}` });
}
});

router.post('/dislike/:commentId', async (req, res) => {
router.post('/dislike/:commentId', auth, async (req, res) => {
try {
await Comment.findOneAndUpdate(
{ _id: req.params.commentId },
{ $inc: { dislikes: 1 } },
{ useFindAndModify: false }
);

return res.send({ message: 'Disliked!' });
const comment = await Comment.findById(req.params.commentId);
const topic = await Topic.findById(comment.topic).populate([
{ path: 'comments', populate: 'user' },
{ path: 'user' },
{ path: 'plant' },
]);
const like = await Like.findOne({
user: req.userId,
comment: req.params.commentId,
});
if (like != null) {
const index = comment.likes.indexOf(like._id);
if (index > -1) {
comment.likes.splice(index, 1);
}

comment.save();
await Like.findByIdAndRemove(like._id);
const topicTrue = await Topic.findById(comment.topic).populate([
{ path: 'comments', populate: 'user' },
{ path: 'user' },
{ path: 'plant' },
]);
return res.send(topicTrue);
}
return res.send(topic);
} catch (err) {
return res
.status(400)
.send({ error: `Error while linking comment.${err}` });
return res.status(400).send({ error: `Error while commenting.${err}` });
}
});

Expand Down
Loading

0 comments on commit fd213e9

Please sign in to comment.