flamingo.me/flamingo-commerce/v3@v3.11.0/category/module.go (about)

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