Skip to content

Commit 1d49c38

Browse files
committed
[#19] [add] impl
1 parent f2358db commit 1d49c38

File tree

5 files changed

+53
-14
lines changed

5 files changed

+53
-14
lines changed

.vscode/settings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"cSpell.words": [
3+
"cref",
4+
"seealso"
5+
]
6+
}

src/Simplify.Web.Postman/Assembly/Collection/PartBuilders/CollectionItemsBuilder.cs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.Linq;
23
using Simplify.Web.Meta;
34
using Simplify.Web.Postman.Models;
45

@@ -23,11 +24,39 @@ public void Build(CollectionModel model)
2324
continue;
2425

2526
foreach (var route in item.ExecParameters!.Routes)
26-
model.Items.Add(BuildCollectionItem(item, route));
27+
BuildCollectionItems(model, 0, BuildRequestCollectionItem(item, route));
2728
}
2829
}
2930

30-
private static CollectionItem BuildCollectionItem(IControllerMetaData metaData, KeyValuePair<HttpMethod, string> route) =>
31+
private static void BuildCollectionItems(CollectionItem currentLevelContainer, int currentLevel, CollectionItem item)
32+
{
33+
// If recursion reached request level
34+
if (currentLevel == item.Request.Url.Path.Count - 1)
35+
{
36+
if (currentLevelContainer.Items == null)
37+
currentLevelContainer.Items = new List<CollectionItem>();
38+
39+
currentLevelContainer.Items.Add(item);
40+
return;
41+
}
42+
43+
// If path recursion not reached request level
44+
45+
var containerName = item.Request.Url.Path[currentLevel];
46+
47+
var container = currentLevelContainer.Items.FirstOrDefault(x => x.Name == containerName);
48+
49+
if (container == null)
50+
currentLevelContainer.Items.Add(container = new CollectionItem
51+
{
52+
Name = containerName,
53+
Items = new List<CollectionItem>()
54+
});
55+
56+
BuildCollectionItems(container, currentLevel + 1, item);
57+
}
58+
59+
private static CollectionItem BuildRequestCollectionItem(IControllerMetaData metaData, KeyValuePair<HttpMethod, string> route) =>
3160
new()
3261
{
3362
Name = BuildName(metaData),

src/Simplify.Web.Postman/Json/JsonSerializer.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Text.Json;
2+
using System.Text.Json.Serialization;
23

34
namespace Simplify.Web.Postman.Json
45
{
@@ -16,7 +17,8 @@ public string Serialize(object model) =>
1617
global::System.Text.Json.JsonSerializer.Serialize(model, new JsonSerializerOptions
1718
{
1819
PropertyNamingPolicy = new LowerCamelCasePolicy(),
19-
WriteIndented = true
20+
WriteIndented = true,
21+
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
2022
});
2123
}
2224
}

src/Simplify.Web.Postman/Models/CollectionItem.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#nullable disable
22

3+
using System.Collections.Generic;
4+
using System.Text.Json.Serialization;
5+
36
namespace Simplify.Web.Postman.Models
47
{
58
/// <summary>
@@ -22,5 +25,14 @@ public class CollectionItem
2225
/// The request.
2326
/// </value>
2427
public Request Request { get; set; }
28+
29+
/// <summary>
30+
/// Gets or sets the items.
31+
/// </summary>
32+
/// <value>
33+
/// The items.
34+
/// </value>
35+
[JsonPropertyName("item")]
36+
public IList<CollectionItem> Items { get; set; }
2537
}
2638
}
Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
#nullable disable
22

3-
using System.Collections.Generic;
43
using System.Text.Json.Serialization;
54

65
namespace Simplify.Web.Postman.Models
76
{
87
/// <summary>
98
/// Provides Postman collection model
109
/// </summary>
11-
public class CollectionModel
10+
public class CollectionModel : CollectionItem
1211
{
1312
/// <summary>
1413
/// Gets or sets the header.
@@ -18,14 +17,5 @@ public class CollectionModel
1817
/// </value>
1918
[JsonPropertyName("info")]
2019
public CollectionHeader Header { get; set; }
21-
22-
/// <summary>
23-
/// Gets or sets the items.
24-
/// </summary>
25-
/// <value>
26-
/// The items.
27-
/// </value>
28-
[JsonPropertyName("item")]
29-
public IList<CollectionItem> Items { get; set; } = new List<CollectionItem>();
3020
}
3121
}

0 commit comments

Comments
 (0)