diff --git a/public/scripts/controllers/bookmarks-controller.js b/public/scripts/controllers/bookmarks-controller.js index f28f387..38011c5 100644 --- a/public/scripts/controllers/bookmarks-controller.js +++ b/public/scripts/controllers/bookmarks-controller.js @@ -24,6 +24,11 @@ app.controller('bookmarksCtr', ['$scope', '$stateParams', '$filter', '$window', getBookmarks(params); }); + pubSubService.subscribe('EditCtr.inserBookmarsSuccess', $scope, function(event, params) { + console.log('subscribe EditCtr.inserBookmarsSuccess', params); + getBookmarks(params); + }); + function getBookmarks(params) { bookmarkService.getBookmarks(params).then( function(data) { diff --git a/public/scripts/controllers/edit-controller.js b/public/scripts/controllers/edit-controller.js index 617ca40..cb6ed76 100644 --- a/public/scripts/controllers/edit-controller.js +++ b/public/scripts/controllers/edit-controller.js @@ -1,41 +1,63 @@ app.controller('editCtr', ['$scope', '$state', '$timeout', 'bookmarkService', 'pubSubService', function($scope, $state, $timeout, bookmarkService, pubSubService) { + var maxSelections = 3; console.log("Hello , I enter editCtr..."); init(); semanticInit(); $scope.$watch('url', function(newValue, oldValue, scope) { - $scope.urlError = $scope.url == ''; + console.log('url is changed', $('.ui.modal.js-add-bookmark').modal('is active')); + $timeout(function() { + $scope.urlError = $scope.url == '' && $('.ui.modal.js-add-bookmark').modal('is active'); + }); }); $scope.$watch('title', function(newValue, oldValue, scope) { - $scope.titleError = $scope.title == ''; + $timeout(function() { + $scope.titleError = $scope.title == '' && $('.ui.modal.js-add-bookmark').modal('is active'); + }); }); $scope.addTags = function() { console.log('Hello , you have click add tag btn......'); - $scope.newTags = $scope.newTags.replace(/,/g, ",").replace(/,+/g, ","); // 先将中文逗号替换成英文逗号,然后将多个英文逗号换成一个英文逗号 + + // 先将中文逗号替换成英文逗号,然后将多个英文逗号换成一个英文逗号 + $scope.newTags = $scope.newTags.replace(/,/g, ",").replace(/,+/g, ","); var tags = $scope.newTags.split(","); var params = []; tags.forEach(function(tag) { tag = tag.replace(/(^\s*)|(\s*$)/g, '').replace(/\s+/g, ' '); // 去除前后空格,多个空格转为一个空格; - - var find = false; - for (var i = 0; i < $scope.tags.length; i++) { - if ($scope.tags[i].name === tag) { - find = true; - $('.ui.fluid.search.dropdown').dropdown('set selected', $scope.tags[i].id); // 在标签上已有的直接加入标签 - } - }; - if (!find && tag !== '') { - params.push(tag); - } + params.push(tag); }); console.log(params); - + bookmarkService.addTags(params).then( + function(data) { + $scope.tags = data; + console.log(JSON.stringify(data)); + $timeout(function() { + var count = 0; + params.forEach(function(tagName) { + data.forEach(function(tag) { + if (tagName == tag.name) { + console.log(tag.id); + if (count < maxSelections) { + $('.ui.fluid.search.dropdown').dropdown('set selected', tag.id); + } + count++; + } + }); + }); + }); + }, + function(errorMsg) {} + ); } $scope.cancel = function() { console.log('Hello , you have click cancel btn......'); + $('.ui.modal.js-add-bookmark').modal('hide'); + $('.ui.modal.js-add-bookmark .ui.dropdown').dropdown('clear'); + + init(); } $scope.ok = function() { console.log('Hello , you have click ok btn......'); @@ -43,13 +65,26 @@ app.controller('editCtr', ['$scope', '$state', '$timeout', 'bookmarkService', 'p console.log($scope.url, $scope.title, $scope.description, $scope.public, selectedTags); $scope.urlError = $scope.url == ''; $scope.titleError = $scope.title == ''; - $scope.tagsError = (selectedTags.length == 0 || selectedTags.length > 3); + $scope.tagsError = (selectedTags.length == 0 || selectedTags.length > maxSelections); + var params = { + url: $scope.url, + title: $scope.title, + public: '1', + tags: selectedTags, + description: $scope.description + } - bookmarkService.addBookmark({ - a: 'Hello i love this world' - }).then( + bookmarkService.addBookmark(params).then( function(data) { console.log(data); + $('.ui.modal.js-add-bookmark').modal('hide'); + $state.go('bookmarks', { + foo: 'i love you', + bar: 'hello world' + }); + pubSubService.publish('EditCtr.inserBookmarsSuccess', { + show: 'navigate' + }); }, function(errorMsg) { console.log(errorMsg); @@ -59,7 +94,9 @@ app.controller('editCtr', ['$scope', '$state', '$timeout', 'bookmarkService', 'p pubSubService.subscribe('MenuCtr.showAddBookmarkMoadl', $scope, function(event, params) { console.log('subscribe MenuCtr.MenuCtr.showAddBookmarkMoadl', params); - $('.ui.modal.js-add-bookmark').modal('show'); + $('.ui.modal.js-add-bookmark').modal({ + closable: false, + }).modal('show'); $('.ui.modal.js-add-bookmark .ui.dropdown').dropdown('clear'); $('.ui.modal.js-add-bookmark .ui.dropdown').addClass('loading'); init(); @@ -86,11 +123,11 @@ app.controller('editCtr', ['$scope', '$state', '$timeout', 'bookmarkService', 'p setTimeout(() => { $('.ui.dropdown').dropdown({ forceSelection: false, - maxSelections: 3, + maxSelections: maxSelections, onChange: function(value, text, $choice) { var selectedTags = $('.ui.modal.js-add-bookmark .ui.dropdown').dropdown('get value'); $timeout(function() { - $scope.tagsError = (selectedTags.length == 0 || selectedTags.length > 3); + $scope.tagsError = (selectedTags.length == 0 || selectedTags.length > maxSelections) && ($('.ui.modal.js-add-bookmark').modal('is active')); }); } }); diff --git a/public/views/edit.html b/public/views/edit.html index 9e730ac..ffff8f8 100644 --- a/public/views/edit.html +++ b/public/views/edit.html @@ -1,5 +1,5 @@
-
取消
+
取消
发送
diff --git a/routes/api.js b/routes/api.js index a977ed9..a01361d 100644 --- a/routes/api.js +++ b/routes/api.js @@ -1,21 +1,21 @@ var api = require('express').Router(); var mysql = require('mysql'); -var client = mysql.createConnection({ - host: '172.24.13.5', - user: 'root', - password: 'root123', - database: 'mybookmarks', - multipleStatements: true, - port: 3306 -}); // var client = mysql.createConnection({ -// host: '127.0.0.1', -// user: 'lcq', -// password: '123456', +// host: '172.24.13.5', +// user: 'root', +// password: 'root123', // database: 'mybookmarks', // multipleStatements: true, // port: 3306 // }); +var client = mysql.createConnection({ + host: '127.0.0.1', + user: 'lcq', + password: '123456', + database: 'mybookmarks', + multipleStatements: true, + port: 3306 +}); client.connect(); api.get('/bookmarks', function(req, res) { @@ -106,7 +106,7 @@ api.get('/bookmarks', function(req, res) { api.get('/tags', function(req, res) { console.log('hello tags', JSON.stringify(req.query)); var user_id = req.query.user_id; - var sql = "SELECT id, name FROM `tags` WHERE `user_id` = '" + user_id + "'" + var sql = "SELECT id, name FROM `tags` WHERE `user_id` = '" + user_id + "' ORDER BY last_use DESC" client.query(sql, function(error, result, fields) { if (error) { res.json({ @@ -120,16 +120,127 @@ api.get('/tags', function(req, res) { api.post('/addBookmark', function(req, res) { console.log('hello addBookmark', JSON.stringify(req.query), JSON.stringify(req.body)); - res.json({ - a: 'i love this world, too!' + var params = req.body.params; + var user_id = '1'; + var tags = params.tags; + var sql = "INSERT INTO `bookmarks` (`user_id`, `title`, `description`, `url`, `public`, `click_count`) VALUES ('" + user_id + "', '" + params.title + "', '" + params.description + "', '" + params.url + "', '" + params.public + "', '1')"; + console.log(sql); + client.query(sql, function(err, result) { + if (err) throw err; + var insertId = result.insertId; + + sql = "INSERT INTO `tags_bookmarks` (`tag_id`, `bookmark_id`) VALUES"; + for (var i = 0; i < tags.length; i++) { + if (i >= 1) { + sql += ',' + } + sql += "('" + tags[i] + "', '" + insertId + "')"; + } + client.query(sql, function(error, result, fields) { + if (error) { + res.json({ + error: 'error tags' + }); + } else { + sql = "UPDATE tags SET last_use = NOW() WHERE user_id = '" + user_id + "' AND id in ("; + for (var i = 0; i < tags.length; i++) { + if (i >= 1) { + sql += ',' + } + sql += "'" + tags[i] + "'"; + } + sql += ')' + console.log(sql); + client.query(sql, function(error, result1, fields) { + if (error) { + res.json({ + error: 'error tags' + }); + } else { + res.json({ + hello: 'success' + }); + } + }) + + } + }) + + console.log(result.insertId); }); + + // res.json({ + // a: 'i love this world, too!' + // }); }); api.post('/addTags', function(req, res) { console.log('hello addTags', JSON.stringify(req.query), JSON.stringify(req.body)); - res.json({ - a: 'i love this world, too!' - }); + var params = req.body.params; + var user_id = '1'; + var addTagNames = []; + var sql = "SELECT * FROM `tags` WHERE `user_id` = '" + user_id + "' AND `name` in ("; + for (var i = 0; i < params.length; i++) { + if (i >= 1) { + sql += ',' + } + sql += "'" + params[i] + "'"; + }; + sql += ")"; + console.log(sql); + client.query(sql, function(error, result1, fields) { + if (error) { + res.json({ + error: 'error tags' + }); + } else { + params.forEach(function(name) { + var find = false; + result1.forEach(function(tag) { + if (tag.name == name) { + find = true; + } + }) + if (!find) { + addTagNames.push(name); + } + }) + + sql = "INSERT INTO `tags` (`user_id`, `name`) VALUES"; + for (var i = 0; i < addTagNames.length; i++) { + if (i >= 1) { + sql += ',' + } + sql += "('" + user_id + "', '" + addTagNames[i] + "')"; + } + if (addTagNames.length == 0) { + sql = "SELECT id, name FROM `tags` WHERE `user_id` = '" + user_id + "'"; + } + console.log(sql); + client.query(sql, function(error, result, fields) { + if (error) { + res.json({ + error: 'error tags' + }); + } else { + if (addTagNames.length == 0) { + res.json(result); + } else { + sql = "SELECT id, name FROM `tags` WHERE `user_id` = '" + user_id + "' ORDER BY last_use DESC" + client.query(sql, function(error, result, fields) { + if (error) { + res.json({ + error: 'error tags' + }); + } else { + res.json(result); + } + }) + } + } + }) + } + }) }); // client.end(); diff --git a/schema.sql b/schema.sql index a0191a1..4d7e88c 100644 --- a/schema.sql +++ b/schema.sql @@ -34,6 +34,7 @@ CREATE TABLE `tags` ( `id` int(11) NOT NULL AUTO_INCREMENT, -- id `user_id` int(11) NOT NULL, -- 用户id `name` varchar(32) NOT NULL, -- 标签 + `last_use` datetime DEFAULT now(), -- 最后使用标签的时间 PRIMARY KEY (`id`), KEY `userIdIdx` (`user_id`) );