-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
53 lines (44 loc) · 1.18 KB
/
service.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
package update_account
import (
"context"
"github.com/devdammit/shekel/cmd/unit/internal/entities"
port "github.com/devdammit/shekel/cmd/unit/internal/ports/use-cases"
"github.com/devdammit/shekel/pkg/currency"
)
type Repository interface {
GetByID(ctx context.Context, accountID uint64) (*entities.Account, error)
Update(ctx context.Context, account *entities.Account) (*entities.Account, error)
}
type UpdateAccountUseCase struct {
repo Repository
}
func NewUseCase(repo Repository) *UpdateAccountUseCase {
return &UpdateAccountUseCase{
repo: repo,
}
}
// Execute
// Можно обновить только имя, описание и баланс.
func (u *UpdateAccountUseCase) Execute(
ctx context.Context,
ID uint64,
name string,
description *string,
balance currency.Amount,
) (bool, error) {
account, err := u.repo.GetByID(ctx, ID)
if err != nil {
return false, err
}
if account.Balance.CurrencyCode != balance.CurrencyCode {
return false, port.ErrorCannotUpdateCurrencyInAccount
}
account.Name = name
account.Description = description
account.Balance = balance
_, err = u.repo.Update(ctx, account)
if err != nil {
return false, err
}
return true, nil
}