Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion server/controllers/comment.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,28 @@ function remove(req, res, next) {
return res.status(500).json({});
}

function update(req, res, next) {
const { comment, user } = req;
const { content } = req.body;
if (comment && user) {
if (comment.author._id.toString() !== user._id.toString()) {
return res.status(401).json({ Error: 'Please login' });
}
comment.content = content;
comment.dateLastEdited = Date();
return comment
.save()
.then((editedComment) => {
// Sucess:
res.json(editedComment);
})
.catch((e) => {
next(e);
});
}
return res.status(500).json({});
}

/**
* @swagger
* tags:
Expand Down Expand Up @@ -192,5 +214,6 @@ export default {
load,
list,
create,
remove
remove,
update
};
32 changes: 29 additions & 3 deletions server/controllers/forum.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,39 @@ function create(req, res, next) {
.catch(err => next(err));
}

function update() {
function update(req, res, next) {
const {
forumThread, user
} = req;
const {
content, title
} = req.body;

if (forumThread && forumThread.author && forumThread.author._id && user) {
if (forumThread.author._id.toString() !== user._id.toString()) {
return res.status(401).json({ Error: 'Please login' });
}
forumThread.content = content;
forumThread.title = title;
forumThread.dateLastEdited = Date();
return forumThread
.save()
.then((editedThread) => {
// Sucess:
res.json(editedThread);
})
.catch((e) => {
next(e);
});
}
return res.status(500).json({});
}

function remove(req, res, next) {
const { forumThread, user } = req;
if (forumThread && user) {
if (forumThread.author.toString() !== user._id.toString()) {
// TODO: if admin should be able to delete too
if (forumThread && forumThread.author && forumThread.author._id && user) {
if (forumThread.author._id.toString() !== user._id.toString()) {
return res.status(401).json({ Error: 'Please login' });
}
forumThread.deleted = true;
Expand Down
4 changes: 1 addition & 3 deletions server/models/comment.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ const CommentSchema = new Schema({
dateDeleted: {
type: Date
},
lastEdited: {
type: Date
},
dateLastEdited: { type: Date },
rootEntity: {
type: Schema.Types.ObjectId // The entity that owns this comment
// , ref: 'Post' | 'AMA'
Expand Down
1 change: 1 addition & 0 deletions server/models/forumThread.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const ForumThreadSchema = new mongoose.Schema({
},
commentsCount: { type: Number, default: 0 },
dateLastAcitiy: { type: Date, default: Date.now },
dateLastEdited: { type: Date },
dateCreated: { type: Date, default: Date.now }
});

Expand Down
5 changes: 4 additions & 1 deletion server/routes/forum.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ router
expressJwt({ secret: config.jwtSecret, credentialsRequired: false }),
forumCtrl.detail
)
.put(expressJwt({ secret: config.jwtSecret, credentialsRequired: true }), forumCtrl.update)
.put(
expressJwt({ secret: config.jwtSecret, credentialsRequired: true }),
forumCtrl.update
)
.delete(
expressJwt({ secret: config.jwtSecret, credentialsRequired: true }),
forumCtrl.remove
Expand Down