Skip to content

Commit 93750dc

Browse files
committed
refactor(cqrs): use standard message in commands
1 parent 92c332b commit 93750dc

32 files changed

Lines changed: 283 additions & 291 deletions

File tree

internal/app/server/handler/user/create.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,26 @@ import (
77

88
"github.com/bastean/codexgo/v4/internal/app/server/service/errs"
99
"github.com/bastean/codexgo/v4/internal/app/server/service/reply"
10-
"github.com/bastean/codexgo/v4/internal/pkg/service/communication"
10+
"github.com/bastean/codexgo/v4/internal/pkg/service/command"
1111
"github.com/bastean/codexgo/v4/internal/pkg/service/errors"
1212
"github.com/bastean/codexgo/v4/internal/pkg/service/module/user"
1313
)
1414

1515
func Create(c *gin.Context) {
16-
command := new(user.CreateCommand)
16+
attributes := new(user.CreateCommandAttributes)
1717

18-
err := c.BindJSON(command)
18+
err := c.BindJSON(attributes)
1919

2020
if err != nil {
2121
errs.AbortByErr(c, errs.BindingJSON(err, "Create"))
2222
return
2323
}
2424

25-
err = communication.CommandBus.Dispatch(command)
25+
err = command.Bus.Dispatch(command.New(
26+
user.CreateCommandKey,
27+
attributes,
28+
new(user.CreateCommandMeta),
29+
))
2630

2731
if err != nil {
2832
errs.AbortByErr(c, errors.BubbleUp(err, "Create"))

internal/app/server/handler/user/delete.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/bastean/codexgo/v4/internal/app/server/service/format"
1111
"github.com/bastean/codexgo/v4/internal/app/server/service/key"
1212
"github.com/bastean/codexgo/v4/internal/app/server/service/reply"
13-
"github.com/bastean/codexgo/v4/internal/pkg/service/communication"
13+
"github.com/bastean/codexgo/v4/internal/pkg/service/command"
1414
"github.com/bastean/codexgo/v4/internal/pkg/service/errors"
1515
"github.com/bastean/codexgo/v4/internal/pkg/service/module/user"
1616
)
@@ -23,18 +23,22 @@ func Delete(c *gin.Context) {
2323
return
2424
}
2525

26-
command := new(user.DeleteCommand)
26+
attributes := new(user.DeleteCommandAttributes)
2727

28-
err := c.BindJSON(command)
28+
err := c.BindJSON(attributes)
2929

3030
if err != nil {
3131
errs.AbortByErr(c, errs.BindingJSON(err, "Delete"))
3232
return
3333
}
3434

35-
command.Id = format.ToString(id)
35+
attributes.Id = format.ToString(id)
3636

37-
err = communication.CommandBus.Dispatch(command)
37+
err = command.Bus.Dispatch(command.New(
38+
user.DeleteCommandKey,
39+
attributes,
40+
new(user.DeleteCommandMeta),
41+
))
3842

3943
if err != nil {
4044
errs.AbortByErr(c, errors.BubbleUp(err, "Delete"))

internal/app/server/handler/user/update.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/bastean/codexgo/v4/internal/app/server/service/format"
1010
"github.com/bastean/codexgo/v4/internal/app/server/service/key"
1111
"github.com/bastean/codexgo/v4/internal/app/server/service/reply"
12-
"github.com/bastean/codexgo/v4/internal/pkg/service/communication"
12+
"github.com/bastean/codexgo/v4/internal/pkg/service/command"
1313
"github.com/bastean/codexgo/v4/internal/pkg/service/errors"
1414
"github.com/bastean/codexgo/v4/internal/pkg/service/module/user"
1515
)
@@ -22,18 +22,22 @@ func Update(c *gin.Context) {
2222
return
2323
}
2424

25-
command := new(user.UpdateCommand)
25+
attributes := new(user.UpdateCommandAttributes)
2626

27-
err := c.BindJSON(command)
27+
err := c.BindJSON(attributes)
2828

2929
if err != nil {
3030
errs.AbortByErr(c, errs.BindingJSON(err, "Update"))
3131
return
3232
}
3333

34-
command.Id = format.ToString(id)
34+
attributes.Id = format.ToString(id)
3535

36-
err = communication.CommandBus.Dispatch(command)
36+
err = command.Bus.Dispatch(command.New(
37+
user.UpdateCommandKey,
38+
attributes,
39+
new(user.UpdateCommandMeta),
40+
))
3741

3842
if err != nil {
3943
errs.AbortByErr(c, errors.BubbleUp(err, "Update"))

internal/app/server/handler/user/verify.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
"github.com/bastean/codexgo/v4/internal/app/server/service/errs"
99
"github.com/bastean/codexgo/v4/internal/app/server/service/key"
10-
"github.com/bastean/codexgo/v4/internal/pkg/service/communication"
10+
"github.com/bastean/codexgo/v4/internal/pkg/service/command"
1111
"github.com/bastean/codexgo/v4/internal/pkg/service/errors"
1212
"github.com/bastean/codexgo/v4/internal/pkg/service/module/user"
1313
)
@@ -20,11 +20,15 @@ func Verify(c *gin.Context) {
2020
return
2121
}
2222

23-
command := new(user.VerifyCommand)
23+
attributes := new(user.VerifyCommandAttributes)
2424

25-
command.Id = id
25+
attributes.Id = id
2626

27-
err := communication.CommandBus.Dispatch(command)
27+
err := command.Bus.Dispatch(command.New(
28+
user.VerifyCommandKey,
29+
attributes,
30+
new(user.VerifyCommandMeta),
31+
))
2832

2933
if err != nil {
3034
errs.AbortByErr(c, errors.BubbleUp(err, "Verify"))
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package command
2+
3+
import (
4+
"github.com/bastean/codexgo/v4/pkg/context/shared/domain/commands"
5+
"github.com/bastean/codexgo/v4/pkg/context/shared/domain/messages"
6+
"github.com/bastean/codexgo/v4/pkg/context/shared/infrastructure/communications/memory"
7+
)
8+
9+
var (
10+
Bus commands.Bus
11+
)
12+
13+
var (
14+
NewBus = memory.NewCommandBus
15+
)
16+
17+
type (
18+
Mapper = memory.CommandMapper
19+
Handler = commands.Handler
20+
)
21+
22+
func New(key commands.Key, attributes, meta any) *commands.Command {
23+
return messages.New[commands.Command](key, attributes, meta)
24+
}

internal/pkg/service/communication/communication.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@ var Service = &struct {
1919
}
2020

2121
var (
22-
err error
23-
RabbitMQ *rabbitmq.RabbitMQ
24-
CommandBus *memory.CommandBus
25-
QueryBus *memory.QueryBus
22+
err error
23+
RabbitMQ *rabbitmq.RabbitMQ
24+
QueryBus *memory.QueryBus
2625
)
2726

2827
func Up() error {

internal/pkg/service/communication/memory/command.go

Lines changed: 0 additions & 15 deletions
This file was deleted.

internal/pkg/service/module/module.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package module
22

33
import (
4+
"github.com/bastean/codexgo/v4/internal/pkg/service/command"
45
"github.com/bastean/codexgo/v4/internal/pkg/service/communication"
56
"github.com/bastean/codexgo/v4/internal/pkg/service/communication/memory"
67
"github.com/bastean/codexgo/v4/internal/pkg/service/errors"
@@ -39,11 +40,11 @@ func Start() error {
3940

4041
log.Starting(communication.Service.InMemory)
4142

42-
communication.CommandBus, err = memory.NewCommandBus([]memory.CommandHandler{
43-
user.Create,
44-
user.Update,
45-
user.Delete,
46-
user.Verify,
43+
command.Bus, err = command.NewBus(command.Mapper{
44+
user.CreateCommandKey: user.CreateHandler,
45+
user.UpdateCommandKey: user.UpdateHandler,
46+
user.DeleteCommandKey: user.DeleteHandler,
47+
user.VerifyCommandKey: user.VerifyHandler,
4748
})
4849

4950
if err != nil {
@@ -52,8 +53,8 @@ func Start() error {
5253
}
5354

5455
communication.QueryBus, err = memory.NewQueryBus([]memory.QueryHandler{
55-
user.Read,
56-
user.Login,
56+
user.ReadHandler,
57+
user.LoginHandler,
5758
})
5859

5960
if err != nil {

internal/pkg/service/module/user/handler.go

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,34 @@ import (
99
"github.com/bastean/codexgo/v4/pkg/context/user/application/verify"
1010
)
1111

12+
var (
13+
CreateHandler *create.Handler
14+
ReadHandler *read.Handler
15+
UpdateHandler *update.Handler
16+
DeleteHandler *delete.Handler
17+
VerifyHandler *verify.Handler
18+
LoginHandler *login.Handler
19+
)
20+
21+
var (
22+
CreateCommandKey = create.CommandKey
23+
UpdateCommandKey = update.CommandKey
24+
DeleteCommandKey = delete.CommandKey
25+
VerifyCommandKey = verify.CommandKey
26+
)
27+
1228
type (
13-
CreateCommand = create.Command
14-
UpdateCommand = update.Command
15-
DeleteCommand = delete.Command
16-
VerifyCommand = verify.Command
29+
CreateCommandAttributes = create.CommandAttributes
30+
UpdateCommandAttributes = update.CommandAttributes
31+
DeleteCommandAttributes = delete.CommandAttributes
32+
VerifyCommandAttributes = verify.CommandAttributes
33+
)
34+
35+
type (
36+
CreateCommandMeta = create.CommandMeta
37+
UpdateCommandMeta = update.CommandMeta
38+
DeleteCommandMeta = delete.CommandMeta
39+
VerifyCommandMeta = verify.CommandMeta
1740
)
1841

1942
type (
@@ -25,12 +48,3 @@ type (
2548
ReadResponse = read.Response
2649
LoginResponse = login.Response
2750
)
28-
29-
var (
30-
Create *create.Handler
31-
Read *read.Handler
32-
Update *update.Handler
33-
Delete *delete.Handler
34-
Verify *verify.Handler
35-
Login *login.Handler
36-
)

internal/pkg/service/module/user/user.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,40 +13,40 @@ import (
1313
)
1414

1515
func Start(repository repository.Repository, bus events.Bus, hashing hashing.Hashing) {
16-
Create = &create.Handler{
16+
CreateHandler = &create.Handler{
1717
Create: &create.Create{
1818
Repository: repository,
1919
},
2020
Bus: bus,
2121
}
2222

23-
Read = &read.Handler{
23+
ReadHandler = &read.Handler{
2424
Read: &read.Read{
2525
Repository: repository,
2626
},
2727
}
2828

29-
Update = &update.Handler{
29+
UpdateHandler = &update.Handler{
3030
Update: &update.Update{
3131
Repository: repository,
3232
Hashing: hashing,
3333
},
3434
}
3535

36-
Delete = &delete.Handler{
36+
DeleteHandler = &delete.Handler{
3737
Delete: &delete.Delete{
3838
Repository: repository,
3939
Hashing: hashing,
4040
},
4141
}
4242

43-
Verify = &verify.Handler{
43+
VerifyHandler = &verify.Handler{
4444
Verify: &verify.Verify{
4545
Repository: repository,
4646
},
4747
}
4848

49-
Login = &login.Handler{
49+
LoginHandler = &login.Handler{
5050
Login: &login.Login{
5151
Repository: repository,
5252
Hashing: hashing,

0 commit comments

Comments
 (0)