From 475150ae19dbe4e9516708434ff85b489ca658ec Mon Sep 17 00:00:00 2001 From: zpeng Date: Sun, 12 Feb 2017 13:33:00 +0800 Subject: [PATCH 1/2] detailpost not login --- controllers/posts/detailPost.js | 25 +++++++++---------------- public/javascripts/main.js | 4 ++++ 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/controllers/posts/detailPost.js b/controllers/posts/detailPost.js index d02aa0b..292ade1 100644 --- a/controllers/posts/detailPost.js +++ b/controllers/posts/detailPost.js @@ -25,14 +25,6 @@ module.exports = { var id = req.params["id"]; - if ( ! req.session.user) { - var error = { - code: 300, - message: "please login" - } - return res.render('index.jade', { title: 'Xgag', error: error}); - } - if (! id) { return res.json({ code: 500, @@ -57,21 +49,22 @@ module.exports = { }); }, addcomment: function(req, res){ + + if ( ! req.session.user) { + return res.json({ + code: 300, + message: "please login" + }); + } + var date = new Date(); var id = req.params["id"].replace(/id=/g, ""); - console.log(req.session.user); - var newComment = new models.comment; + var newComment = new models.comment; newComment.user = req.session.user.id; newComment.create_date = date; newComment.message = req.body.message; newComment.status = req.body.status; - if ( ! req.session.user || typeof req.session.user==undefined) { - return res.json({ - code: 300, - message: "please login" - }); - } if (! id) { return res.json({ diff --git a/public/javascripts/main.js b/public/javascripts/main.js index 945104a..a6e9771 100644 --- a/public/javascripts/main.js +++ b/public/javascripts/main.js @@ -204,6 +204,10 @@ $(function(){ status: status }, function (result) { // clean input + result = JSON.parse(JSON.stringify(result)); + if(result.code != 200){ + return alert(result.message); + } node.find('input[name=comment]').val(''); $("#"+postId+" > div.comment-box > div.comment-area").text(''); getCommentsAjax(postId); From 2a5f3de6fbae7a1f72b98f171eeb19a10f793a7e Mon Sep 17 00:00:00 2001 From: zpeng Date: Sun, 12 Feb 2017 21:33:18 +0800 Subject: [PATCH 2/2] fix the code position and hide the comment form if not login --- controllers/index.js | 2 +- public/javascripts/main.js | 3 +-- views/postDetail.jade | 18 +++++++++++------- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/controllers/index.js b/controllers/index.js index ea462cd..2cf2e50 100644 --- a/controllers/index.js +++ b/controllers/index.js @@ -55,4 +55,4 @@ function isLoggedIn(req, res, next) { // if they aren't redirect them to the home page res.redirect('/'); -} \ No newline at end of file +} diff --git a/public/javascripts/main.js b/public/javascripts/main.js index a6e9771..5cf6d24 100644 --- a/public/javascripts/main.js +++ b/public/javascripts/main.js @@ -203,11 +203,10 @@ $(function(){ message: message, status: status }, function (result) { - // clean input - result = JSON.parse(JSON.stringify(result)); if(result.code != 200){ return alert(result.message); } + // clean input node.find('input[name=comment]').val(''); $("#"+postId+" > div.comment-box > div.comment-area").text(''); getCommentsAjax(postId); diff --git a/views/postDetail.jade b/views/postDetail.jade index 53f4de8..8a38606 100644 --- a/views/postDetail.jade +++ b/views/postDetail.jade @@ -71,13 +71,17 @@ block content i.fa.fa-thumbs-o-down span.dislike #{posts.dislike.length} .comment-box - .comment-area - .comment-body - form.comment-form(action="/comment/add/#{posts._id}" role="form") - .form-group - input(type="text" class="form-control" name="comment" placeholder="評論..." value="") - input(type="hidden" name="status" value="") - input(type="hidden" name="post_id" value="#{posts._id}") + .comment-area + if !user + .content-text + |留言請登入 + else + .comment-body + form.comment-form(action="/comment/add/#{posts._id}" role="form") + .form-group + input(type="text" class="form-control" name="comment" placeholder="評論..." value="") + input(type="hidden" name="status" value="") + input(type="hidden" name="post_id" value="#{posts._id}") .col-md-4(style="margin-top: 80px;")