-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeline.go
62 lines (52 loc) · 1.15 KB
/
timeline.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
package models
import (
"github.com/go-xorm/xorm"
_ "github.com/mattn/go-sqlite3"
"log"
)
type Content struct {
Id int64
Time string
Title string
Details string
}
type Timeline struct {
Title string
Contents []Content
}
func NewTimeline() *Timeline {
return &Timeline{Title: "Timeline"}
}
func (this *Timeline) AppendLine(time, title, detail string) {
line := &Content{Time: time, Title: title, Details: detail}
this.Contents = append(this.Contents, *line)
}
func (this *Timeline) SetTitle(t string) {
this.Title = t
}
func (this *Timeline) SaveDB(db string) {
orm, err := xorm.NewEngine("sqlite3", db)
if err != nil {
log.Fatalln(err)
}
err = orm.Sync(new(Content))
if err != nil {
log.Fatalln(err)
}
session := orm.NewSession()
session.Begin()
session.InsertMulti(this.Contents)
session.Commit()
}
func (this *Timeline) LoadDB(db string, offset, within int) {
orm, err := xorm.NewEngine("sqlite3", db)
if err != nil {
log.Fatalln(err)
}
this.Contents = make([]Content, 0)
if -1 == offset || -1 == within {
orm.Desc("id").Find(&this.Contents)
} else {
orm.Desc("id").Limit(within, offset).Find(&this.Contents)
}
}