Skip to content

Commit 9bbcbe0

Browse files
committed
refactor(cqrs): use standard message in queries
1 parent 93750dc commit 9bbcbe0

33 files changed

Lines changed: 596 additions & 349 deletions

File tree

internal/app/server/component/page/dashboard/page.dashboard.templ

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ script Logout() {
3939
window.location.replace("/");
4040
}
4141

42-
templ Page(user *user.ReadResponse) {
42+
templ Page(attributes *user.ReadResponseAttributes) {
4343
@layout.Index(scripts.Head{}, scripts.Body{}) {
4444
<div class="ui centered grid container" style="height: 100%;">
4545
<div class="sixteen wide column">
@@ -56,11 +56,11 @@ templ Page(user *user.ReadResponse) {
5656
<img class="ui avatar image" src="/public/static/assets/favicon.png"/>
5757
<div class="content">
5858
<div class="ui inverted dropdown">
59-
<div class="text">{ user.Username }</div>
59+
<div class="text">{ attributes.Username }</div>
6060
<i class="dropdown icon"></i>
6161
<div class="menu">
6262
<div class="header">
63-
if !user.Verified {
63+
if !attributes.Verified {
6464
<i class="inverted red times circle icon" data-variation="red" data-content="Unverified"></i>
6565
} else {
6666
<i class="inverted green check circle icon" data-variation="green" data-content="Verified"></i>
@@ -85,10 +85,10 @@ templ Page(user *user.ReadResponse) {
8585
</div>
8686
</div>
8787
<div class="sixteen wide mobile eight wide computer column">
88-
@UpdateForm(user.Email, user.Username)
88+
@UpdateForm(attributes.Email, attributes.Username)
8989
@DeleteForm()
9090
</div>
91-
if !user.Verified {
91+
if !attributes.Verified {
9292
<div class="ui looping pulsating transition blue bottom fixed nag">
9393
<div class="title">
9494
<i class="check circle icon"></i>

internal/app/server/component/page/dashboard/page.dashboard_templ.go

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/app/server/handler/page/dashboard.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import (
77
"github.com/bastean/codexgo/v4/internal/app/server/service/errs"
88
"github.com/bastean/codexgo/v4/internal/app/server/service/format"
99
"github.com/bastean/codexgo/v4/internal/app/server/service/key"
10-
"github.com/bastean/codexgo/v4/internal/pkg/service/communication"
1110
"github.com/bastean/codexgo/v4/internal/pkg/service/errors"
1211
"github.com/bastean/codexgo/v4/internal/pkg/service/module/user"
12+
"github.com/bastean/codexgo/v4/internal/pkg/service/query"
1313
)
1414

1515
func Dashboard(c *gin.Context) {
@@ -20,18 +20,22 @@ func Dashboard(c *gin.Context) {
2020
return
2121
}
2222

23-
query := new(user.ReadQuery)
23+
attributes := new(user.ReadQueryAttributes)
2424

25-
query.Id = format.ToString(id)
25+
attributes.Id = format.ToString(id)
2626

27-
response, err := communication.QueryBus.Ask(query)
27+
response, err := query.Bus.Ask(query.New(
28+
user.ReadQueryKey,
29+
attributes,
30+
new(user.ReadQueryMeta),
31+
))
2832

2933
if err != nil {
3034
errs.AbortByErrWithRedirect(c, errors.BubbleUp(err, "Dashboard"), "/")
3135
return
3236
}
3337

34-
found, ok := response.(*user.ReadResponse)
38+
found, ok := response.Attributes.(*user.ReadResponseAttributes)
3539

3640
if !ok {
3741
errs.AbortByErrWithRedirect(c, errs.Assertion("Dashboard"), "/")

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,33 @@ import (
1111
"github.com/bastean/codexgo/v4/internal/app/server/service/key"
1212
"github.com/bastean/codexgo/v4/internal/app/server/service/reply"
1313
"github.com/bastean/codexgo/v4/internal/pkg/service/authentication/jwt"
14-
"github.com/bastean/codexgo/v4/internal/pkg/service/communication"
1514
"github.com/bastean/codexgo/v4/internal/pkg/service/errors"
1615
"github.com/bastean/codexgo/v4/internal/pkg/service/module/user"
16+
"github.com/bastean/codexgo/v4/internal/pkg/service/query"
1717
)
1818

1919
func Login(c *gin.Context) {
20-
query := new(user.LoginQuery)
20+
attributes := new(user.LoginQueryAttributes)
2121

22-
err := c.BindJSON(query)
22+
err := c.BindJSON(attributes)
2323

2424
if err != nil {
2525
errs.AbortByErr(c, errs.BindingJSON(err, "Login"))
2626
return
2727
}
2828

29-
response, err := communication.QueryBus.Ask(query)
29+
response, err := query.Bus.Ask(query.New(
30+
user.LoginQueryKey,
31+
attributes,
32+
new(user.LoginQueryMeta),
33+
))
3034

3135
if err != nil {
3236
errs.AbortByErr(c, errors.BubbleUp(err, "Login"))
3337
return
3438
}
3539

36-
found, ok := response.(*user.LoginResponse)
40+
found, ok := response.Attributes.(*user.LoginResponseAttributes)
3741

3842
if !ok {
3943
errs.AbortByErr(c, errs.Assertion("Login"))

internal/pkg/service/communication/communication.go

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

33
import (
4-
"github.com/bastean/codexgo/v4/internal/pkg/service/communication/memory"
54
"github.com/bastean/codexgo/v4/internal/pkg/service/communication/rabbitmq"
65
"github.com/bastean/codexgo/v4/internal/pkg/service/env"
76
"github.com/bastean/codexgo/v4/internal/pkg/service/errors"
@@ -21,7 +20,6 @@ var Service = &struct {
2120
var (
2221
err error
2322
RabbitMQ *rabbitmq.RabbitMQ
24-
QueryBus *memory.QueryBus
2523
)
2624

2725
func Up() error {

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

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

internal/pkg/service/module/module.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package module
33
import (
44
"github.com/bastean/codexgo/v4/internal/pkg/service/command"
55
"github.com/bastean/codexgo/v4/internal/pkg/service/communication"
6-
"github.com/bastean/codexgo/v4/internal/pkg/service/communication/memory"
76
"github.com/bastean/codexgo/v4/internal/pkg/service/errors"
87
"github.com/bastean/codexgo/v4/internal/pkg/service/module/user"
98
"github.com/bastean/codexgo/v4/internal/pkg/service/persistence"
9+
"github.com/bastean/codexgo/v4/internal/pkg/service/query"
1010
"github.com/bastean/codexgo/v4/internal/pkg/service/record/log"
1111
)
1212

@@ -52,9 +52,9 @@ func Start() error {
5252
return errors.BubbleUp(err, "Start")
5353
}
5454

55-
communication.QueryBus, err = memory.NewQueryBus([]memory.QueryHandler{
56-
user.ReadHandler,
57-
user.LoginHandler,
55+
query.Bus, err = query.NewBus(query.Mapper{
56+
user.ReadQueryKey: user.ReadHandler,
57+
user.LoginQueryKey: user.LoginHandler,
5858
})
5959

6060
if err != nil {

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

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,33 @@ var (
2525
VerifyCommandKey = verify.CommandKey
2626
)
2727

28+
var (
29+
ReadQueryKey = read.QueryKey
30+
LoginQueryKey = login.QueryKey
31+
)
32+
33+
var (
34+
ReadResponseKey = read.ResponseKey
35+
LoginResponseKey = login.ResponseKey
36+
)
37+
2838
type (
2939
CreateCommandAttributes = create.CommandAttributes
3040
UpdateCommandAttributes = update.CommandAttributes
3141
DeleteCommandAttributes = delete.CommandAttributes
3242
VerifyCommandAttributes = verify.CommandAttributes
3343
)
3444

45+
type (
46+
ReadQueryAttributes = read.QueryAttributes
47+
LoginQueryAttributes = login.QueryAttributes
48+
)
49+
50+
type (
51+
ReadResponseAttributes = read.ResponseAttributes
52+
LoginResponseAttributes = login.ResponseAttributes
53+
)
54+
3555
type (
3656
CreateCommandMeta = create.CommandMeta
3757
UpdateCommandMeta = update.CommandMeta
@@ -40,11 +60,11 @@ type (
4060
)
4161

4262
type (
43-
ReadQuery = read.Query
44-
LoginQuery = login.Query
63+
ReadQueryMeta = read.QueryMeta
64+
LoginQueryMeta = login.QueryMeta
4565
)
4666

4767
type (
48-
ReadResponse = read.Response
49-
LoginResponse = login.Response
68+
ReadResponseMeta = read.ResponseMeta
69+
LoginResponseMeta = login.ResponseMeta
5070
)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package query
2+
3+
import (
4+
"github.com/bastean/codexgo/v4/pkg/context/shared/domain/messages"
5+
"github.com/bastean/codexgo/v4/pkg/context/shared/domain/queries"
6+
"github.com/bastean/codexgo/v4/pkg/context/shared/infrastructure/communications/memory"
7+
)
8+
9+
var (
10+
Bus queries.Bus
11+
)
12+
13+
var (
14+
NewBus = memory.NewQueryBus
15+
)
16+
17+
type (
18+
Mapper = memory.QueryMapper
19+
Handler = queries.Handler
20+
)
21+
22+
func New(key queries.Key, attributes, meta any) *queries.Query {
23+
return messages.New[queries.Query](key, attributes, meta)
24+
}

pkg/context/notification/application/confirmation/event.go renamed to pkg/context/notification/application/confirmation/consumer.go

File renamed without changes.

0 commit comments

Comments
 (0)