-
Notifications
You must be signed in to change notification settings - Fork 242
/
persistence_profile_showcase.go
122 lines (99 loc) · 3.02 KB
/
persistence_profile_showcase.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package protocol
import (
"context"
"database/sql"
)
type ProfileShowcaseEntryType int
const (
ProfileShowcaseEntryTypeCommunity ProfileShowcaseEntryType = iota
ProfileShowcaseEntryTypeAccount
ProfileShowcaseEntryTypeCollectible
ProfileShowcaseEntryTypeAsset
)
type ProfileShowcaseVisibility int
const (
ProfileShowcaseVisibilityNoOne ProfileShowcaseVisibility = iota
ProfileShowcaseVisibilityIDVerifiedContacts
ProfileShowcaseVisibilityContacts
ProfileShowcaseVisibilityEveryone
)
const insertOrUpdateProfilePreferencesQuery = "INSERT OR REPLACE INTO profile_showcase_preferences(id, entry_type, visibility, sort_order) VALUES (?, ?, ?, ?)"
const selectAllProfilePreferencesQuery = "SELECT id, entry_type, visibility, sort_order FROM profile_showcase_preferences"
const selectProfilePreferencesByTypeQuery = "SELECT id, entry_type, visibility, sort_order FROM profile_showcase_preferences WHERE entry_type = ?"
type ProfileShowcaseEntry struct {
ID string `json:"id"`
EntryType ProfileShowcaseEntryType `json:"entryType"`
ShowcaseVisibility ProfileShowcaseVisibility `json:"showcaseVisibility"`
Order int `json:"order"`
}
func (db sqlitePersistence) InsertOrUpdateProfileShowcasePreference(entry *ProfileShowcaseEntry) error {
_, err := db.db.Exec(insertOrUpdateProfilePreferencesQuery,
entry.ID,
entry.EntryType,
entry.ShowcaseVisibility,
entry.Order,
)
if err != nil {
return err
}
return nil
}
func (db sqlitePersistence) SaveProfileShowcasePreferences(entries []*ProfileShowcaseEntry) error {
tx, err := db.db.BeginTx(context.Background(), &sql.TxOptions{})
if err != nil {
return err
}
defer func() {
if err == nil {
err = tx.Commit()
return
}
// don't shadow original error
_ = tx.Rollback()
}()
for _, entry := range entries {
_, err = tx.Exec(insertOrUpdateProfilePreferencesQuery,
entry.ID,
entry.EntryType,
entry.ShowcaseVisibility,
entry.Order,
)
if err != nil {
return err
}
}
return nil
}
func (db sqlitePersistence) parseProfileShowcasePreferencesRows(rows *sql.Rows) ([]*ProfileShowcaseEntry, error) {
entries := []*ProfileShowcaseEntry{}
for rows.Next() {
entry := &ProfileShowcaseEntry{}
err := rows.Scan(
&entry.ID,
&entry.EntryType,
&entry.ShowcaseVisibility,
&entry.Order,
)
if err != nil {
return nil, err
}
entries = append(entries, entry)
}
return entries, nil
}
func (db sqlitePersistence) GetAllProfileShowcasePreferences() ([]*ProfileShowcaseEntry, error) {
rows, err := db.db.Query(selectAllProfilePreferencesQuery)
if err != nil {
return nil, err
}
defer rows.Close()
return db.parseProfileShowcasePreferencesRows(rows)
}
func (db sqlitePersistence) GetProfileShowcasePreferencesByType(entryType ProfileShowcaseEntryType) ([]*ProfileShowcaseEntry, error) {
rows, err := db.db.Query(selectProfilePreferencesByTypeQuery, entryType)
if err != nil {
return nil, err
}
defer rows.Close()
return db.parseProfileShowcasePreferencesRows(rows)
}