/
keys.go
98 lines (86 loc) · 2.81 KB
/
keys.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
// Copyright (C) 2017, 2018, 2019 EGAAS S.A.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or (at
// your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
package model
import (
"fmt"
"github.com/AplaProject/go-apla/packages/converter"
)
// Key is model
type Key struct {
ecosystem int64 `json:"ecosystem"`
accountKeyID int64 `gorm:"-" json:"-"`
ID int64 `gorm:"primary_key;not null" json:"id"`
Ecosystem int64 `gorm:"column:ecosystem;not null" json:"ecosystem"`
AccountID string `gorm:"column:account;not null" json:"account"`
PublicKey []byte `gorm:"column:pub;not null" json:"pub"`
Amount string `gorm:"not null" json:"amount"`
Deposit string `gorm:"not null" json:"deposit"`
Maxpay string `gorm:"not null" json:"maxpay"`
Multi int64 `gorm:"not null" json:"multi"`
Deleted int64 `gorm:"not null" json:"deleted"`
Blocked int64 `gorm:"not null" json:"blocked"`
}
// SetTablePrefix is setting table prefix
func (m *Key) SetTablePrefix(prefix int64) *Key {
m.ecosystem = prefix
return m
}
// TableName returns name of table
func (m Key) TableName() string {
if m.ecosystem == 0 {
m.ecosystem = 1
}
return `1_keys`
}
// Get is retrieving model from database
func (m *Key) Get(wallet int64) (bool, error) {
return isFound(DBConn.Where("id = ? and ecosystem = ?", wallet, m.ecosystem).First(m))
}
func (m *Key) AccountKeyID() int64 {
if m.accountKeyID == 0 {
m.accountKeyID = converter.StringToAddress(m.AccountID)
}
return m.accountKeyID
}
// KeyTableName returns name of key table
func KeyTableName(prefix int64) string {
return fmt.Sprintf("%d_keys", prefix)
}
// GetKeysCount returns common count of keys
func GetKeysCount() (int64, error) {
var cnt int64
row := DBConn.Raw(`SELECT count(*) key_count FROM "1_keys" WHERE ecosystem = 1`).Select("key_count").Row()
err := row.Scan(&cnt)
return cnt, err
}
// GetKeys returns a list of keys records
func GetKeys(prefix int64, order string, limit int64, offset int64) ([]Key) {
if prefix == 0 {
prefix = 1
}
if order == "" {
order = "id ASC"
}
if limit == 0 {
limit = -1
}
if offset == 0 {
offset = -1
}
var keys []Key
DBConn.Table(KeyTableName(prefix)).Order(order).Limit(limit).Offset(offset).Find(&keys)
return keys
}