/
account.go
45 lines (36 loc) · 941 Bytes
/
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
package gledger
import "github.com/twinj/uuid"
type Account struct {
UUID, Name, Type string
Balance int64
Active bool
}
type AccountService interface {
Create(Account) (Account, error)
All() ([]Account, error)
Read(string) (Account, error)
}
func NewAccountService(
s func(Account) error,
a func() ([]Account, error),
r func(string) (Account, error),
) accountService {
return accountService{saveAccount: s, allAccounts: a, readAccount: r}
}
type accountService struct {
saveAccount func(Account) error
allAccounts func() ([]Account, error)
readAccount func(string) (Account, error)
}
func (as accountService) Create(a Account) (Account, error) {
if a.UUID == "" {
a.UUID = uuid.NewV4().String()
}
return a, as.saveAccount(a)
}
func (as accountService) All() ([]Account, error) {
return as.allAccounts()
}
func (as accountService) Read(u string) (Account, error) {
return as.readAccount(u)
}