-
Notifications
You must be signed in to change notification settings - Fork 0
/
account.go
107 lines (98 loc) · 2.8 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package usecases
import (
"log"
"github.com/braejan/go-transactions-summary/internal/domain/account/entity"
accRepo "github.com/braejan/go-transactions-summary/internal/domain/account/repository"
userRepo "github.com/braejan/go-transactions-summary/internal/domain/user/repository"
"github.com/braejan/go-transactions-summary/internal/valueobject/account"
"github.com/braejan/go-transactions-summary/internal/valueobject/user"
"github.com/google/uuid"
)
// accountUsecases struct implements the AccountUsecases interface.
type accountUsecases struct {
accountRepo accRepo.AccountRepository
userRepo userRepo.UserRepository
}
// NewAccountUseCases returns a new accountUsecases instance.
func NewAccountUseCases(accountRepo accRepo.AccountRepository, usrRepo userRepo.UserRepository) (usecases AccountUseCases, err error) {
if accountRepo == nil {
err = account.ErrAccountRepositoryIsNil
return
}
if usrRepo == nil {
err = user.ErrUserRepositoryIsNil
return
}
usecases = &accountUsecases{
accountRepo: accountRepo,
userRepo: usrRepo,
}
return
}
// Usecases interface implementation:
// GetByID implements the AccountUsecases interface method.
func (u *accountUsecases) GetByID(ID string) (acc entity.Account, err error) {
accID, err := uuid.Parse(ID)
if err != nil {
err = account.ErrProcessingAccountID
return
}
accAux, err := u.accountRepo.GetByID(accID)
if err != nil {
return
}
acc = *accAux
return
}
// GetByUserID implements the AccountUsecases interface method.
func (u *accountUsecases) GetByUserID(userID int64) (acc entity.Account, err error) {
accAux, err := u.accountRepo.GetByUserID(userID)
if err != nil {
return
}
acc = *accAux
return
}
// Create implements the AccountUsecases interface method.
func (u *accountUsecases) Create(userID int64) (err error) {
log.Println("Creating account for user", userID)
// Check if the user exists.
_, err = u.userRepo.GetByID(userID)
if err != nil {
// The user is not created.
err = user.ErrUserNotFound
return
}
// Check if the user already has an account.
acc, err := u.accountRepo.GetByUserID(userID)
if err != nil && err != account.ErrAccountNotFound {
return
}
if acc != nil {
// The user already has an account.
err = account.ErrAccountAlreadyCreated
return
}
// Create the account.
acc = entity.NewAccount(userID)
err = u.accountRepo.Create(acc)
return
}
// Update implements the AccountUsecases interface method.
func (u *accountUsecases) Update(ID string, balance float64, active bool) (err error) {
accID, err := uuid.Parse(ID)
if err != nil {
err = account.ErrProcessingAccountID
return
}
// Check if the account exists.
acc, err := u.accountRepo.GetByID(accID)
if err != nil {
return
}
// Update the account.
acc.Balance = balance
acc.Active = active
err = u.accountRepo.Update(acc)
return
}