@@ -2,6 +2,7 @@ package accounts
2
2
3
3
import (
4
4
"context"
5
+ "github.com/cockroachdb/errors"
5
6
accountsv1 "github.com/ft-t/go-money-pb/gen/gomoneypb/accounts/v1"
6
7
"github.com/ft-t/go-money/pkg/database"
7
8
"github.com/shopspring/decimal"
@@ -15,8 +16,44 @@ type Service struct {
15
16
}
16
17
17
18
func (s * Service ) List (ctx context.Context , req * accountsv1.ListAccountsRequest ) (* accountsv1.ListAccountsResponse , error ) {
18
- //TODO implement me
19
- panic ("implement me" )
19
+ var accounts []database.Account
20
+
21
+ db := database .GetDbWithContext (ctx , database .DbTypeReadonly )
22
+
23
+ if err := db .Unscoped ().Order ("position desc" ).Find (& accounts ).Error ; err != nil {
24
+ return nil , err
25
+ }
26
+
27
+ var mapped []* accountsv1.ListAccountsResponse_AccountItem
28
+
29
+ for _ , account := range accounts {
30
+ mapped = append (mapped ,
31
+ & accountsv1.ListAccountsResponse_AccountItem {
32
+ Account : s .cfg .MapperSvc .MapAccount (ctx , & account ),
33
+ })
34
+ }
35
+
36
+ return & accountsv1.ListAccountsResponse {
37
+ Accounts : mapped ,
38
+ }, nil
39
+ }
40
+
41
+ func (s * Service ) Delete (ctx context.Context , req * accountsv1.DeleteAccountRequest ) (* accountsv1.DeleteAccountResponse , error ) {
42
+ var account database.Account
43
+
44
+ db := database .GetDbWithContext (ctx , database .DbTypeMaster )
45
+
46
+ if err := db .Where ("id = ?" , req .Id ).First (& account ).Error ; err != nil {
47
+ return nil , errors .Join (err , errors .New ("account not found" ))
48
+ }
49
+
50
+ if err := db .Delete (& account ).Error ; err != nil {
51
+ return nil , err
52
+ }
53
+
54
+ return & accountsv1.DeleteAccountResponse {
55
+ Account : s .cfg .MapperSvc .MapAccount (ctx , & account ),
56
+ }, nil
20
57
}
21
58
22
59
type ServiceConfig struct {
@@ -36,28 +73,27 @@ func (s *Service) Create(
36
73
req * accountsv1.CreateAccountRequest ,
37
74
) (* accountsv1.CreateAccountResponse , error ) {
38
75
account := & database.Account {
39
- Name : req .Account .Name ,
40
- Currency : req .Account .Currency ,
41
- CurrentBalance : decimal.Decimal {},
42
- Extra : req .Account .Extra ,
43
- Flags : 0 ,
44
- LastUpdatedAt : time .Now ().UTC (),
45
- CreatedAt : time .Now ().UTC (),
46
- DeletedAt : gorm.DeletedAt {},
47
- Type : req .Account .Type ,
48
- Note : req .Account .Note ,
76
+ Name : req .Name ,
77
+ Currency : req .Currency ,
78
+ Extra : req .Extra ,
79
+ Flags : 0 ,
80
+ LastUpdatedAt : time .Now ().UTC (),
81
+ CreatedAt : time .Now ().UTC (),
82
+ DeletedAt : gorm.DeletedAt {},
83
+ Type : req .Type ,
84
+ Note : req .Note ,
85
+ Iban : req .Iban ,
86
+ AccountNumber : req .AccountNumber ,
49
87
}
50
88
51
- if req .Account .CurrencyBalance != "" {
52
- cb , err := decimal .NewFromString (req .Account .CurrencyBalance )
53
- if err != nil {
54
- return nil , err
55
- }
56
-
57
- account .CurrentBalance = cb
89
+ liabilityPercent , err := s .parseLiabilityPercent (req .LiabilityPercent )
90
+ if err != nil {
91
+ return nil , err
58
92
}
59
93
60
- if err := database .GetDbWithContext (ctx , database .DbTypeMaster ).Create (account ).Error ; err != nil {
94
+ account .LiabilityPercent = liabilityPercent
95
+
96
+ if err = database .GetDbWithContext (ctx , database .DbTypeMaster ).Create (account ).Error ; err != nil {
61
97
return nil , err
62
98
}
63
99
@@ -66,6 +102,19 @@ func (s *Service) Create(
66
102
}, nil
67
103
}
68
104
105
+ func (s * Service ) parseLiabilityPercent (input * string ) (decimal.NullDecimal , error ) {
106
+ if input == nil {
107
+ return decimal.NullDecimal {}, nil
108
+ }
109
+
110
+ parsed , err := decimal .NewFromString (* input )
111
+ if err != nil {
112
+ return decimal.NullDecimal {}, errors .Join (err , errors .New ("failed to parse liability percent" ))
113
+ }
114
+
115
+ return decimal .NewNullDecimal (parsed ), nil
116
+ }
117
+
69
118
func (s * Service ) Update (
70
119
ctx context.Context ,
71
120
req * accountsv1.UpdateAccountRequest ,
@@ -86,16 +135,25 @@ func (s *Service) Update(
86
135
account .Extra = req .Extra
87
136
account .LastUpdatedAt = time .Now ().UTC ()
88
137
account .Note = req .Note
138
+ account .AccountNumber = req .AccountNumber
139
+ account .Iban = req .Iban
140
+
141
+ liabilityPercent , err := s .parseLiabilityPercent (req .LiabilityPercent )
142
+ if err != nil {
143
+ return nil , err
144
+ }
145
+
146
+ account .LiabilityPercent = liabilityPercent
89
147
90
148
if account .Extra == nil {
91
149
account .Extra = map [string ]string {}
92
150
}
93
151
94
- if err : = tx .Save (& account ).Error ; err != nil {
152
+ if err = tx .Save (& account ).Error ; err != nil {
95
153
return nil , err
96
154
}
97
155
98
- if err : = tx .Commit ().Error ; err != nil {
156
+ if err = tx .Commit ().Error ; err != nil {
99
157
return nil , err
100
158
}
101
159
0 commit comments