Skip to content

Commit 46be9e5

Browse files
authored
feat: accounts (#2)
* feat: change version * fix: name * feat: accounts * fix: migration
1 parent 6573aa7 commit 46be9e5

File tree

15 files changed

+365
-81
lines changed

15 files changed

+365
-81
lines changed

.github/workflows/general.yaml

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ on:
22
push:
33
branches:
44
- master
5-
- initial
65

76
env:
87
version: 0.0.0.${{github.run_number}}
@@ -13,7 +12,7 @@ jobs:
1312
versionOut: ${{ steps.generateVersion.outputs.version }}
1413
steps:
1514
- id: generateVersion
16-
run: echo "version=$(date '+%Y.%m.%d.%H%M%S').${{ github.run_number }}" >> "$GITHUB_OUTPUT"
15+
run: echo "version=0.0.${{ github.run_number }}" >> "$GITHUB_OUTPUT"
1716
image:
1817
runs-on: ubuntu-latest
1918
needs:

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,7 @@ go.work.sum
2525
.env
2626

2727
.idea/
28+
**/*_mocks_test.go
29+
**/*_mock_test.go
30+
**/*_mock.go
31+
**/*_mocks.go

cmd/server/accounts.go

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package main
2+
3+
import (
4+
"connectrpc.com/connect"
5+
"context"
6+
accountsv1 "github.com/ft-t/go-money-pb/gen/gomoneypb/accounts/v1"
7+
"github.com/ft-t/go-money-pb/gen/gomoneypb/accounts/v1/accountsv1connect"
8+
"github.com/ft-t/go-money/pkg/boilerplate"
9+
)
10+
11+
type AccountsApi struct {
12+
accSvc AccountSvc
13+
}
14+
15+
func (a *AccountsApi) CreateAccount(
16+
ctx context.Context,
17+
c *connect.Request[accountsv1.CreateAccountRequest],
18+
) (*connect.Response[accountsv1.CreateAccountResponse], error) {
19+
// todo auth
20+
21+
resp, err := a.accSvc.Create(ctx, c.Msg)
22+
if err != nil {
23+
return nil, err
24+
}
25+
26+
return connect.NewResponse(resp), nil
27+
}
28+
29+
func (a *AccountsApi) UpdateAccount(
30+
ctx context.Context,
31+
c *connect.Request[accountsv1.UpdateAccountRequest],
32+
) (*connect.Response[accountsv1.UpdateAccountResponse], error) {
33+
// todo auth
34+
35+
resp, err := a.accSvc.Update(ctx, c.Msg)
36+
if err != nil {
37+
return nil, err
38+
}
39+
40+
return connect.NewResponse(resp), nil
41+
}
42+
43+
func (a *AccountsApi) ListAccounts(
44+
ctx context.Context,
45+
c *connect.Request[accountsv1.ListAccountsRequest],
46+
) (*connect.Response[accountsv1.ListAccountsResponse], error) {
47+
// todo auth
48+
resp, err := a.accSvc.List(ctx, c.Msg)
49+
if err != nil {
50+
return nil, err
51+
}
52+
53+
return connect.NewResponse(resp), nil
54+
}
55+
56+
func NewAccountsApi(
57+
mux *boilerplate.DefaultGrpcServer,
58+
accSvc AccountSvc,
59+
) (*AccountsApi, error) {
60+
res := &AccountsApi{
61+
accSvc: accSvc,
62+
}
63+
64+
mux.GetMux().Handle(
65+
accountsv1connect.NewAccountsServiceHandler(res, mux.GetDefaultHandlerOptions()...),
66+
)
67+
68+
return res, nil
69+
}

cmd/server/interfaces.go

+18
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"context"
5+
accountsv1 "github.com/ft-t/go-money-pb/gen/gomoneypb/accounts/v1"
56
configurationv1 "github.com/ft-t/go-money-pb/gen/gomoneypb/configuration/v1"
67
usersv1 "github.com/ft-t/go-money-pb/gen/gomoneypb/users/v1"
78
)
@@ -18,6 +19,23 @@ type UserSvc interface {
1819
) (*usersv1.CreateResponse, error)
1920
}
2021

22+
type AccountSvc interface {
23+
Create(
24+
ctx context.Context,
25+
req *accountsv1.CreateAccountRequest,
26+
) (*accountsv1.CreateAccountResponse, error)
27+
28+
Update(
29+
ctx context.Context,
30+
req *accountsv1.UpdateAccountRequest,
31+
) (*accountsv1.UpdateAccountResponse, error)
32+
33+
List(
34+
ctx context.Context,
35+
req *accountsv1.ListAccountsRequest,
36+
) (*accountsv1.ListAccountsResponse, error)
37+
}
38+
2139
type ConfigSvc interface {
2240
GetConfiguration(
2341
ctx context.Context,

cmd/server/main.go

+16
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ package main
22

33
import (
44
"context"
5+
"github.com/ft-t/go-money/pkg/accounts"
56
"github.com/ft-t/go-money/pkg/appcfg"
67
"github.com/ft-t/go-money/pkg/boilerplate"
78
"github.com/ft-t/go-money/pkg/configuration"
9+
"github.com/ft-t/go-money/pkg/currency"
810
"github.com/ft-t/go-money/pkg/jwt"
11+
"github.com/ft-t/go-money/pkg/mappers"
912
"github.com/ft-t/go-money/pkg/users"
1013
"github.com/rs/zerolog/log"
1114
"os"
@@ -54,6 +57,19 @@ func main() {
5457
log.Logger.Fatal().Err(err).Msg("failed to create config handler")
5558
}
5659

60+
decimalSvc := currency.NewDecimalService()
61+
62+
mapper := mappers.NewMapper(&mappers.MapperConfig{
63+
DecimalSvc: decimalSvc,
64+
})
65+
66+
_, err = NewAccountsApi(grpcServer, accounts.NewService(&accounts.ServiceConfig{
67+
MapperSvc: mapper,
68+
}))
69+
if err != nil {
70+
log.Logger.Fatal().Err(err).Msg("failed to create accounts handler")
71+
}
72+
5773
go func() {
5874
grpcServer.ServeAsync(config.GrpcPort)
5975

File renamed without changes.

pkg/accounts/interfaces.go

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"github.com/ft-t/go-money/pkg/database"
77
)
88

9+
//go:generate mockgen -destination interfaces_mocks_test.go -package accounts_test -source=interfaces.go
10+
911
type MapperSvc interface {
1012
MapAccount(ctx context.Context, acc *database.Account) *v1.Account
1113
}

pkg/accounts/service.go

+20
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ type Service struct {
1414
cfg *ServiceConfig
1515
}
1616

17+
func (s *Service) List(ctx context.Context, req *accountsv1.ListAccountsRequest) (*accountsv1.ListAccountsResponse, error) {
18+
//TODO implement me
19+
panic("implement me")
20+
}
21+
1722
type ServiceConfig struct {
1823
MapperSvc MapperSvc
1924
}
@@ -40,6 +45,16 @@ func (s *Service) Create(
4045
CreatedAt: time.Now().UTC(),
4146
DeletedAt: gorm.DeletedAt{},
4247
Type: req.Account.Type,
48+
Note: req.Account.Note,
49+
}
50+
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
4358
}
4459

4560
if err := database.GetDbWithContext(ctx, database.DbTypeMaster).Create(account).Error; err != nil {
@@ -70,6 +85,11 @@ func (s *Service) Update(
7085
account.Type = req.Type
7186
account.Extra = req.Extra
7287
account.LastUpdatedAt = time.Now().UTC()
88+
account.Note = req.Note
89+
90+
if account.Extra == nil {
91+
account.Extra = map[string]string{}
92+
}
7393

7494
if err := tx.Save(&account).Error; err != nil {
7595
return nil, err

0 commit comments

Comments
 (0)