Skip to content

Commit

Permalink
post crud
Browse files Browse the repository at this point in the history
  • Loading branch information
zqhZY committed Jul 10, 2016
1 parent b165660 commit 97a628c
Show file tree
Hide file tree
Showing 9 changed files with 376 additions and 158 deletions.
49 changes: 43 additions & 6 deletions api/controllers/PostController.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,7 @@ module.exports = {
count: req.body.count
};

console.log('----------------->', pages);

Post.find().exec(function (err, posts) {
console.log('err----------------->', err);
console.log('posts----------------->', posts);
if (err)
res.send(err);
else
Expand All @@ -60,14 +56,55 @@ module.exports = {

var postid = req.body.postid;

console.log('posts------------->', postid);
console.log('delete postid------------->', postid);
Post.destroy({id:postid}).exec(function (err){
if (err) {
return res.negotiate(err);
}
sails.log('Any post named Finn have now been deleted, if there were any.');
return res.ok();
return res.redirect('/admin_posts');
});
},

updateview: function (req, res) {

var postid = req.body.postid;

console.log('updateview posts------------->', postid);

Post.find({id: postid}).exec(function (err, post) {
// console.log('posts----------------->', post);
if (err)
res.send(err);
else
res.view('admin/post_update', {updatepost: post});


});
},

update: function (req, res) {

var postid = req.body.postid;
console.log('update postids------------->', postid);

var post = {
"title": req.body.title,
"author": req.body.author,
"imagePath": req.body.imagePath,
"detail": req.body.detail,
"Content": req.body.Content,
"createtime": req.body.createtime
};

Post.update({id:postid}, post).exec(function (err){
if (err) {
return res.negotiate(err);
}
sails.log('Any post named Finn have now been update, if there were any.');
return res.redirect('/admin_posts');
});

}

};
Expand Down
4 changes: 2 additions & 2 deletions api/controllers/TagsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ module.exports = {

var tagid = req.body.tagid;

console.log('tags------------->', tagid);
console.log('tagsid------------->', tagid);
Tags.destroy({id:tagid}).exec(function (err){
if (err) {
return res.negotiate(err);
}
sails.log('Any tag named Finn have now been deleted, if there were any.');
return res.ok();
res.redirect('/admin_tags');
});
}

Expand Down
90 changes: 78 additions & 12 deletions assets/js/admin/AdminPosts.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,66 @@ Posts.service('PostService', ['$http','$q', function ($http, $q) {
return _getDataPromise;
}

_deletePost = function (id) {
// _deletePost = function (id) {
// var message = {
// postid: id
// };
// _deletePostPromise =
// $http.post('/delete_post.json', message).then(function (data) {
// if (data)
// return data;
// },
// function (err) {
// return $q.reject(err);
// });
// return _deletePostPromise;
//
// }


// _loadUpdatePage = function (id) {
// var message = {
// postid: id
// };
// _updateViewPromise =
// $http.post('/update_view.json', message).then(function (data) {
// if (data)
// return data;
// },
// function (err) {
// return $q.reject(err);
// });
// return _updateViewPromise;
//
//
// }


_updatePost = function (id) {
var message = {
postid: id
};
_deletePostPromise =
$http.post('/delete_post.json', message).then(function (data) {

_updatePostPromise =
$http.post('/update_post.json', message).then(function (data) {
if (data)
return data;
},
function (err) {
return $q.reject(err);
});
return _deletePostPromise;
return _updatePostPromise;

}




_PostService = {
_getData: _getData,
deletePost: _deletePost
// deletePost: _deletePost,
updatePost: _updatePost,
// loadUpdatePage: _loadUpdatePage
}
return _PostService;
}
Expand All @@ -72,15 +112,41 @@ Posts.controller('PostCtrl', ['$scope','$window', 'NgTableParams','PostService',
});
});

$scope.deletePost = function (id) {
console.log('--------------->', id);
PostService.deletePost(id)
// $scope.deletePost = function (id) {
// console.log('--------------->', id);
// PostService.deletePost(id)
// .then(function (res) {
// //reload
// console.log('--------------->', res);
// $scope.tableParams.reload();
//
// });
// }

// $scope.updateView = function (id) {
// console.log('------------>', id);
// PostService.loadUpdatePage(id)
// .then(function (res) {
// console.log('-------------->', res);
// // $scope.title_up = res.data.title;
// // $scope.author_up = res.data.author;
// // $scope.imagePath_up = res.data.imagePath;
// // $scope.detail_up = res.data.detail;
// // $scope.Content_up = res.data.Content;
// // $scope.createtime_up = res.data.createtime;
// })
//
// }


$scope.updatePost = function(id){
console.log('------------->', id);
PostService.updatePost(id)
.then(function (res) {
//reload
console.log('--------------->', res);
$scope.tableParams.reload();
console.log('------------->', res);
})


});
}
}
]);
Expand Down
55 changes: 28 additions & 27 deletions assets/js/admin/AdminTags.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,28 @@ Tags.service('tagService', ['$http','$q', function ($http, $q) {
});
return _getDataPromise;
}

_deleteTag = function (id) {
var message = {
tagid: id
};
_deleteTagPromise =
$http.post('/delete_tag.json', message).then(function (data) {
if (data)
return data;
},
function (err) {
return $q.reject(err);
});
return _deleteTagPromise;

}

//
// _deleteTag = function (id) {
// var message = {
// tagid: id
// };
// _deleteTagPromise =
// $http.post('/delete_tag.json', message).then(function (data) {
// if (data)
// return data;
// },
// function (err) {
// return $q.reject(err);
// });
// return _deleteTagPromise;
//
// }


_tagService = {
_getData: _getData,
deleteTag: _deleteTag
// deleteTag: _deleteTag
}
return _tagService;
}
Expand All @@ -68,16 +69,16 @@ Tags.controller('tagCtrl', ['$scope','$window', 'NgTableParams','tagService',
});
});

$scope.deleteTag = function (id) {
console.log('--------------->', id);
tagService.deleteTag(id)
.then(function (res) {
//reload
console.log('--------------->', res);
$scope.tableParams.reload();

});
}
// $scope.deleteTag = function (id) {
// console.log('--------------->', id);
// tagService.deleteTag(id)
// .then(function (res) {
// //reload
// console.log('--------------->', res);
// $scope.tableParams.reload();
//
// });
// }
}
]);

Expand Down
2 changes: 2 additions & 0 deletions config/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ module.exports.routes = {
'post /post_create': 'PostController.create',
'post /posts.json': 'PostController.list',
'post /delete_post.json': 'PostController.delete',
'post /update_post.json': 'PostController.update',
'post /update_view.json': 'PostController.updateview',



Expand Down
88 changes: 12 additions & 76 deletions views/admin/admin_post.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,19 @@

<td class="list-buttons-column">

<a class="icon" href="/sqla/simple/admin/tag/edit/?url=%2Fsqla%2Fsimple%2Fadmin%2Ftag%2F&amp;id=1" title="Edit record">
<span class="fa fa-pencil glyphicon glyphicon-pencil"></span>
</a>

<form class="icon" method="POST" ng-submit="deleteTag(post.id)">
<input id="id" name="id" value="1" type="hidden">
<input id="url" name="url" value="/sqla/simple/admin/tag/" type="hidden">
<form class="icon" method="POST" action="/update_view.json">
<input id="id" name="postid" value="{{post.id}}" type="hidden">
<!--<input id="url" name="url" value="/sqla/simple/admin/tag/" type="hidden">-->

<button type="submit">
<span class="fa fa-pencil glyphicon glyphicon-pencil"></span>
</button>
</form>


<form class="icon" method="POST" action="/delete_post.json">
<input id="id" name="postid" value="{{post.id}}" type="hidden">

<button onclick="return confirm('Are you sure you want to delete this record?');" title="Delete record">
<span class="fa fa-trash glyphicon glyphicon-trash"></span>
Expand All @@ -207,75 +213,5 @@
</table>



<ul class="pagination">


















<li class="disabled">
<a href="javascript:void(0)">«</a>
</li>


<li class="disabled">
<a href="javascript:void(0)">&lt;</a>
</li>




<li class="active">
<a href="javascript:void(0)">1</a>
</li>



<li>
<a href="/sqla/simple/admin/post/?page=1">2</a>
</li>




<li>
<a href="/sqla/simple/admin/post/?page=1">&gt;</a>
</li>


<li class="disabled">
<a href="javascript:void(0)">»</a>
</li>

</ul>








<form id="action_form" action="/sqla/simple/admin/post/action/" method="POST" style="display: none">

<input id="action" name="action" type="hidden">
</form>



</div>

Loading

0 comments on commit 97a628c

Please sign in to comment.