Skip to content

Commit db3f0f2

Browse files
committed
Agregado de event, command y query handlers
1 parent 1c402e4 commit db3f0f2

File tree

19 files changed

+354
-194
lines changed

19 files changed

+354
-194
lines changed

.idea/.gitignore

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

.idea/go-hexagonal_http_api-course.iml

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

.idea/modules.xml

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

.idea/vcs.xml

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

04-03-command-bus/cmd/api/bootstrap/boostrap.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func Run() error {
3131
}
3232

3333
var (
34-
commandBus = inmemory.NewCommandBus()
34+
bus = inmemory.NewCommandBus()
3535
)
3636

3737
courseRepository := mysql.NewCourseRepository(db)
@@ -40,11 +40,11 @@ func Run() error {
4040
fetchingCourseService := fetching.NewCourseFetchingService(courseRepository)
4141

4242
createCourseCommandHandler := creating.NewCourseCommandHandler(creatingCourseService)
43-
fetchingCourseCommandHandler := fetching.NewCourseCommandHandler(fetchingCourseService)
43+
fetchingCourseQueryHandler := fetching.NewCourseQueryHandler(fetchingCourseService)
4444

45-
commandBus.Register(creating.CourseCommandType, createCourseCommandHandler)
46-
commandBus.Register(fetching.CourseCommandType, fetchingCourseCommandHandler)
45+
bus.RegisterCommandHandler(creating.CourseCommandType, createCourseCommandHandler)
46+
bus.RegisterQueryHandler(fetching.CourseQueryType, fetchingCourseQueryHandler)
4747

48-
srv := server.New(host, port, commandBus)
48+
srv := server.New(host, port, bus)
4949
return srv.Run()
5050
}

04-03-command-bus/internal/creating/command.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import (
44
"context"
55
"errors"
66

7-
"github.com/CodelyTV/go-hexagonal_http_api-course/04-03-command-bus/kit/command"
7+
"github.com/CodelyTV/go-hexagonal_http_api-course/04-03-command-bus/kit/bus"
88
)
99

10-
const CourseCommandType command.Type = "command.creating.course"
10+
const CourseCommandType bus.Type = "bus.creating.course"
1111

12-
// CourseCommand is the command dispatched to create a new course.
12+
// CourseCommand is the bus dispatched to create a new course.
1313
type CourseCommand struct {
1414
id string
1515
name string
@@ -25,11 +25,11 @@ func NewCourseCommand(id, name, duration string) CourseCommand {
2525
}
2626
}
2727

28-
func (c CourseCommand) Type() command.Type {
28+
func (c CourseCommand) Type() bus.Type {
2929
return CourseCommandType
3030
}
3131

32-
// CourseCommandHandler is the command handler
32+
// CourseCommandHandler is the bus handler
3333
// responsible for creating courses.
3434
type CourseCommandHandler struct {
3535
service CourseService
@@ -42,11 +42,11 @@ func NewCourseCommandHandler(service CourseService) CourseCommandHandler {
4242
}
4343
}
4444

45-
// Handle implements the command.Handler interface.
46-
func (h CourseCommandHandler) Handle(ctx context.Context, cmd command.Command) error {
45+
// Handle implements the bus.CommandHandler interface.
46+
func (h CourseCommandHandler) Handle(ctx context.Context, cmd bus.Command) error {
4747
createCourseCmd, ok := cmd.(CourseCommand)
4848
if !ok {
49-
return errors.New("unexpected command")
49+
return errors.New("unexpected bus")
5050
}
5151

5252
return h.service.CreateCourse(

04-03-command-bus/internal/fetching/command.go

Lines changed: 0 additions & 46 deletions
This file was deleted.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package fetching
2+
3+
import (
4+
"context"
5+
"errors"
6+
"github.com/CodelyTV/go-hexagonal_http_api-course/04-03-command-bus/kit/bus"
7+
)
8+
9+
const CourseQueryType bus.Type = "bus.fetching.courses"
10+
11+
// CourseQuery is the bus dispatched to create a new course.
12+
type CourseQuery struct {
13+
}
14+
15+
// NewFetchCourseQuery creates a new CourseQuery.
16+
func NewFetchCourseQuery() CourseQuery {
17+
return CourseQuery{}
18+
}
19+
20+
func (c CourseQuery) Type() bus.Type {
21+
return CourseQueryType
22+
}
23+
24+
// CourseQueryHandler is the bus handler
25+
// responsible for fetching courses.
26+
type CourseQueryHandler struct {
27+
service FetchingCourseService
28+
}
29+
30+
// NewCourseQueryHandler initializes a new NewCourseQueryHandler.
31+
func NewCourseQueryHandler(service FetchingCourseService) CourseQueryHandler {
32+
return CourseQueryHandler{
33+
service: service,
34+
}
35+
}
36+
37+
// Handle implements the bus.QueryHandler interface.
38+
func (h CourseQueryHandler) Handle(ctx context.Context, query bus.Query) (bus.QueryResponse, error) {
39+
_, ok := query.(CourseQuery)
40+
if !ok {
41+
return nil, errors.New("unexpected bus")
42+
}
43+
44+
return h.service.GetAll(ctx)
45+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package inmemory
2+
3+
import (
4+
"context"
5+
"log"
6+
7+
"github.com/CodelyTV/go-hexagonal_http_api-course/04-03-command-bus/kit/bus"
8+
)
9+
10+
// Bus is an in-memory implementation of the bus.Bus.
11+
type Bus struct {
12+
commandsHandlers map[bus.Type]bus.CommandHandler
13+
queryHandlers map[bus.Type]bus.QueryHandler
14+
eventHandlers map[bus.Type]bus.EventHandler
15+
}
16+
17+
// NewCommandBus initializes a new instance of Bus.
18+
func NewCommandBus() *Bus {
19+
return &Bus{
20+
commandsHandlers: make(map[bus.Type]bus.CommandHandler),
21+
queryHandlers: make(map[bus.Type]bus.QueryHandler),
22+
eventHandlers: make(map[bus.Type]bus.EventHandler),
23+
}
24+
}
25+
26+
// DispatchCommand implements the bus.Bus interface.
27+
func (b *Bus) DispatchCommand(ctx context.Context, cmd bus.Command) error {
28+
handler, ok := b.commandsHandlers[cmd.Type()]
29+
if !ok {
30+
return nil
31+
}
32+
33+
go func() {
34+
err := handler.Handle(ctx, cmd)
35+
if err != nil {
36+
log.Printf("Error while handling %s - %s\n", cmd.Type(), err)
37+
}
38+
39+
}()
40+
41+
return nil
42+
}
43+
44+
// RegisterCommandHandler implements the bus.Bus interface.
45+
func (b *Bus) RegisterCommandHandler(cmdType bus.Type, handler bus.CommandHandler) {
46+
b.commandsHandlers[cmdType] = handler
47+
}
48+
49+
// DispatchQuery implements the bus.Bus interface.
50+
func (b *Bus) DispatchQuery(ctx context.Context, query bus.Query) (bus.QueryResponse, error) {
51+
handler, ok := b.queryHandlers[query.Type()]
52+
if !ok {
53+
return nil, nil
54+
}
55+
// Como este es sincronico, decide el que lo usa si lo espera o no.
56+
return handler.Handle(ctx, query)
57+
}
58+
59+
// RegisterQueryHandler implements the bus.Bus interface.
60+
func (b *Bus) RegisterQueryHandler(queryType bus.Type, handler bus.QueryHandler) {
61+
b.queryHandlers[queryType] = handler
62+
}
63+
64+
// DispatchEvent implements the bus.Bus interface.
65+
func (b *Bus) DispatchEvent(ctx context.Context, event bus.Event) error {
66+
handler, ok := b.eventHandlers[event.Type()]
67+
if !ok {
68+
return nil
69+
}
70+
71+
go func() {
72+
err := handler.Handle(ctx, event)
73+
if err != nil {
74+
log.Printf("Error while handling %s - %s\n", event.Type(), err)
75+
}
76+
77+
}()
78+
79+
return nil
80+
}
81+
82+
// RegisterEventHandler implements the bus.Bus interface.
83+
func (b *Bus) RegisterEventHandler(cmdType bus.Type, handler bus.EventHandler) {
84+
b.eventHandlers[cmdType] = handler
85+
}

04-03-command-bus/internal/platform/bus/inmemory/command.go

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

0 commit comments

Comments
 (0)