-
Notifications
You must be signed in to change notification settings - Fork 0
/
repository.go
151 lines (133 loc) · 3.55 KB
/
repository.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package coin
import (
"sync"
"github.com/go-pg/pg"
"github.com/noah-blockchain/coinExplorer-tools/models"
)
type Repository struct {
db *pg.DB
cache *sync.Map
invCache *sync.Map
}
func NewRepository(db *pg.DB) *Repository {
return &Repository{
db: db,
cache: new(sync.Map), //TODO: добавить реализацию очистки
invCache: new(sync.Map), //TODO: добавить реализацию очистки
}
}
// Find coin id by symbol
func (r *Repository) FindIdBySymbol(symbol string) (uint64, error) {
//First look in the cache
id, ok := r.cache.Load(symbol)
if ok {
return id.(uint64), nil
}
coin := new(models.Coin)
err := r.db.Model(coin).
Column("id").
Where("symbol = ?", symbol).
Select()
if err != nil {
return 0, err
}
r.cache.Store(symbol, coin.ID)
return coin.ID, nil
}
// Find coin by symbol
func (r *Repository) FindCoinByID(id uint64) (*models.Coin, error) {
coin := new(models.Coin)
err := r.db.Model(coin).
Column("id", "crr", "volume", "reserve_balance", "name", "price", "delegated", "updated_at", "capitalization").
Where("id = ?", id).
Select()
if err != nil {
return nil, err
}
return coin, nil
}
func (r *Repository) FindSymbolById(id uint64) (string, error) {
//First look in the cache
symbol, ok := r.invCache.Load(id)
if ok {
return symbol.(string), nil
}
coin := &models.Coin{ID: id}
err := r.db.Model(coin).
Where("id = ?", id).
Limit(1).
Select()
if err != nil {
return "", err
}
r.cache.Store(coin.Symbol, id)
r.invCache.Store(id, coin.Symbol)
return coin.Symbol, nil
}
func (r *Repository) Save(c *models.Coin) error {
_, err := r.db.Model(c).
Where("symbol = ?symbol").
OnConflict("DO NOTHING"). //TODO: change to DO UPDATE
SelectOrInsert()
if err != nil {
return err
}
r.cache.Store(c.Symbol, c.ID)
return nil
}
func (r Repository) SaveAllIfNotExist(coins []*models.Coin) error {
_, err := r.db.Model(&coins).OnConflict("(symbol) DO UPDATE").Insert()
if err != nil {
return err
}
for _, coin := range coins {
r.cache.Store(coin.Symbol, coin.ID)
r.invCache.Store(coin.ID, coin.Symbol)
}
return err
}
func (r *Repository) GetAllCoins() ([]*models.Coin, error) {
var coins []*models.Coin
err := r.db.Model(&coins).Order("symbol ASC").Select()
return coins, err
}
func (r Repository) DeleteBySymbol(symbol string) error {
coin := &models.Coin{Symbol: symbol}
_, err := r.db.Model(coin).Where("symbol = ?symbol").Delete()
return err
}
func (r *Repository) UpdateCoinDelegation(id uint64, delegated uint64) error {
coin := models.Coin{Delegated: delegated}
_, err := r.db.Model(&coin).Column("delegated").Where("id = ?", id).Update()
if err != nil {
return err
}
return nil
}
func (r Repository) ResetCoinDelegationNotInListIds(idList []uint64) error {
if len(idList) > 0 {
_, err := r.db.Query(nil, `update coins set delegated=0 where id not in (?) and delegated > 0;`, pg.In(idList))
return err
}
return nil
}
func (r *Repository) UpdateCoinMetaInfo(symbol string, trxId, ownerAddrId uint64) error {
coin := models.Coin{CreationTransactionID: &trxId, CreationAddressID: &ownerAddrId}
_, err := r.db.Model(&coin).
Column("creation_transaction_id", "creation_address_id").
Where("symbol = ?", symbol).Update()
if err != nil {
return err
}
return nil
}
func (r *Repository) SelectCoinsWithBrokenMeta() (*[]models.Coin, error) {
var coins []models.Coin
err := r.db.Model(&coins).Column("symbol").
Where("creation_transaction_id IS NULL").
Where("id > ?", 1).Select()
if err != nil {
return nil, err
}
return &coins, nil
}