/
coin_transactions.go
72 lines (66 loc) · 1.42 KB
/
coin_transactions.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
package datastore
import (
"time"
"github.com/pkg/errors"
"github.com/jmoiron/sqlx"
"github.com/october93/engine/kit/globalid"
"github.com/october93/engine/model"
)
// SaveCard saves a card
func (store *Store) SaveCoinTransaction(m *model.CoinTransaction) error {
return saveCoinTransaction(store, m)
}
// SaveCard saves a card
func (tx *Tx) SaveCoinTransaction(m *model.CoinTransaction) error {
return saveCoinTransaction(tx, m)
}
func saveCoinTransaction(e sqlx.Ext, m *model.CoinTransaction) error {
if m == nil {
return errors.New("provided model can not be nil")
}
if m.ID == globalid.Nil {
m.ID = globalid.Next()
}
tn := time.Now().UTC()
if m.CreatedAt.IsZero() {
m.CreatedAt = tn
}
m.UpdatedAt = tn
_, err := sqlx.NamedExec(e,
`INSERT INTO coin_transactions
(
id,
source_user_id,
recipient_user_id,
card_id,
amount,
type,
created_at,
updated_at
)
VALUES
(
:id,
:source_user_id,
:recipient_user_id,
:card_id,
:amount,
:type,
:created_at,
:updated_at
)
ON CONFLICT(id) DO UPDATE
SET
source_user_id = :source_user_id,
recipient_user_id = :recipient_user_id,
card_id = :card_id,
amount = :amount,
type = :type,
created_at = :created_at,
updated_at = :updated_at
WHERE coin_transactions.id = :id `, m)
if err != nil {
return errors.Wrap(err, "SaveCoinTransaction failed")
}
return nil
}