Skip to content

Commit a32031d

Browse files
committed
04-03. Add fetching course command
1 parent bfd8a00 commit a32031d

File tree

23 files changed

+538
-128
lines changed

23 files changed

+538
-128
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: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package bootstrap
33
import (
44
"database/sql"
55
"fmt"
6+
"github.com/CodelyTV/go-hexagonal_http_api-course/04-03-command-bus/internal/fetching"
67

78
"github.com/CodelyTV/go-hexagonal_http_api-course/04-03-command-bus/internal/creating"
89
"github.com/CodelyTV/go-hexagonal_http_api-course/04-03-command-bus/internal/platform/bus/inmemory"
@@ -30,16 +31,20 @@ func Run() error {
3031
}
3132

3233
var (
33-
commandBus = inmemory.NewCommandBus()
34+
bus = inmemory.NewCommandBus()
3435
)
3536

3637
courseRepository := mysql.NewCourseRepository(db)
3738

3839
creatingCourseService := creating.NewCourseService(courseRepository)
40+
fetchingCourseService := fetching.NewCourseFetchingService(courseRepository)
3941

4042
createCourseCommandHandler := creating.NewCourseCommandHandler(creatingCourseService)
41-
commandBus.Register(creating.CourseCommandType, createCourseCommandHandler)
43+
fetchingCourseQueryHandler := fetching.NewCourseQueryHandler(fetchingCourseService)
4244

43-
srv := server.New(host, port, commandBus)
45+
bus.RegisterCommandHandler(creating.CourseCommandType, createCourseCommandHandler)
46+
bus.RegisterQueryHandler(fetching.CourseQueryType, fetchingCourseQueryHandler)
47+
48+
srv := server.New(host, port, bus)
4449
return srv.Run()
4550
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ type Course struct {
8787
// CourseRepository defines the expected behaviour from a course storage.
8888
type CourseRepository interface {
8989
Save(ctx context.Context, course Course) error
90+
GetAll(ctx context.Context) ([]Course, error)
9091
}
9192

9293
//go:generate mockery --case=snake --outpkg=storagemocks --output=platform/storage/storagemocks --name=CourseRepository

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/creating/service.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ func (s CourseService) CreateCourse(ctx context.Context, id, name, duration stri
2727
}
2828
return s.courseRepository.Save(ctx, course)
2929
}
30+
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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package fetching
2+
3+
import (
4+
"context"
5+
6+
mooc "github.com/CodelyTV/go-hexagonal_http_api-course/04-03-command-bus/internal"
7+
)
8+
9+
// CourseService is the default CourseService interface
10+
// implementation returned by fetching.NewCourseFetchingService.
11+
type FetchingCourseService struct {
12+
courseRepository mooc.CourseRepository
13+
}
14+
15+
// NewCourseService returns the default Service interface implementation.
16+
func NewCourseFetchingService(courseRepository mooc.CourseRepository) FetchingCourseService {
17+
return FetchingCourseService{
18+
courseRepository: courseRepository,
19+
}
20+
}
21+
22+
// CreateCourse implements the creating.CourseService interface.
23+
func (s FetchingCourseService) GetAll(ctx context.Context) ([]mooc.Course, error) {
24+
return s.courseRepository.GetAll(ctx)
25+
}

0 commit comments

Comments
 (0)