-
Notifications
You must be signed in to change notification settings - Fork 78
/
module.go
127 lines (112 loc) · 3.51 KB
/
module.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package category
import (
"flamingo.me/dingo"
"flamingo.me/flamingo/v3/framework/web"
flamingographql "flamingo.me/graphql"
"flamingo.me/flamingo-commerce/v3/category/application"
"flamingo.me/flamingo-commerce/v3/category/domain"
"flamingo.me/flamingo-commerce/v3/category/infrastructure"
"flamingo.me/flamingo-commerce/v3/category/infrastructure/fake"
"flamingo.me/flamingo-commerce/v3/category/interfaces/controller"
categoryGraphql "flamingo.me/flamingo-commerce/v3/category/interfaces/graphql"
"flamingo.me/flamingo-commerce/v3/product"
productApplication "flamingo.me/flamingo-commerce/v3/product/application"
"flamingo.me/flamingo-commerce/v3/search"
)
// Module registers our profiler
type Module struct {
useCategoryFixedAdapter bool
useFakeService bool
}
// URL to category
func URL(code string) (string, map[string]string) {
return application.URL(code)
}
// URLWithName to category
func URLWithName(code, name string) (string, map[string]string) {
return application.URLWithName(code, web.URLTitle(name))
}
// Inject dependencies
func (m *Module) Inject(
config *struct {
UseCategoryFixedAdapter bool `inject:"config:commerce.category.useCategoryFixedAdapter,optional"`
UseFakeService bool `inject:"config:commerce.category.fakeService.enabled,optional"`
},
) {
if config != nil {
m.useCategoryFixedAdapter = config.UseCategoryFixedAdapter
m.useFakeService = config.UseFakeService
}
}
// Configure the product URL
func (m *Module) Configure(injector *dingo.Injector) {
injector.Bind(new(controller.QueryHandler)).To(controller.QueryHandlerImpl{})
injector.Bind(new(controller.ProductSearchService)).To(productApplication.ProductSearchService{})
if m.useCategoryFixedAdapter {
injector.Bind((*domain.CategoryService)(nil)).To(infrastructure.CategoryServiceFixed{})
}
if m.useFakeService {
injector.Override((*domain.CategoryService)(nil), "").To(fake.CategoryService{}).In(dingo.ChildSingleton)
}
web.BindRoutes(injector, new(routes))
injector.Bind(new(application.RouterRouter)).To(new(web.Router))
injector.BindMulti(new(flamingographql.Service)).To(categoryGraphql.Service{})
}
// Depends on other modules
func (m *Module) Depends() []dingo.Module {
return []dingo.Module{
new(product.Module),
new(search.Module),
}
}
type routes struct {
view *controller.ViewController
entity *controller.Entity
tree *controller.Tree
}
func (r *routes) Inject(view *controller.ViewController, entity *controller.Entity, tree *controller.Tree) {
r.view = view
r.entity = entity
r.tree = tree
}
func (r *routes) Routes(registry *web.RouterRegistry) {
registry.HandleGet("category.view", r.view.Get)
handler, _ := registry.Route("/category/:code/:name.html", "category.view(code, name, *)")
handler.Normalize("name")
registry.MustRoute("/category/:code", "category.view(code, *)")
registry.HandleData("category.tree", r.tree.Data)
registry.HandleData("category", r.entity.Data)
}
// CueConfig defines the category module configuration
func (*Module) CueConfig() string {
return `
commerce: {
CategoryTree :: {
[string]: CategoryTreeNode
}
CategoryTreeNode :: {
code: string
name: string
sort?: number
childs?: CategoryTree
}
category: {
view: {
template: *"category/category" | !=""
teaserTemplate: *"category/teaser" | !=""
}
useCategoryFixedAdapter: bool | *false
if useCategoryFixedAdapter {
categoryServiceFixed: {
tree: CategoryTree
}
}
fakeService: {
enabled: bool | *false
if enabled {
testDataFolder?: string & !=""
}
}
}
}`
}