MCPcopy Create free account
hub / github.com/TruthHun/BookStack / InsertOrUpdate

Method InsertOrUpdate

models/document_store.go:25–65  ·  view source on GitHub ↗

插入或者更新

(ds DocumentStore, fields ...string)

Source from the content-addressed store, hash-verified

23
24//插入或者更新
25func (this *DocumentStore) InsertOrUpdate(ds DocumentStore, fields ...string) (err error) {
26 o := orm.NewOrm()
27
28 var one DocumentStore
29
30 // 全部要修改更新时间,除非用fields 参数指定不修改,即"-updated_at"
31 // 这里要多加 1 秒的时间。因为在书籍导入的时候,这个时间跟文档的创建时间是一样的,在内容发布的时候会发布不了。
32 ds.UpdatedAt = time.Now().Add(1 * time.Second)
33
34 o.QueryTable(TableDocumentStore).Filter("document_id", ds.DocumentId).One(&one, "document_id")
35
36 if one.DocumentId > 0 {
37
38 if len(fields) > 0 {
39 var updateFields []string
40 withoutUpdatedAt := false
41 for _, field := range fields {
42 if field == "-updated_at" || field == "-UpdatedAt" {
43 withoutUpdatedAt = true
44 continue
45 }
46
47 if field == "updated_at" || field == "UpdatedAt" {
48 continue
49 }
50 updateFields = append(updateFields, field)
51 }
52
53 fields = updateFields
54
55 if withoutUpdatedAt == false {
56 fields = append(fields, "updated_at")
57 }
58 }
59
60 _, err = o.Update(&ds, fields...)
61 } else {
62 _, err = o.Insert(&ds)
63 }
64 return
65}
66
67//查询markdown内容或者content内容
68func (this *DocumentStore) GetFiledById(docId interface{}, field string) string {

Callers

nothing calls this directly

Calls 4

appendFunction · 0.50
AddMethod · 0.45
UpdateMethod · 0.45
InsertMethod · 0.45

Tested by

no test coverage detected