forked from TruthHun/BookStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocument_store.go
More file actions
93 lines (76 loc) · 2.53 KB
/
document_store.go
File metadata and controls
93 lines (76 loc) · 2.53 KB
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
package models
import (
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
"time"
)
var TableDocumentStore = "md_document_store"
// Document Store,文档存储,将大内容分发到专门的数据表里面
type DocumentStore struct {
DocumentId int `orm:"pk;auto;column(document_id)"` //文档id,对应Document中的document_id
Markdown string `orm:"type(text);"` //markdown内容
Content string `orm:"type(text);"` //文本内容
UpdatedAt time.Time `orm:"null"`
}
func NewDocumentStore() *DocumentStore {
return &DocumentStore{}
}
//插入或者更新
func (this *DocumentStore) InsertOrUpdate(ds DocumentStore, fields ...string) (err error) {
o := orm.NewOrm()
var one DocumentStore
// 全部要修改更新时间,除非用fields 参数指定不修改,即"-updated_at"
// 这里要多加 1 秒的时间。因为在项目导入的时候,这个时间跟文档的创建时间是一样的,在内容发布的时候会发布不了。
ds.UpdatedAt = time.Now().Add(1 * time.Second)
o.QueryTable(TableDocumentStore).Filter("document_id", ds.DocumentId).One(&one, "document_id")
if one.DocumentId > 0 {
if len(fields) > 0 {
var updateFields []string
withoutUpdatedAt := false
for _, field := range fields {
if field == "-updated_at" || field == "-UpdatedAt" {
withoutUpdatedAt = true
continue
}
if field == "updated_at" || field == "UpdatedAt" {
continue
}
updateFields = append(updateFields, field)
}
fields = updateFields
if withoutUpdatedAt == false {
fields = append(fields, "updated_at")
}
}
_, err = o.Update(&ds, fields...)
} else {
_, err = o.Insert(&ds)
}
return
}
//查询markdown内容或者content内容
func (this *DocumentStore) GetFiledById(docId interface{}, field string) string {
var ds = DocumentStore{}
if field != "markdown" {
field = "content"
}
orm.NewOrm().QueryTable(TableDocumentStore).Filter("document_id", docId).One(&ds, field)
if field == "content" {
return ds.Content
}
return ds.Markdown
}
//查询markdown内容或者content内容
func (this *DocumentStore) DeleteById(docId ...interface{}) {
if len(docId) > 0 {
orm.NewOrm().QueryTable(TableDocumentStore).Filter("document_id__in", docId...).Delete()
}
}
//查询markdown内容或者content内容
func (this *DocumentStore) GetById(docId interface{}) (ds DocumentStore, err error) {
err = orm.NewOrm().QueryTable(TableDocumentStore).Filter("document_id", docId).One(&ds)
if err != nil {
beego.Error(err)
}
return
}