1 /**
2 * Copyright © DiamondMVC 2019
3 * License: MIT (https://github.com/DiamondMVC/Diamond/blob/master/LICENSE)
4 * Author: Jacob Jensen (bausshf)
5 */
6 module diamond.controllers.action;
7 
8 import diamond.core.apptype;
9 
10 static if (isWeb)
11 {
12   import diamond.controllers.status;
13 
14   /// Wrapper for a controller action
15   class Action
16   {
17       private:
18       /// The associated delegate.
19       Status delegate() _delegate;
20 
21       /// The associated function pointer.
22       Status function() _functionPointer;
23 
24       public:
25       /**
26       *   Creates a new controler action.
27       *   Params:
28       *       d = The delegate.
29       */
30       this(Status delegate() d)
31       {
32           _delegate = d;
33       }
34 
35       /**
36       *   Creates a new controler action.
37       *   Params:
38       *       f = The function pointer..
39       */
40       this(Status function() f)
41       {
42           _functionPointer = f;
43       }
44 
45       /**
46       *   Operator overload for using the wrapper as a call.
47       *   Returns:
48       *       The status of the call.
49       */
50       Status opCall()
51       {
52           if (_delegate)
53           {
54             return _delegate();
55           }
56           else if (_functionPointer)
57           {
58             return _functionPointer();
59           }
60 
61           return Status.notFound;
62       }
63   }
64 
65   static if (isWebApi)
66   {
67     /// Wrapper for a controller's generate action
68     class GenerateControllerAction
69     {
70       import diamond.controllers.basecontroller;
71       import diamond.http;
72 
73       private:
74       /// The associated delegate.
75       BaseController delegate(HttpClient) _delegate;
76 
77       /// The associated function pointer.
78       BaseController function(HttpClient) _functionPointer;
79 
80       public:
81       /**
82       *   Creates a new generate controler action.
83       *   Params:
84       *       d = The delegate.
85       */
86       this(BaseController delegate(HttpClient) d)
87       {
88           _delegate = d;
89       }
90 
91       /**
92       *   Creates a new generate controler action.
93       *   Params:
94       *       f = The function pointer..
95       */
96       this(BaseController function(HttpClient) f)
97       {
98           _functionPointer = f;
99       }
100 
101       /**
102       *   Operator overload for using the wrapper as a call.
103       *   Params:
104       *     client =   The client
105       *   Returns:
106       *     The controller.
107       */
108       BaseController opCall(HttpClient client)
109       {
110           if (_delegate)
111           {
112             return _delegate(client);
113           }
114           else if (_functionPointer)
115           {
116             return _functionPointer(client);
117           }
118 
119           return null;
120       }
121     }
122   }
123 }