tag 基本ok, TODO tag sync

This commit is contained in:
life
2015-02-06 00:28:33 +08:00
parent 4f5dabe839
commit ec4e790449
7 changed files with 339 additions and 50 deletions

89
node_modules/tag.js generated vendored
View File

@@ -1,18 +1,88 @@
var db = require('db');
var Common = require('common');
var User = require('user');
var Note = require('note');
var Web = require('web');
var Tags = db.tags;
/*
TagId
UserId
ServerTagId
Title
NumberNotes
Usn
IsDirty
CreatedTime
UpdatedTime
Usn
Count 笔记数
*/
// 笔记本服务
var Tag = {
// 添加或更新标签
addOrUpdateTag: function(title, callback) {
var userId = User.getCurActiveUserId();
Tags.findOne({UserId: userId, Tag: title}, function(err, tag) {
// 存在, 则更新该tag下的笔记数量
// 已存的, 不更新IsDirty
if(!err && tag) {
Note.countNoteByTag(title, function(cnt) {
tag.Count = cnt;
Tags.update({UserId: userId, Title: title}, {$set: {Count: cnt, UpdatedTime: new Date()}}, function() {
console.log('已存在tag' + title);
callback(tag);
});
});
} else {
var date = new Date();
Tags.insert({
TagId: Common.objectId(),
UserId: userId,
Tag: title,
IsDirty: true, // 新添加的
Count: 1,
LocalIsDelete: false,
CreatedTime: date,
UpdatedTime: date
}, function(err, doc) {
if(err) {
callback && callback({Ok: false, Inserted: false});
} else {
callback && callback(doc);
}
});
}
});
},
getTags: function(callback) {
Tags.find({UserId: User.getCurActiveUserId(), LocalIsDelete: false}, function(err, tags) {
if(err) {
callback && callback(false);
} else {
callback && callback(tags);
}
});
},
// 删除标签, 更新为LocaleIsDelete = true
deleteTag: function(title, callback) {
var me = this;
Tags.update({UserId: User.getCurActiveUserId(), Tag: title}, {$set: {LocalIsDelete: true, UpdatedTime: new Date()}}, function() {
});
// 笔记本
Note.updateNoteToDeleteTag(title, function(updates) {
callback(updates);
});
},
// 更新标签的数量, 在彻底删除笔记时调用
updateTagCount: function(title, count) {
// 更新Tag's Count
Tags.update({UserId: userId, Tag: title}, {$set: {Count: cnt}});
// 更新web
Web.updateTagCount({Tag: title, Count: cnt});
},
/*
// 添加多个标签
addTags: function(titles) {
for(var i in titles) {
@@ -22,7 +92,7 @@ var Tag = {
},
// 添加标签, 先查询是否存在
addTag: function(title, callback) {
userId = User.getCurActiveUserId();
var userId = User.getCurActiveUserId();
Tags.count({UserId: userId, Title: title}, function(err, count) {
if(count) {
callback && callback({Ok: false, IsExists: true});
@@ -56,15 +126,6 @@ var Tag = {
}
});
},
// 得到标签
getTags: function(callback) {
Tags.find({UserId: User.getCurActiveUserId()}, function(err, tags) {
if(err) {
callback && callback(false);
} else {
callback && callback(tags);
}
});
}
*/
};
module.exports = Tag;