Files
leanote/app/service/NoteContentHistoryService.go

66 lines
1.9 KiB
Go
Raw Normal View History

2014-05-07 13:06:24 +08:00
package service
import (
"github.com/leanote/leanote/app/db"
2015-11-13 17:58:41 +08:00
"github.com/leanote/leanote/app/info"
// . "github.com/leanote/leanote/app/lea"
"gopkg.in/mgo.v2/bson"
2015-11-13 17:58:41 +08:00
// "time"
2014-05-07 13:06:24 +08:00
)
// 历史记录
type NoteContentHistoryService struct {
}
// 每个历史记录最大值
var maxSize = 10
// 新建一个note, 不需要添加历史记录
// 添加历史
func (this *NoteContentHistoryService) AddHistory(noteId, userId string, eachHistory info.EachHistory) {
// 检查是否是空
if eachHistory.Content == "" {
return
}
2015-11-13 17:58:41 +08:00
2014-05-07 13:06:24 +08:00
// 先查是否存在历史记录, 没有则添加之
history := info.NoteContentHistory{}
db.GetByIdAndUserId(db.NoteContentHistories, noteId, userId, &history)
if history.NoteId == "" {
this.newHistory(noteId, userId, eachHistory)
} else {
// 判断是否超出 maxSize, 如果超出则pop最后一个, 再push之, 不用那么麻烦, 直接update吧, 虽然影响性能
// TODO
l := len(history.Histories)
if l >= maxSize {
// history.Histories = history.Histories[l-maxSize:] // BUG, 致使都是以前的
history.Histories = history.Histories[:maxSize]
2014-05-07 13:06:24 +08:00
}
newHistory := []info.EachHistory{eachHistory}
newHistory = append(newHistory, history.Histories...) // 在开头加了, 最近的在最前
2015-11-13 17:58:41 +08:00
history.Histories = newHistory
2014-05-07 13:06:24 +08:00
// 更新之
db.UpdateByIdAndUserId(db.NoteContentHistories, noteId, userId, history)
}
return
}
// 新建历史
func (this *NoteContentHistoryService) newHistory(noteId, userId string, eachHistory info.EachHistory) {
2015-11-13 17:58:41 +08:00
history := info.NoteContentHistory{NoteId: bson.ObjectIdHex(noteId),
UserId: bson.ObjectIdHex(userId),
2014-05-07 13:06:24 +08:00
Histories: []info.EachHistory{eachHistory},
}
2015-11-13 17:58:41 +08:00
2014-05-07 13:06:24 +08:00
// 保存之
2015-11-13 17:58:41 +08:00
db.Insert(db.NoteContentHistories, history)
2014-05-07 13:06:24 +08:00
}
// 列表展示
func (this *NoteContentHistoryService) ListHistories(noteId, userId string) []info.EachHistory {
histories := info.NoteContentHistory{}
db.GetByIdAndUserId(db.NoteContentHistories, noteId, userId, &histories)
return histories.Histories
}