forked from leanote/leanote
/
TagService.go
137 lines (122 loc) · 3.78 KB
/
TagService.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package service
import (
"github.com/JacobXie/leanote/app/db"
"github.com/JacobXie/leanote/app/info"
// . "github.com/JacobXie/leanote/app/lea"
"gopkg.in/mgo.v2/bson"
"time"
)
/*
每添加,更新note时, 都将tag添加到tags表中
*/
type TagService struct {
}
/*
func (this *TagService) GetTags(userId string) []string {
tag := info.Tag{}
db.Get(db.Tags, userId, &tag)
LogJ(tag)
return tag.Tags
}
*/
func (this *TagService) AddTagsI(userId string, tags interface{}) bool {
if ts, ok2 := tags.([]string); ok2 {
return this.AddTags(userId, ts)
}
return false
}
func (this *TagService) AddTags(userId string, tags []string) bool {
for _, tag := range tags {
if !db.Upsert(db.Tags,
bson.M{"_id": bson.ObjectIdHex(userId)},
bson.M{"$addToSet": bson.M{"Tags": tag}}) {
return false
}
}
return true
}
//---------------------------
// v2
// 第二版标签, 单独一张表, 每一个tag一条记录
// 添加或更新标签, 先查下是否存在, 不存在则添加, 存在则更新
// 都要统计下tag的note数
// 什么时候调用? 笔记添加Tag, 删除Tag时
// 删除note时, 都可以调用
// 万能
func (this *TagService) AddOrUpdateTag(userId string, tag string) info.NoteTag {
userIdO := bson.ObjectIdHex(userId)
noteTag := info.NoteTag{}
db.GetByQ(db.NoteTags, bson.M{"UserId": userIdO, "Tag": tag}, ¬eTag)
// 存在, 则更新之
if noteTag.TagId != "" {
// 统计note数
count := noteService.CountNoteByTag(userId, tag)
noteTag.Count = count
noteTag.UpdatedTime = time.Now()
// noteTag.Usn = userService.IncrUsn(userId), 更新count而已
db.UpdateByIdAndUserId(db.NoteTags, noteTag.TagId.Hex(), userId, noteTag)
return noteTag
}
// 不存在, 则创建之
noteTag.TagId = bson.NewObjectId()
noteTag.Count = 1
noteTag.Tag = tag
noteTag.UserId = bson.ObjectIdHex(userId)
noteTag.CreatedTime = time.Now()
noteTag.UpdatedTime = noteTag.CreatedTime
noteTag.Usn = userService.IncrUsn(userId)
noteTag.IsDeleted = false
db.Insert(db.NoteTags, noteTag)
return noteTag
}
// 得到标签, 按更新时间来排序
func (this *TagService) GetTags(userId string) []info.NoteTag {
tags := []info.NoteTag{}
query := bson.M{"UserId": bson.ObjectIdHex(userId), "IsDeleted": false}
q := db.NoteTags.Find(query)
sortFieldR := "-UpdatedTime"
q.Sort(sortFieldR).All(&tags)
return tags
}
// 删除标签
// 也删除所有的笔记含该标签的
// 返回noteId => usn
func (this *TagService) DeleteTag(userId string, tag string) map[string]int {
usn := userService.IncrUsn(userId)
if db.UpdateByQMap(db.NoteTags, bson.M{"UserId": bson.ObjectIdHex(userId), "Tag": tag}, bson.M{"Usn": usn, "IsDeleted": true}) {
return noteService.UpdateNoteToDeleteTag(userId, tag)
}
return map[string]int{}
}
// 删除标签, 供API调用
func (this *TagService) DeleteTagApi(userId string, tag string, usn int) (ok bool, msg string, toUsn int) {
noteTag := info.NoteTag{}
db.GetByQ(db.NoteTags, bson.M{"UserId": bson.ObjectIdHex(userId), "Tag": tag}, ¬eTag)
if noteTag.TagId == "" {
return false, "notExists", 0
}
if noteTag.Usn > usn {
return false, "conflict", 0
}
toUsn = userService.IncrUsn(userId)
if db.UpdateByQMap(db.NoteTags, bson.M{"UserId": bson.ObjectIdHex(userId), "Tag": tag}, bson.M{"Usn": usn, "IsDeleted": true}) {
return true, "", toUsn
}
return false, "", 0
}
// 重新统计标签的count
func (this *TagService) reCountTagCount(userId string, tags []string) {
if tags == nil {
return
}
for _, tag := range tags {
this.AddOrUpdateTag(userId, tag)
}
}
// 同步用
func (this *TagService) GeSyncTags(userId string, afterUsn, maxEntry int) []info.NoteTag {
noteTags := []info.NoteTag{}
q := db.NoteTags.Find(bson.M{"UserId": bson.ObjectIdHex(userId), "Usn": bson.M{"$gt": afterUsn}})
q.Sort("Usn").Limit(maxEntry).All(¬eTags)
return noteTags
}