github.com/condensat/bank-core@v0.1.0/database/query/currency.go (about) 1 // Copyright 2020 Condensat Tech. All rights reserved. 2 // Use of this source code is governed by a MIT 3 // license that can be found in the LICENSE file. 4 5 package query 6 7 import ( 8 "errors" 9 10 "github.com/condensat/bank-core/database" 11 "github.com/condensat/bank-core/database/model" 12 13 "github.com/jinzhu/gorm" 14 ) 15 16 const ( 17 FlagCurencyAll = 0 18 19 FlagCurencyDisable = 0 20 FlagCurencyAvailable = 1 21 ) 22 23 var ( 24 ErrInvalidCurrencyName = errors.New("Invalid Currency Name") 25 ErrCurrencyNotFound = errors.New("Currency Not found") 26 ErrCurrencyNotAvailable = errors.New("Currency Not Available") 27 ErrCurrencyNotCrypto = errors.New("Currency Not Crypto") 28 ) 29 30 // AddOrUpdateCurrency 31 func AddOrUpdateCurrency(db database.Context, currency model.Currency) (model.Currency, error) { 32 var result model.Currency 33 if db == nil { 34 return result, database.ErrInvalidDatabase 35 } 36 37 gdb := db.DB().(*gorm.DB) 38 39 err := gdb. 40 Where(model.Currency{ 41 Name: currency.Name, 42 }). 43 Assign(currency). 44 FirstOrCreate(&result).Error 45 46 return result, err 47 } 48 49 // CurrencyExists 50 func CurrencyExists(db database.Context, name model.CurrencyName) bool { 51 entry, err := GetCurrencyByName(db, name) 52 53 return err == nil && entry.Name == name 54 } 55 56 // GetCurrencyByName 57 func GetCurrencyByName(db database.Context, name model.CurrencyName) (model.Currency, error) { 58 var result model.Currency 59 60 list, err := QueryCurrencyList(db, name, FlagCurencyAll) 61 if len(list) > 0 { 62 result = list[0] 63 } 64 65 return result, err 66 } 67 68 // CountCurrencies 69 func CountCurrencies(db database.Context) int { 70 gdb := db.DB().(*gorm.DB) 71 if gdb == nil { 72 return 0 73 } 74 75 var count int 76 gdb.Model(&model.Currency{}).Count(&count) 77 return count 78 } 79 80 // ListAllCurrency 81 func ListAllCurrency(db database.Context) ([]model.Currency, error) { 82 return QueryCurrencyList(db, "", FlagCurencyAll) 83 } 84 85 // ListAvailableCurrency 86 func ListAvailableCurrency(db database.Context) ([]model.Currency, error) { 87 return QueryCurrencyList(db, "", FlagCurencyAvailable) 88 } 89 90 // QueryCurrencyList 91 func QueryCurrencyList(db database.Context, name model.CurrencyName, available int) ([]model.Currency, error) { 92 gdb := db.DB().(*gorm.DB) 93 if db == nil { 94 return nil, database.ErrInvalidDatabase 95 } 96 97 var filters []func(db *gorm.DB) *gorm.DB 98 if len(name) > 0 { 99 filters = append(filters, ScopeCurencyName(name)) 100 } 101 if available > 0 { 102 filters = append(filters, ScopeCurencyAvailable(available)) 103 } 104 105 var list []*model.Currency 106 err := gdb.Model(&model.Currency{}). 107 Scopes(filters...). 108 Find(&list).Error 109 110 if err != nil && err != gorm.ErrRecordNotFound { 111 return nil, err 112 } 113 114 return convertCurrencyList(list), nil 115 } 116 117 // ScopeCurencyName 118 func ScopeCurencyName(name model.CurrencyName) func(db *gorm.DB) *gorm.DB { 119 return func(db *gorm.DB) *gorm.DB { 120 return db.Where(reqName(), name) 121 } 122 } 123 124 // ScopeCurencyAvailable 125 func ScopeCurencyAvailable(available int) func(db *gorm.DB) *gorm.DB { 126 return func(db *gorm.DB) *gorm.DB { 127 return db.Where(reqAvailable(), available) 128 } 129 } 130 131 func convertCurrencyList(list []*model.Currency) []model.Currency { 132 var result []model.Currency 133 for _, curr := range list { 134 if curr == nil { 135 continue 136 } 137 result = append(result, *curr) 138 } 139 140 return result[:] 141 } 142 143 const ( 144 colCurrencyName = "name" 145 colCurrencyDisplayName = "display_name" 146 colCurrencyType = "type" 147 colCurrencyAvailable = "available" 148 colCurrencyCrypto = "crypto" 149 colCurrencyPrecision = "precision" 150 colCurrencyAutoCreate = "auto_create" 151 ) 152 153 func currencyColumnNames() []string { 154 return []string{ 155 colCurrencyName, 156 colCurrencyDisplayName, 157 colCurrencyType, 158 colCurrencyAvailable, 159 colCurrencyCrypto, 160 colCurrencyPrecision, 161 colCurrencyAutoCreate, 162 } 163 } 164 165 // zero allocation querys string for scope 166 func reqName() string { 167 var req [len(colCurrencyName) + len(reqEQ)]byte 168 off := 0 169 off += copy(req[off:], colCurrencyName) 170 copy(req[off:], reqEQ) 171 172 return string(req[:]) 173 } 174 175 func reqAvailable() string { 176 var req [len(colCurrencyAvailable) + len(reqGTE)]byte 177 off := 0 178 off += copy(req[off:], colCurrencyAvailable) 179 copy(req[off:], reqGTE) 180 181 return string(req[:]) 182 }