forked from tanitall/memo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memo_poll_vote.go
133 lines (122 loc) · 3.45 KB
/
memo_poll_vote.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
package db
import (
"github.com/jchavannes/btcd/chaincfg/chainhash"
"github.com/jchavannes/jgo/jerr"
"github.com/memocash/memo/app/bitcoin/wallet"
"time"
)
type MemoPollVote struct {
Id uint `gorm:"primary_key"`
TxHash []byte `gorm:"unique;size:50"`
ParentHash []byte
PkHash []byte `gorm:"index:pk_hash"`
PkScript []byte
BlockId uint
Block *Block
OptionTxHash []byte `gorm:"index:option_tx_hash"`
TipAmount int64
TipPkHash []byte
Message string
CreatedAt time.Time
UpdatedAt time.Time
}
func (m MemoPollVote) Save() error {
result := save(&m)
if result.Error != nil {
return jerr.Get("error saving memo poll vote", result.Error)
}
return nil
}
func (m MemoPollVote) GetTransactionHashString() string {
hash, err := chainhash.NewHash(m.TxHash)
if err != nil {
jerr.Get("error getting chainhash from memo poll vote", err).Print()
return ""
}
return hash.String()
}
func (m MemoPollVote) GetAddressString() string {
return m.GetAddress().GetEncoded()
}
func (m MemoPollVote) GetAddress() wallet.Address {
return wallet.GetAddressFromPkHash(m.PkHash)
}
func GetMemoPollVote(txHash []byte) (*MemoPollVote, error) {
var memoPollVote MemoPollVote
err := find(&memoPollVote, MemoPollVote{
TxHash: txHash,
})
if err != nil {
return nil, jerr.Get("error getting memo poll vote", err)
}
return &memoPollVote, nil
}
func GetMemoPollVotesByTxHashes(txHashes [][]byte) ([]*MemoPollVote, error) {
var memoPollVotes []*MemoPollVote
db, err := getDb()
if err != nil {
return nil, jerr.Get("error getting db", err)
}
result := db.
Where("tx_hash IN (?)", txHashes).
Find(&memoPollVotes)
if result.Error != nil {
return nil, jerr.Get("error getting memo poll votes", result.Error)
}
return memoPollVotes, nil
}
func GetVotesForOptions(questionTxHash []byte, single bool) ([]*MemoPollVote, error) {
db, err := getDb()
if err != nil {
return nil, jerr.Get("error getting db", err)
}
var memoPollVotes []*MemoPollVote
if single {
var joinSql = "JOIN (" +
"SELECT MIN(memo_poll_votes.id) AS id " +
"FROM memo_poll_votes " +
"JOIN memo_poll_options ON (memo_poll_votes.option_tx_hash = memo_poll_options.tx_hash) " +
"WHERE memo_poll_options.poll_tx_hash = ? " +
"GROUP BY memo_poll_votes.pk_hash) AS uids ON (memo_poll_votes.id = uids.id)"
db = db.Joins(joinSql, questionTxHash)
} else {
var joinSql = "JOIN (" +
"SELECT memo_poll_votes.id AS id " +
"FROM memo_poll_votes " +
"JOIN memo_poll_options ON (memo_poll_votes.option_tx_hash = memo_poll_options.tx_hash) " +
"WHERE memo_poll_options.poll_tx_hash = ? " +
") AS uids ON (memo_poll_votes.id = uids.id)"
db = db.Joins(joinSql, questionTxHash)
}
result := db.
Order("created_at DESC").
Find(&memoPollVotes)
if result.Error != nil {
return nil, jerr.Get("error getting memo poll votes", result.Error)
}
return memoPollVotes, nil
}
func GetCountMemoPollVote() (uint, error) {
cnt, err := count(&MemoPollVote{})
if err != nil {
return 0, jerr.Get("error getting total count", err)
}
return cnt, nil
}
func GetPollVotes(offset uint) ([]*MemoPollVote, error) {
db, err := getDb()
if err != nil {
return nil, jerr.Get("error getting db", err)
}
var memoPollVotes []*MemoPollVote
result := db.
Preload(BlockTable).
Limit(25).
Offset(offset).
Order("id ASC").
Find(&memoPollVotes)
if result.Error != nil {
return nil, jerr.Get("error running query", result.Error)
}
return memoPollVotes, nil
}