/
currency.go
182 lines (147 loc) · 4.01 KB
/
currency.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright 2020 Condensat Tech. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package query
import (
"errors"
"github.com/condensat/bank-core/database"
"github.com/condensat/bank-core/database/model"
"github.com/jinzhu/gorm"
)
const (
FlagCurencyAll = 0
FlagCurencyDisable = 0
FlagCurencyAvailable = 1
)
var (
ErrInvalidCurrencyName = errors.New("Invalid Currency Name")
ErrCurrencyNotFound = errors.New("Currency Not found")
ErrCurrencyNotAvailable = errors.New("Currency Not Available")
ErrCurrencyNotCrypto = errors.New("Currency Not Crypto")
)
// AddOrUpdateCurrency
func AddOrUpdateCurrency(db database.Context, currency model.Currency) (model.Currency, error) {
var result model.Currency
if db == nil {
return result, database.ErrInvalidDatabase
}
gdb := db.DB().(*gorm.DB)
err := gdb.
Where(model.Currency{
Name: currency.Name,
}).
Assign(currency).
FirstOrCreate(&result).Error
return result, err
}
// CurrencyExists
func CurrencyExists(db database.Context, name model.CurrencyName) bool {
entry, err := GetCurrencyByName(db, name)
return err == nil && entry.Name == name
}
// GetCurrencyByName
func GetCurrencyByName(db database.Context, name model.CurrencyName) (model.Currency, error) {
var result model.Currency
list, err := QueryCurrencyList(db, name, FlagCurencyAll)
if len(list) > 0 {
result = list[0]
}
return result, err
}
// CountCurrencies
func CountCurrencies(db database.Context) int {
gdb := db.DB().(*gorm.DB)
if gdb == nil {
return 0
}
var count int
gdb.Model(&model.Currency{}).Count(&count)
return count
}
// ListAllCurrency
func ListAllCurrency(db database.Context) ([]model.Currency, error) {
return QueryCurrencyList(db, "", FlagCurencyAll)
}
// ListAvailableCurrency
func ListAvailableCurrency(db database.Context) ([]model.Currency, error) {
return QueryCurrencyList(db, "", FlagCurencyAvailable)
}
// QueryCurrencyList
func QueryCurrencyList(db database.Context, name model.CurrencyName, available int) ([]model.Currency, error) {
gdb := db.DB().(*gorm.DB)
if db == nil {
return nil, database.ErrInvalidDatabase
}
var filters []func(db *gorm.DB) *gorm.DB
if len(name) > 0 {
filters = append(filters, ScopeCurencyName(name))
}
if available > 0 {
filters = append(filters, ScopeCurencyAvailable(available))
}
var list []*model.Currency
err := gdb.Model(&model.Currency{}).
Scopes(filters...).
Find(&list).Error
if err != nil && err != gorm.ErrRecordNotFound {
return nil, err
}
return convertCurrencyList(list), nil
}
// ScopeCurencyName
func ScopeCurencyName(name model.CurrencyName) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
return db.Where(reqName(), name)
}
}
// ScopeCurencyAvailable
func ScopeCurencyAvailable(available int) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
return db.Where(reqAvailable(), available)
}
}
func convertCurrencyList(list []*model.Currency) []model.Currency {
var result []model.Currency
for _, curr := range list {
if curr == nil {
continue
}
result = append(result, *curr)
}
return result[:]
}
const (
colCurrencyName = "name"
colCurrencyDisplayName = "display_name"
colCurrencyType = "type"
colCurrencyAvailable = "available"
colCurrencyCrypto = "crypto"
colCurrencyPrecision = "precision"
colCurrencyAutoCreate = "auto_create"
)
func currencyColumnNames() []string {
return []string{
colCurrencyName,
colCurrencyDisplayName,
colCurrencyType,
colCurrencyAvailable,
colCurrencyCrypto,
colCurrencyPrecision,
colCurrencyAutoCreate,
}
}
// zero allocation querys string for scope
func reqName() string {
var req [len(colCurrencyName) + len(reqEQ)]byte
off := 0
off += copy(req[off:], colCurrencyName)
copy(req[off:], reqEQ)
return string(req[:])
}
func reqAvailable() string {
var req [len(colCurrencyAvailable) + len(reqGTE)]byte
off := 0
off += copy(req[off:], colCurrencyAvailable)
copy(req[off:], reqGTE)
return string(req[:])
}