-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathITreeDataService.cs
More file actions
282 lines (250 loc) · 11.6 KB
/
ITreeDataService.cs
File metadata and controls
282 lines (250 loc) · 11.6 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
using MongoDB.Driver;
using TreeDataApi.Models;
namespace TreeDataApi.Services
{
public interface ITreeDataService
{
Task<List<FlatTreeNode>> GetFlattenedTreeAsync();
Task<List<FlatTreeNode>> GetFlattenedTreeByParentAsync(string parentId);
Task<TreeNode> CreateNodeAsync(TreeNode node);
Task<bool> UpdateNodeAsync(string id, TreeNode node);
Task<bool> DeleteNodeAsync(string id);
}
public class TreeDataService : ITreeDataService
{
private readonly IMongoCollection<TreeNode> _treeNodes;
private readonly ILogger<TreeDataService> _logger;
public TreeDataService(IMongoDatabase database, ILogger<TreeDataService> logger)
{
_treeNodes = database.GetCollection<TreeNode>("tree_nodes");
_logger = logger;
}
public async Task<List<FlatTreeNode>> GetFlattenedTreeAsync()
{
try
{
// MongoDB Aggregation Pipeline to flatten tree structure
var pipeline = new BsonDocument[]
{
// Stage 1: Match active nodes only
new BsonDocument("$match", new BsonDocument("isActive", true)),
// Stage 2: Add computed fields
new BsonDocument("$addFields", new BsonDocument
{
["parentObjectId"] = new BsonDocument("$cond", new BsonDocument[]
{
new BsonDocument("$ne", new BsonArray { "$parentId", BsonNull.Value }),
new BsonDocument("$toObjectId", "$parentId"),
BsonNull.Value
})
}),
// Stage 3: Lookup to build hierarchy paths
new BsonDocument("$graphLookup", new BsonDocument
{
["from"] = "tree_nodes",
["startWith"] = "$parentObjectId",
["connectFromField"] = "parentObjectId",
["connectToField"] = "_id",
["as"] = "ancestors",
["depthField"] = "depth"
}),
// Stage 4: Lookup children count
new BsonDocument("$lookup", new BsonDocument
{
["from"] = "tree_nodes",
["localField"] = "_id",
["foreignField"] = "parentObjectId",
["as"] = "children"
}),
// Stage 5: Sort ancestors by level for proper path ordering
new BsonDocument("$addFields", new BsonDocument
{
["sortedAncestors"] = new BsonDocument("$sortArray", new BsonDocument
{
["input"] = "$ancestors",
["sortBy"] = new BsonDocument("level", 1)
})
}),
// Stage 6: Project final structure
new BsonDocument("$project", new BsonDocument
{
["_id"] = 1,
["name"] = 1,
["description"] = 1,
["parentId"] = 1,
["level"] = 1,
["sortOrder"] = 1,
["createdDate"] = 1,
["isActive"] = 1,
["nodeType"] = 1,
["hasChildren"] = new BsonDocument("$gt", new BsonArray { new BsonDocument("$size", "$children"), 0 }),
["childCount"] = new BsonDocument("$size", "$children"),
["orgHierarchy"] = new BsonDocument("$concatArrays", new BsonArray
{
new BsonDocument("$map", new BsonDocument
{
["input"] = "$sortedAncestors",
["as"] = "ancestor",
["in"] = new BsonDocument("$toString", "$$ancestor._id")
}),
new BsonArray { new BsonDocument("$toString", "$_id") }
}),
["fullPath"] = new BsonDocument("$concat", new BsonArray
{
new BsonDocument("$reduce", new BsonDocument
{
["input"] = "$sortedAncestors",
["initialValue"] = "",
["in"] = new BsonDocument("$concat", new BsonArray { "$$value", "$$this.name", " > " })
}),
"$name"
})
}),
// Stage 7: Sort by level and sortOrder
new BsonDocument("$sort", new BsonDocument { ["level"] = 1, ["sortOrder"] = 1, ["name"] = 1 })
};
var aggregateResult = await _treeNodes.Aggregate<BsonDocument>(pipeline).ToListAsync();
return aggregateResult.Select(doc => new FlatTreeNode
{
Id = doc["_id"].AsObjectId.ToString(),
Name = doc.GetValue("name", "").AsString,
Description = doc.GetValue("description", "").AsString,
ParentId = doc.GetValue("parentId", BsonNull.Value).IsBsonNull ? null : doc["parentId"].AsString,
Level = doc.GetValue("level", 0).AsInt32,
SortOrder = doc.GetValue("sortOrder", 0).AsInt32,
CreatedDate = doc.GetValue("createdDate", DateTime.MinValue).ToUniversalTime(),
IsActive = doc.GetValue("isActive", true).AsBoolean,
NodeType = doc.GetValue("nodeType", "").AsString,
HasChildren = doc.GetValue("hasChildren", false).AsBoolean,
ChildCount = doc.GetValue("childCount", 0).AsInt32,
OrgHierarchy = doc.GetValue("orgHierarchy", new BsonArray()).AsBsonArray.Select(x => x.AsString).ToList(),
FullPath = doc.GetValue("fullPath", "").AsString,
DisplayName = doc.GetValue("name", "").AsString,
IndentedName = new string(' ', doc.GetValue("level", 0).AsInt32 * 4) + doc.GetValue("name", "").AsString
}).ToList();
}
catch (Exception ex)
{
_logger.LogError(ex, "Error flattening tree data from MongoDB");
throw;
}
}
public async Task<List<FlatTreeNode>> GetFlattenedTreeByParentAsync(string parentId)
{
var filter = string.IsNullOrEmpty(parentId)
? Builders<TreeNode>.Filter.Eq(x => x.ParentId, null)
: Builders<TreeNode>.Filter.Eq(x => x.ParentId, parentId);
var nodes = await _treeNodes.Find(filter & Builders<TreeNode>.Filter.Eq(x => x.IsActive, true))
.SortBy(x => x.SortOrder)
.ThenBy(x => x.Name)
.ToListAsync();
var flatNodes = new List<FlatTreeNode>();
foreach (var node in nodes)
{
flatNodes.Add(await BuildFlatNodeAsync(node));
flatNodes.AddRange(await GetDescendantsAsync(node.Id));
}
return flatNodes;
}
private async Task<FlatTreeNode> BuildFlatNodeAsync(TreeNode node)
{
var orgHierarchy = await BuildHierarchyPathAsync(node.Id);
var childCount = await _treeNodes.CountDocumentsAsync(
Builders<TreeNode>.Filter.Eq(x => x.ParentId, node.Id) &
Builders<TreeNode>.Filter.Eq(x => x.IsActive, true));
return new FlatTreeNode
{
Id = node.Id,
Name = node.Name,
Description = node.Description,
ParentId = node.ParentId,
Level = node.Level,
SortOrder = node.SortOrder,
CreatedDate = node.CreatedDate,
IsActive = node.IsActive,
NodeType = node.NodeType,
HasChildren = childCount > 0,
ChildCount = (int)childCount,
OrgHierarchy = orgHierarchy,
FullPath = await BuildFullPathAsync(node.Id),
DisplayName = node.Name,
IndentedName = new string(' ', node.Level * 4) + node.Name
};
}
private async Task<List<string>> BuildHierarchyPathAsync(string nodeId)
{
var path = new List<string>();
var currentNodeId = nodeId;
while (!string.IsNullOrEmpty(currentNodeId))
{
path.Insert(0, currentNodeId);
var node = await _treeNodes.Find(x => x.Id == currentNodeId).FirstOrDefaultAsync();
currentNodeId = node?.ParentId;
}
return path;
}
private async Task<string> BuildFullPathAsync(string nodeId)
{
var pathNames = new List<string>();
var currentNodeId = nodeId;
while (!string.IsNullOrEmpty(currentNodeId))
{
var node = await _treeNodes.Find(x => x.Id == currentNodeId).FirstOrDefaultAsync();
if (node != null)
{
pathNames.Insert(0, node.Name);
currentNodeId = node.ParentId;
}
else
{
break;
}
}
return string.Join(" > ", pathNames);
}
private async Task<List<FlatTreeNode>> GetDescendantsAsync(string parentId)
{
var descendants = new List<FlatTreeNode>();
var children = await _treeNodes.Find(
Builders<TreeNode>.Filter.Eq(x => x.ParentId, parentId) &
Builders<TreeNode>.Filter.Eq(x => x.IsActive, true))
.SortBy(x => x.SortOrder)
.ToListAsync();
foreach (var child in children)
{
descendants.Add(await BuildFlatNodeAsync(child));
descendants.AddRange(await GetDescendantsAsync(child.Id));
}
return descendants;
}
public async Task<TreeNode> CreateNodeAsync(TreeNode node)
{
node.CreatedDate = DateTime.UtcNow;
node.IsActive = true;
// Auto-calculate level if parent exists
if (!string.IsNullOrEmpty(node.ParentId))
{
var parent = await _treeNodes.Find(x => x.Id == node.ParentId).FirstOrDefaultAsync();
node.Level = parent?.Level + 1 ?? 0;
}
else
{
node.Level = 0;
}
await _treeNodes.InsertOneAsync(node);
return node;
}
public async Task<bool> UpdateNodeAsync(string id, TreeNode node)
{
var result = await _treeNodes.ReplaceOneAsync(x => x.Id == id, node);
return result.ModifiedCount > 0;
}
public async Task<bool> DeleteNodeAsync(string id)
{
// Soft delete - mark as inactive
var update = Builders<TreeNode>.Update.Set(x => x.IsActive, false);
var result = await _treeNodes.UpdateOneAsync(x => x.Id == id, update);
return result.ModifiedCount > 0;
}
}
}