Skip to content

Commit 1eaa05e

Browse files
authored
feat: improve accounts (#3)
* feat: improve accounts * feat: account list & delete
1 parent 46be9e5 commit 1eaa05e

File tree

13 files changed

+372
-62
lines changed

13 files changed

+372
-62
lines changed

Makefile

+5-1
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@ lint-dev:
1010
generate:
1111
go generate ./...
1212

13+
.PHONY: update
14+
update: update-pb
15+
16+
1317
.PHONY: update-pb
1418
update-pb:
15-
go get github.com/ft-t/go-money-pb@master
19+
GOPRIVATE=github.com/ft-t/* go get github.com/ft-t/go-money-pb@master
1620
go mod tidy
1721

1822
.PHONY: test

cmd/server/accounts.go

+11
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ type AccountsApi struct {
1212
accSvc AccountSvc
1313
}
1414

15+
func (a *AccountsApi) DeleteAccount(ctx context.Context, c *connect.Request[accountsv1.DeleteAccountRequest]) (*connect.Response[accountsv1.DeleteAccountResponse], error) {
16+
// todo auth
17+
18+
resp, err := a.accSvc.Delete(ctx, c.Msg)
19+
if err != nil {
20+
return nil, err
21+
}
22+
23+
return connect.NewResponse(resp), nil
24+
}
25+
1526
func (a *AccountsApi) CreateAccount(
1627
ctx context.Context,
1728
c *connect.Request[accountsv1.CreateAccountRequest],

cmd/server/interfaces.go

+5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ type AccountSvc interface {
3434
ctx context.Context,
3535
req *accountsv1.ListAccountsRequest,
3636
) (*accountsv1.ListAccountsResponse, error)
37+
38+
Delete(
39+
ctx context.Context,
40+
req *accountsv1.DeleteAccountRequest,
41+
) (*accountsv1.DeleteAccountResponse, error)
3742
}
3843

3944
type ConfigSvc interface {

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
connectrpc.com/grpcreflect v1.3.0
88
github.com/cockroachdb/errors v1.11.3
99
github.com/dgrijalva/jwt-go v3.2.0+incompatible
10-
github.com/ft-t/go-money-pb v0.0.0-20250223204332-c94ffb0ded76
10+
github.com/ft-t/go-money-pb v0.0.0-20250225123647-d3dc09777fc4
1111
github.com/go-gormigrate/gormigrate/v2 v2.1.3
1212
github.com/golang-jwt/jwt/v5 v5.2.1
1313
github.com/golang/mock v1.6.0

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
1515
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1616
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
1717
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
18-
github.com/ft-t/go-money-pb v0.0.0-20250223204332-c94ffb0ded76 h1:RA9M/xgDWq4p7lf800g9G/36kzz53A1h4jVkxOtJkIg=
19-
github.com/ft-t/go-money-pb v0.0.0-20250223204332-c94ffb0ded76/go.mod h1:PI8ulxDJ0HamtFdQhvfOTJDla0blE6tJX9EuMw1IrzU=
18+
github.com/ft-t/go-money-pb v0.0.0-20250225123647-d3dc09777fc4 h1:lXPFhyf3BhGEOFywOYyNEhQoEJf063IfvtKoRdeUOjs=
19+
github.com/ft-t/go-money-pb v0.0.0-20250225123647-d3dc09777fc4/go.mod h1:PI8ulxDJ0HamtFdQhvfOTJDla0blE6tJX9EuMw1IrzU=
2020
github.com/getsentry/sentry-go v0.27.0 h1:Pv98CIbtB3LkMWmXi4Joa5OOcwbmnX88sF5qbK3r3Ps=
2121
github.com/getsentry/sentry-go v0.27.0/go.mod h1:lc76E2QywIyW8WuBnwl8Lc4bkmQH4+w1gwTf25trprY=
2222
github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxIA=

pkg/accounts/service.go

+80-22
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package accounts
22

33
import (
44
"context"
5+
"github.com/cockroachdb/errors"
56
accountsv1 "github.com/ft-t/go-money-pb/gen/gomoneypb/accounts/v1"
67
"github.com/ft-t/go-money/pkg/database"
78
"github.com/shopspring/decimal"
@@ -15,8 +16,44 @@ type Service struct {
1516
}
1617

1718
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
2057
}
2158

2259
type ServiceConfig struct {
@@ -36,28 +73,27 @@ func (s *Service) Create(
3673
req *accountsv1.CreateAccountRequest,
3774
) (*accountsv1.CreateAccountResponse, error) {
3875
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,
4987
}
5088

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
5892
}
5993

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 {
6197
return nil, err
6298
}
6399

@@ -66,6 +102,19 @@ func (s *Service) Create(
66102
}, nil
67103
}
68104

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+
69118
func (s *Service) Update(
70119
ctx context.Context,
71120
req *accountsv1.UpdateAccountRequest,
@@ -86,16 +135,25 @@ func (s *Service) Update(
86135
account.Extra = req.Extra
87136
account.LastUpdatedAt = time.Now().UTC()
88137
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
89147

90148
if account.Extra == nil {
91149
account.Extra = map[string]string{}
92150
}
93151

94-
if err := tx.Save(&account).Error; err != nil {
152+
if err = tx.Save(&account).Error; err != nil {
95153
return nil, err
96154
}
97155

98-
if err := tx.Commit().Error; err != nil {
156+
if err = tx.Commit().Error; err != nil {
99157
return nil, err
100158
}
101159

0 commit comments

Comments
 (0)