-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
98 lines (79 loc) · 1.88 KB
/
store.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
package sqlite
import (
"database/sql"
"strings"
"time"
"github.com/rot1024/garoo/garoo"
"github.com/samber/lo"
_ "embed"
_ "github.com/mattn/go-sqlite3"
)
type Store struct {
dataSource string
}
var _ garoo.Store = (*Store)(nil)
//go:embed schemas.sql
var initSQL string
func New(dataSource string) (*Store, error) {
s := &Store{dataSource: dataSource}
if err := s.openAndInitDB(); err != nil {
return nil, err
}
return s, nil
}
func (s *Store) Name() string {
return "sqlite"
}
func (s *Store) Save(post *garoo.Post) error {
return s.open(func(db *sql.DB) (err error) {
return s.upsertPost(db, post)
})
}
func (s *Store) Login(code string) (string, error) {
return "", nil
}
func (s *Store) upsertPost(db *sql.DB, post *garoo.Post) error {
_, err := db.Exec(
"REPLACE INTO pictures (id, user_name, user_screenname, user_id, description, provider, url, created_at, category, label, count, media_url, user_avatar_url) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13)",
post.ID,
post.Author.Name,
post.Author.ScreenName,
post.Author.ID,
post.Content,
post.Provider,
post.URL,
formatTime(post.Timestamp),
post.Category,
strings.Join(post.Tags, " "),
len(post.Media),
strings.Join(lo.Map(post.Media, func(m garoo.Media, _ int) string { return m.URL }), ","),
post.Author.Avator,
)
return err
}
func (s *Store) initDB(db *sql.DB) error {
_, err := db.Exec(initSQL)
return err
}
func (s *Store) openAndInitDB() error {
return s.open(func(db *sql.DB) error {
return s.initDB(db)
})
}
func (s *Store) open(f func(db *sql.DB) error) error {
db, err := sql.Open("sqlite3", s.dataSource)
if err != nil {
return err
}
defer db.Close()
return f(db)
}
func formatTime(t time.Time) string {
return t.Format("2006-01-02 15:04:05")
}
func (s *Store) GetConfig() string {
return ""
}
func (s *Store) Init(c string) error {
return nil
}