Skip to content

Commit 1874ed1

Browse files
committed
more work at #124
1 parent 35bcdc9 commit 1874ed1

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

src/NetCore2Blockly/NetCore2Blockly/BlocklyActionRegisterFilter.cs

+29-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.AspNetCore.Http;
22
using Microsoft.AspNetCore.Mvc.Controllers;
33
using Microsoft.AspNetCore.Mvc.Filters;
4+
using NetCore2Blockly.ExtensionMethods;
45
using System;
56
using System.Collections.Generic;
67
using System.Diagnostics;
@@ -13,6 +14,7 @@ namespace NetCore2Blockly
1314
{
1415
public class BlocklyActionRegisterFilter : IActionFilter
1516
{
17+
1618
private readonly GenerateBlocklyFilesHostedService hosted;
1719

1820
public BlocklyActionRegisterFilter(GenerateBlocklyFilesHostedService hosted)
@@ -26,43 +28,61 @@ public void OnActionExecuted(ActionExecutedContext context)
2628

2729
public void OnActionExecuting(ActionExecutingContext context)
2830
{
31+
return;
2932
var req = context.HttpContext.Request;
3033
var url = req.Path.Value;
3134
var m = req.Method.ToUpper();
3235
var ad = context.ActionDescriptor as ControllerActionDescriptor;
3336

3437
var rv = context.ActionArguments;
35-
38+
3639
var possibleCandidatesMethod =
3740
hosted
3841
.blocklyFileGeneratorWebAPI?
3942
._actionList
4043
.Where(it => it.Verb?.ToUpper() == m)
41-
.Select(it=>it as ActionInfoFromNetAPI)
44+
.Select(it => it as ActionInfoFromNetAPI)
4245
.Where(it => ad.ControllerName.ToUpper() == it.ControllerName.ToUpper())
4346
.ToArray();
4447
if (url.StartsWith("/"))
4548
url = url.Substring(1);
4649
ActionInfo ai = null;
47-
foreach(var item in possibleCandidatesMethod)
50+
foreach (var item in possibleCandidatesMethod)
4851
{
4952
var urlFromRoute = item.RelativeRequestUrl;
50-
foreach(var kv in rv)
53+
foreach (var kv in rv)
5154
{
5255
urlFromRoute = urlFromRoute.Replace("{" + kv.Key + "}", kv.Value.ToString());
5356
}
54-
if(urlFromRoute.StartsWith("/"))
57+
if (urlFromRoute.StartsWith("/"))
5558
url = url.Substring(1);
5659
if (url.ToLower() == urlFromRoute.ToLower())
5760
{
5861
ai = item;
5962
break;
6063
}
6164
}
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);
6686
}
6787
}
6888
//class BlocklyRegisterMiddleware : IMiddleware

src/NetCore2Blockly/TestBlocklyHtml/Startup.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,13 @@ public void ConfigureServices(IServiceCollection services)
6666
});
6767
});
6868
services.AddOData();
69+
6970
services.AddControllers(
71+
#region blockly optional
7072
config => config.Filters.Add<BlocklyActionRegisterFilter>()
73+
#endregion
7174
)
72-
75+
7376
.AddJsonOptions(options =>
7477
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()));
7578
#region for odata

0 commit comments

Comments
 (0)