1
1
using Microsoft . AspNetCore . Http ;
2
2
using Microsoft . AspNetCore . Mvc . Controllers ;
3
3
using Microsoft . AspNetCore . Mvc . Filters ;
4
+ using NetCore2Blockly . ExtensionMethods ;
4
5
using System ;
5
6
using System . Collections . Generic ;
6
7
using System . Diagnostics ;
@@ -13,6 +14,7 @@ namespace NetCore2Blockly
13
14
{
14
15
public class BlocklyActionRegisterFilter : IActionFilter
15
16
{
17
+
16
18
private readonly GenerateBlocklyFilesHostedService hosted ;
17
19
18
20
public BlocklyActionRegisterFilter ( GenerateBlocklyFilesHostedService hosted )
@@ -26,43 +28,61 @@ public void OnActionExecuted(ActionExecutedContext context)
26
28
27
29
public void OnActionExecuting ( ActionExecutingContext context )
28
30
{
31
+ return ;
29
32
var req = context . HttpContext . Request ;
30
33
var url = req . Path . Value ;
31
34
var m = req . Method . ToUpper ( ) ;
32
35
var ad = context . ActionDescriptor as ControllerActionDescriptor ;
33
36
34
37
var rv = context . ActionArguments ;
35
-
38
+
36
39
var possibleCandidatesMethod =
37
40
hosted
38
41
. blocklyFileGeneratorWebAPI ?
39
42
. _actionList
40
43
. Where ( it => it . Verb ? . ToUpper ( ) == m )
41
- . Select ( it=> it as ActionInfoFromNetAPI )
44
+ . Select ( it => it as ActionInfoFromNetAPI )
42
45
. Where ( it => ad . ControllerName . ToUpper ( ) == it . ControllerName . ToUpper ( ) )
43
46
. ToArray ( ) ;
44
47
if ( url . StartsWith ( "/" ) )
45
48
url = url . Substring ( 1 ) ;
46
49
ActionInfo ai = null ;
47
- foreach ( var item in possibleCandidatesMethod )
50
+ foreach ( var item in possibleCandidatesMethod )
48
51
{
49
52
var urlFromRoute = item . RelativeRequestUrl ;
50
- foreach ( var kv in rv )
53
+ foreach ( var kv in rv )
51
54
{
52
55
urlFromRoute = urlFromRoute . Replace ( "{" + kv . Key + "}" , kv . Value . ToString ( ) ) ;
53
56
}
54
- if ( urlFromRoute . StartsWith ( "/" ) )
57
+ if ( urlFromRoute . StartsWith ( "/" ) )
55
58
url = url . Substring ( 1 ) ;
56
59
if ( url . ToLower ( ) == urlFromRoute . ToLower ( ) )
57
60
{
58
61
ai = item ;
59
62
break ;
60
63
}
61
64
}
62
- // Generate blocks from ai
63
- //string x = possibleCandidatesMethod.Length.ToString();
64
-
65
-
65
+ if ( ai == null )
66
+ {
67
+ //log not found action in swagger
68
+ return ;
69
+ }
70
+ var blockName = ai . GenerateCommandName ( ) ;
71
+ var block = new StringBuilder ( $ "<block type='{ blockName } '>") ;
72
+ if ( ai . HasParams )
73
+ foreach ( var param in ai . Params )
74
+ {
75
+ if ( ! rv . ContainsKey ( param . Key ) )
76
+ continue ;
77
+ //make the same for other blocks - this works with Get from example 7
78
+ block . Append ( $ "<value name='val_{ param . Key } '>" ) ;
79
+ block . Append ( "<shadow type='math_number'>" ) ;
80
+ block . Append ( $ "<field name='NUM'>{ rv [ param . Key ] } </field>") ;
81
+ block . Append ( "</shadow>" ) ;
82
+ block . Append ( $ "</value>") ;
83
+ }
84
+ block . Append ( "</block>" ) ;
85
+ Console . WriteLine ( block ) ;
66
86
}
67
87
}
68
88
//class BlocklyRegisterMiddleware : IMiddleware
0 commit comments