Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to add sections to layouts #597

Merged
merged 3 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions Modules/Layouting/MultiSegmentSupport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,53 @@ public static LayoutBuilder Add(this LayoutBuilder builder, string[] segments, I
return builder.Add(handler);
}

var current = builder;
var last = builder.AddSegments(segments[..^1]);

last.Add(segments[^1], handler);

return builder;
}

for (var i = 0; i < segments.Length - 1; i++)
/// <summary>
/// Adds all the given segments to the layout and returns the
/// last one.
/// </summary>
/// <param name="builder">The layout to add the sections to</param>
/// <param name="segments">The segments to be added</param>
/// <returns>The last created layout</returns>
/// <exception cref="InvalidOperationException">Thrown if there are incompatible handlers already registered at a segment</exception>
public static LayoutBuilder AddSegments(this LayoutBuilder builder, params string[] segments)
{
if (segments.Length == 0)
{
var segment = segments[i];
return builder;
}

var current = builder;

if (current.RoutedHandlers.TryGetValue(segment, out var existing))
foreach (var section in segments)
{
if (current.RoutedHandlers.TryGetValue(section, out var existing))
{
if (existing is LayoutBuilder existingLayout)
{
current = existingLayout;
}
else
{
throw new InvalidOperationException($"Existing segment '{segment}' is no layout");
throw new InvalidOperationException($"Existing section '{section}' is no layout");
}
}
else
{
var newLayout = Layout.Create();

current.Add(segment, newLayout);
current.Add(section, newLayout);
current = newLayout;
}
}

current.Add(segments[^1], handler);

return builder;
return current;
}

}
14 changes: 14 additions & 0 deletions Modules/Layouting/Provider/LayoutBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,20 @@ public LayoutBuilder Add(IConcernBuilder concern)
return this;
}

/// <summary>
/// Creates a new layout and registers it at the given path.
/// </summary>
/// <param name="section">The path of the segment to be added</param>
/// <returns>The newly created segment</returns>
public LayoutBuilder AddSegment(string segment)
{
var child = Layout.Create();

Add(segment, child);

return child;
}

public IHandler Build()
{
var routed = RoutedHandlers.ToDictionary(kv => kv.Key, kv => kv.Value.Build());
Expand Down
16 changes: 16 additions & 0 deletions Testing/Acceptance/Modules/Layouting/MultiSegmentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,20 @@ public void TestSegmentOccupied()
});
}

[TestMethod]
[MultiEngineTest]
public async Task TestEmpty(TestEngine engine)
{
var app = Layout.Create()
.Add([], Content.From(Resource.FromString("Hello Empty!")));

await using var host = await TestHost.RunAsync(app, engine: engine);

using var response = await host.GetResponseAsync();

await response.AssertStatusAsync(HttpStatusCode.OK);

Assert.AreEqual("Hello Empty!", await response.GetContentAsync());
}

}
50 changes: 50 additions & 0 deletions Testing/Acceptance/Modules/Layouting/SegmentTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.Net;
using GenHTTP.Modules.IO;
using GenHTTP.Modules.Layouting;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace GenHTTP.Testing.Acceptance.Modules.Layouting;

[TestClass]
public class SegmentTests
{

[TestMethod]
[MultiEngineTest]
public async Task TestSegment(TestEngine engine)
{
var app = Layout.Create();

var section = app.AddSegment("segment");

section.Index(Content.From(Resource.FromString("Hello World")));

await using var host = await TestHost.RunAsync(app, engine: engine);

using var response = await host.GetResponseAsync("/segment/");

await response.AssertStatusAsync(HttpStatusCode.OK);

Assert.AreEqual("Hello World", await response.GetContentAsync());
}

[TestMethod]
[MultiEngineTest]
public async Task TestMultiSegment(TestEngine engine)
{
var app = Layout.Create();

var section = app.AddSegments(["one", "two"]);

section.Index(Content.From(Resource.FromString("Hello World")));

await using var host = await TestHost.RunAsync(app, engine: engine);

using var response = await host.GetResponseAsync("/one/two/");

await response.AssertStatusAsync(HttpStatusCode.OK);

Assert.AreEqual("Hello World", await response.GetContentAsync());
}

}
Loading