Skip to content

Commit 5721349

Browse files
committed
remove C_AggregateID from CommandBase
Signed-off-by: Jerry Bai <chinaren_bjr@163.com>
1 parent 168951e commit 5721349

4 files changed

Lines changed: 41 additions & 29 deletions

File tree

commanding/commanding.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"log/slog"
66
"strings"
77

8-
"github.com/berkaroad/squat/domain"
98
"github.com/berkaroad/squat/messaging"
109
"github.com/berkaroad/squat/serialization"
1110
)
@@ -61,31 +60,23 @@ type CommandHandlerGroup interface {
6160

6261
type CommandHandlerProxy messaging.MessageHandlerProxy[CommandData]
6362

64-
func NewCommandBase(commandID string, aggregateID string) CommandBase {
63+
func NewCommandBase(commandID string) CommandBase {
6564
if commandID == "" {
6665
panic(ErrEmptyCommandID)
6766
}
68-
if aggregateID == "" {
69-
panic(domain.ErrEmptyAggregateID)
70-
}
7167
return CommandBase{
72-
C_ID: commandID,
73-
C_AggregateID: aggregateID,
68+
C_ID: commandID,
7469
}
7570
}
7671

7772
// Base of command, should override method 'TypeName()' and 'AggregateTypeName()'
7873
type CommandBase struct {
79-
C_ID string `json:"c_id"`
80-
C_AggregateID string `json:"c_aggregate_id"`
74+
C_ID string `json:"c_id"`
8175
}
8276

8377
func (c CommandBase) CommandID() string {
8478
return c.C_ID
8579
}
86-
func (c CommandBase) AggregateID() string {
87-
return c.C_AggregateID
88-
}
8980

9081
func CreateCommandMail(data *CommandData) messaging.Mail[CommandData] {
9182
return &commandMail{

tests/benchmark_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ func BenchmarkAccount(b *testing.B) {
135135
for i := 0; i < b.N; i++ {
136136
wg.Add(1)
137137
cmd := &CreateAccountCommand{
138-
CommandBase: commanding.NewCommandBase(NewUUID(), fmt.Sprintf("acc-%d", i)),
138+
CommandBase: commanding.NewCommandBase(NewUUID()),
139+
AccountId: fmt.Sprintf("acc-%d", i),
139140
Name: fmt.Sprintf("Account %d", i),
140141
}
141142
go processCmdResult(wg, cmd, time.Second*30)
@@ -153,7 +154,8 @@ func BenchmarkAccount(b *testing.B) {
153154
for i := 0; i < b.N; i++ {
154155
wg.Add(1)
155156
cmd := &DepositCommand{
156-
CommandBase: commanding.NewCommandBase(NewUUID(), fmt.Sprintf("acc-%d", i)),
157+
CommandBase: commanding.NewCommandBase(NewUUID()),
158+
AccountId: fmt.Sprintf("acc-%d", i),
157159
Amount: 1.1,
158160
}
159161
go processCmdResult(wg, cmd, time.Second*30)
@@ -171,7 +173,8 @@ func BenchmarkAccount(b *testing.B) {
171173
for i := 0; i < b.N; i++ {
172174
wg.Add(1)
173175
cmd := &WithdrawCommand{
174-
CommandBase: commanding.NewCommandBase(NewUUID(), fmt.Sprintf("acc-%d", i)),
176+
CommandBase: commanding.NewCommandBase(NewUUID()),
177+
AccountId: fmt.Sprintf("acc-%d", i),
175178
Amount: 1.1,
176179
}
177180
go processCmdResult(wg, cmd, time.Second*30)
@@ -189,7 +192,8 @@ func BenchmarkAccount(b *testing.B) {
189192
for i := 0; i < b.N; i++ {
190193
wg.Add(1)
191194
cmd := &RemoveAccountCommand{
192-
CommandBase: commanding.NewCommandBase(NewUUID(), fmt.Sprintf("acc-%d", i)),
195+
CommandBase: commanding.NewCommandBase(NewUUID()),
196+
AccountId: fmt.Sprintf("acc-%d", i),
193197
}
194198
go processCmdResult(wg, cmd, time.Second*30)
195199
}

tests/commandhandlers.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ func (ch *AccountCommandHandler) handleCreateAccount(ctx context.Context, data c
2929
data.SetCustomExtension(ctx, "create", "false")
3030
cmd := data.Command.(*CreateAccountCommand)
3131

32-
accountData, err := ch.Repo.Get(ctx, cmd.C_AggregateID)
32+
accountData, err := ch.Repo.Get(ctx, cmd.AggregateID())
3333
if err != nil {
3434
logger.Error(err.Error())
3535
return err
3636
}
3737

3838
newAccountData, err := CreateAccount(accountData, AccountInfo{
39-
ID: cmd.C_AggregateID,
39+
ID: cmd.AggregateID(),
4040
Name: cmd.Name,
4141
})
4242
if err != nil {
@@ -57,13 +57,13 @@ func (ch *AccountCommandHandler) handleDeposit(ctx context.Context, data command
5757
data.SetCustomExtension(ctx, "update", "true")
5858
cmd := data.Command.(*DepositCommand)
5959

60-
accountData, err := ch.Repo.Get(ctx, cmd.C_AggregateID)
60+
accountData, err := ch.Repo.Get(ctx, cmd.AggregateID())
6161
if err != nil {
6262
logger.Error(err.Error())
6363
return err
6464
}
6565
if accountData == nil {
66-
return fmt.Errorf("%w: %s", ErrAccounteNotExists, cmd.C_AggregateID)
66+
return fmt.Errorf("%w: %s", ErrAccounteNotExists, cmd.AggregateID())
6767
}
6868

6969
err = accountData.Deposit(cmd.Amount)
@@ -85,13 +85,13 @@ func (ch *AccountCommandHandler) handleWithdraw(ctx context.Context, data comman
8585
data.SetCustomExtension(ctx, "update", "true")
8686
cmd := data.Command.(*WithdrawCommand)
8787

88-
accountData, err := ch.Repo.Get(ctx, cmd.C_AggregateID)
88+
accountData, err := ch.Repo.Get(ctx, cmd.AggregateID())
8989
if err != nil {
9090
logger.Error(err.Error())
9191
return err
9292
}
9393
if accountData == nil {
94-
return fmt.Errorf("%w: %s", ErrAccounteNotExists, cmd.C_AggregateID)
94+
return fmt.Errorf("%w: %s", ErrAccounteNotExists, cmd.AggregateID())
9595
}
9696

9797
err = accountData.WithDraw(cmd.Amount)
@@ -113,7 +113,7 @@ func (ch *AccountCommandHandler) handleRemoveAccount(ctx context.Context, data c
113113
data.SetCustomExtension(ctx, "delete", "true")
114114
cmd := data.Command.(*RemoveAccountCommand)
115115

116-
accountData, err := ch.Repo.Get(ctx, cmd.C_AggregateID)
116+
accountData, err := ch.Repo.Get(ctx, cmd.AggregateID())
117117
if err != nil {
118118
logger.Error(err.Error())
119119
return err

tests/commands.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,18 @@ var _ commanding.Command = (*CreateAccountCommand)(nil)
1515

1616
type CreateAccountCommand struct {
1717
commanding.CommandBase
18-
19-
Name string
18+
AccountId string
19+
Name string
2020
}
2121

2222
func (c CreateAccountCommand) TypeName() string {
2323
return Command_CreateAccount
2424
}
2525

26+
func (c CreateAccountCommand) AggregateID() string {
27+
return c.AccountId
28+
}
29+
2630
func (c CreateAccountCommand) AggregateTypeName() string {
2731
return AggregateTypeName
2832
}
@@ -31,14 +35,18 @@ var _ commanding.Command = (*DepositCommand)(nil)
3135

3236
type DepositCommand struct {
3337
commanding.CommandBase
34-
35-
Amount float64
38+
AccountId string
39+
Amount float64
3640
}
3741

3842
func (c DepositCommand) TypeName() string {
3943
return Command_Deposit
4044
}
4145

46+
func (c DepositCommand) AggregateID() string {
47+
return c.AccountId
48+
}
49+
4250
func (c DepositCommand) AggregateTypeName() string {
4351
return AggregateTypeName
4452
}
@@ -47,14 +55,18 @@ var _ commanding.Command = (*WithdrawCommand)(nil)
4755

4856
type WithdrawCommand struct {
4957
commanding.CommandBase
50-
51-
Amount float64
58+
AccountId string
59+
Amount float64
5260
}
5361

5462
func (c WithdrawCommand) TypeName() string {
5563
return Command_Withdraw
5664
}
5765

66+
func (c WithdrawCommand) AggregateID() string {
67+
return c.AccountId
68+
}
69+
5870
func (c WithdrawCommand) AggregateTypeName() string {
5971
return AggregateTypeName
6072
}
@@ -63,12 +75,17 @@ var _ commanding.Command = (*RemoveAccountCommand)(nil)
6375

6476
type RemoveAccountCommand struct {
6577
commanding.CommandBase
78+
AccountId string
6679
}
6780

6881
func (c RemoveAccountCommand) TypeName() string {
6982
return Command_RemoveAccount
7083
}
7184

85+
func (c RemoveAccountCommand) AggregateID() string {
86+
return c.AccountId
87+
}
88+
7289
func (c RemoveAccountCommand) AggregateTypeName() string {
7390
return AggregateTypeName
7491
}

0 commit comments

Comments
 (0)