66
77from ellar .constants import NOT_SET
88from ellar .di import RequestScope , injectable
9- from ellar .helper import get_name
109
1110from .model import ControllerBase , ControllerType
1211from .router import ControllerRouter
1312
14- if t .TYPE_CHECKING :
13+ if t .TYPE_CHECKING : # pragma: no cover
1514 from ellar .core .guard import GuardCanActivate
1615 from ellar .core .routing .base import RouteOperationBase
1716
1817
19- class MissingAPIControllerDecoratorException (Exception ):
20- pass
21-
22-
2318def get_route_functions (cls : t .Type ) -> t .Iterable ["RouteOperationBase" ]:
2419 from ellar .core .routing .base import RouteOperationBase
2520
@@ -28,20 +23,11 @@ def get_route_functions(cls: t.Type) -> t.Iterable["RouteOperationBase"]:
2823 yield method
2924
3025
31- def compute_api_route_function (
32- base_cls : t .Type , controller_instance : "ControllerDecorator"
33- ) -> None :
34- for cls_route_function in get_route_functions (base_cls ):
35- controller_instance .add_route (cls_route_function )
36-
37-
3826class ControllerDecorator :
3927 __slots__ = (
4028 "_controller_class" ,
41- "_routes" ,
42- "_mount" ,
43- "_tag" ,
4429 "_meta" ,
30+ "_router" ,
4531 )
4632
4733 def __init__ (
@@ -73,8 +59,6 @@ def __init__(
7359
7460 # `controller_class`
7561 self ._controller_class : t .Optional [t .Type [ControllerBase ]] = None
76- # `_path_operations`
77- self ._routes : t .Dict [str , BaseRoute ] = {}
7862
7963 self ._meta = dict (
8064 tag = tag ,
@@ -87,11 +71,14 @@ def __init__(
8771 guards = guards or [],
8872 include_in_schema = include_in_schema ,
8973 )
90- self ._mount : t .Optional [ControllerRouter ] = None
74+ self ._router : t .Optional [" ControllerRouter" ] = None
9175
9276 if _controller_class :
9377 self (_controller_class )
9478
79+ def get_meta (self ) -> t .Dict :
80+ return self ._meta
81+
9582 def __call__ (self , cls : t .Type ) -> "ControllerDecorator" :
9683 if type (cls ) is not ControllerType :
9784 # We force the cls to inherit from `ControllerBase` by creating another type.
@@ -106,35 +93,38 @@ def __call__(self, cls: t.Type) -> "ControllerDecorator":
10693 if self ._meta ["path" ] is NOT_SET :
10794 self ._meta ["path" ] = f"/{ tag } "
10895
96+ if not self ._meta ["name" ]:
97+ self ._meta ["name" ] = (
98+ str (self ._controller_class .controller_class_name ())
99+ .lower ()
100+ .replace ("controller" , "" )
101+ )
102+
103+ self ._router = ControllerRouter (
104+ ** self ._meta , # type: ignore
105+ controller_type = self .get_controller_type (),
106+ )
109107 bases = inspect .getmro (cls )
110108 for base_cls in reversed (bases ):
111109 if base_cls not in [ABC , ControllerBase , object ]:
112- compute_api_route_function (base_cls , self )
110+ self . compute_api_route_function (base_cls )
113111
114- cls = injectable (RequestScope )(cls )
112+ injectable (RequestScope )(cls )
115113
116- if not self ._meta ["name" ]:
117- self ._meta ["name" ] = str (cls .__name__ ).lower ().replace ("controller" , "" )
118114 return self
119115
116+ def compute_api_route_function (self , base_cls : t .Type ) -> None :
117+ for cls_route_function in get_route_functions (base_cls ):
118+ self .get_router ().routes .append (cls_route_function )
119+
120120 def get_controller_type (self ) -> t .Type [ControllerBase ]:
121121 assert self ._controller_class , "Controller not properly initialised"
122122 return self ._controller_class
123123
124- def get_mount (self ) -> "ControllerRouter" :
125- if not self ._mount :
126- self ._mount = ControllerRouter (
127- routes = list (self ._routes .values ()),
128- ** self ._meta , # type: ignore
129- controller_type = self .get_controller_type (),
130- )
131- return self ._mount
132-
133- def add_route (self , cls_route_function : "RouteOperationBase" ) -> None :
134- self ._routes [get_name (cls_route_function .endpoint )] = t .cast (
135- BaseRoute , cls_route_function
136- )
124+ def get_router (self ) -> "ControllerRouter" :
125+ assert self ._router , "Controller not properly initialised"
126+ return self ._router
137127
138128 def build_routes (self ) -> t .List [BaseRoute ]:
139- mount = self .get_mount ()
140- return mount .build_routes ()
129+ router = self .get_router ()
130+ return router .build_routes ()
0 commit comments