This repository has been archived by the owner on Nov 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccount.go
92 lines (82 loc) · 2.31 KB
/
account.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
package conveyearthgo
import (
"aletheiaware.com/authgo"
"errors"
"log"
"time"
)
var ErrInsufficientBalance = errors.New("Insufficient Balance")
type AccountDatabase interface {
SelectUser(string) (int64, string, []byte, time.Time, error)
SelectChargesForUser(int64) (int64, error)
SelectYieldsForUser(int64) (int64, error)
SelectPurchasesForUser(int64) (int64, error)
SelectAwardsForUser(int64) (int64, error)
SelectGiftsForUser(int64) (int64, error)
SelectGiftsFromUser(int64) (int64, error)
CreatePurchase(int64, string, string, string, string, int64, int64, time.Time) (int64, error)
}
func AccountBalance(db AccountDatabase, user int64) (int64, error) {
charges, err := db.SelectChargesForUser(user)
if err != nil {
return 0, err
}
yields, err := db.SelectYieldsForUser(user)
if err != nil {
return 0, err
}
purchases, err := db.SelectPurchasesForUser(user)
if err != nil {
return 0, err
}
awards, err := db.SelectAwardsForUser(user)
if err != nil {
return 0, err
}
received, err := db.SelectGiftsForUser(user)
if err != nil {
return 0, err
}
given, err := db.SelectGiftsFromUser(user)
if err != nil {
return 0, err
}
return received + awards + purchases + yields - charges - given, nil
}
type AccountManager interface {
Account(string) (*authgo.Account, error)
AccountBalance(int64) (int64, error)
NewPurchase(int64, string, string, string, string, int64, int64) error
}
func NewAccountManager(db AccountDatabase) AccountManager {
return &accountManager{
database: db,
}
}
type accountManager struct {
database AccountDatabase
}
func (m *accountManager) Account(username string) (*authgo.Account, error) {
id, email, _, created, err := m.database.SelectUser(username)
if err != nil {
return nil, err
}
return &authgo.Account{
ID: id,
Username: username,
Email: email,
Created: created,
}, nil
}
func (m *accountManager) AccountBalance(user int64) (int64, error) {
return AccountBalance(m.database, user)
}
func (m *accountManager) NewPurchase(user int64, sessionID, customerID, paymentIntentID, currency string, amount, size int64) error {
created := time.Now()
purchase, err := m.database.CreatePurchase(user, sessionID, customerID, paymentIntentID, currency, amount, size, created)
if err != nil {
return err
}
log.Println("Created Purchase", purchase)
return nil
}