-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
DemoEF.razor
101 lines (83 loc) · 2.61 KB
/
DemoEF.razor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
@*
// Copyright (c) Alex Reich.
// Licensed under the CC BY 4.0 License.
*@
@page "/DemoEF"
@implements IDisposable
@using RulesEngine.Models
@using RulesEngineEditor.Models
@using Microsoft.EntityFrameworkCore
@using RulesEngineEditor.Data
@inject IDbContextFactory<RulesEngineEditorDbContext> DbFactory
Last Saved: @lastSaved
<RulesEngineEditorPage EditorRulesEngine="re" @bind-WorkflowDatas="WorkflowDatas" MenuButtons="menuButtons" WorkflowsSaved="Save" />
@code {
private RulesEngineEditorDbContext db;
private RulesEngine.RulesEngine re;
private List<WorkflowData> _workflowDatas;
private List<WorkflowData> WorkflowDatas
{
get { return _workflowDatas; }
set
{
_workflowDatas = value;
//add any missing workflows into dbcontext
foreach (var wf in _workflowDatas)
{
if (!db.Workflows.Any(w => w.Id == wf.Id))
db.Workflows.Add(wf);
}
//remove any missing workflows into dbcontext
foreach (var wf in db.Workflows)
{
if (!_workflowDatas.Any(w => w.Id == wf.Id))
db.Workflows.Remove(wf);
}
try
{
if (db.ChangeTracker.HasChanges())
{
db.SaveChanges(true);
RefreshDB();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
DateTime? lastSaved;
enum FooEnum
{
bar = 123
}
public void Dispose()
{
db?.Dispose();
}
void RefreshDB()
{
WorkflowDatas = db.Workflows.Include(i => i.GlobalParams.OrderBy(o => o.Seq)).Include(i => i.Rules.OrderBy(o => o.Seq)).ThenInclude(i => i.Rules.OrderBy(o => o.Seq)).ThenInclude(i => i.LocalParams.OrderBy(o => o.Seq)).ToList();
}
void Save(Workflow[] wfs)
{
lastSaved = DateTime.Now;
db.Workflows.UpdateRange(WorkflowDatas.ToList());
db.SaveChanges();
}
private List<MenuButton> menuButtons = new List<MenuButton>();
protected override void OnInitialized()
{
menuButtons.Add(new MenuButton("NewWorkflows", false));
menuButtons.Add(new MenuButton("SaveWorkflow", true));
var settings = new ReSettings
{
CustomTypes = new Type[] { typeof(FooEnum) }
};
re = new RulesEngine.RulesEngine(new string[0], settings);
db = DbFactory.CreateDbContext();
db.Database.EnsureCreated();
RefreshDB();
}
}