Skip to content

Commit 5daf585

Browse files
erdtsieckjeremydmiller
authored andcommitted
Update HttpChain.cs
1 parent d8e3d1a commit 5daf585

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

src/Http/Wolverine.Http.Marten/AggregateAttribute.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,16 @@ public override Variable Modify(HttpChain chain, ParameterInfo parameter, IServi
149149
return v3;
150150
}
151151

152+
if (chain.FindQuerystringVariable(idType, "id", out var v4))
153+
{
154+
return v4;
155+
}
156+
157+
if (chain.FindQuerystringVariable(idType, $"{AggregateType.Name.ToCamelCase()}Id", out var v5))
158+
{
159+
return v5;
160+
}
161+
152162
return null;
153163
}
154164

src/Http/Wolverine.Http.Tests/Marten/using_aggregate_handler_workflow.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,32 @@ await Scenario(x =>
174174
order.ShouldNotBeNull();
175175
order.Shipped.HasValue.ShouldBeTrue();
176176
}
177+
178+
[Fact]
179+
public async Task use_aggregate_in_endpoint_from_query_param_in_url()
180+
{
181+
var result1 = await Scenario(x =>
182+
{
183+
x.Post.Json(new StartOrder(["Socks", "Shoes", "Shirt"])).ToUrl("/orders/create");
184+
});
185+
186+
var status1 = result1.ReadAsJson<OrderStatus>();
187+
status1.ShouldNotBeNull();
188+
189+
await Scenario(x =>
190+
{
191+
x.Post.Url($"/orders/ship/from-query?id={status1.OrderId}");
192+
193+
x.StatusCodeShouldBe(204);
194+
});
195+
196+
await using var session = Store.LightweightSession();
197+
198+
var order = await session.Events.AggregateStreamAsync<Order>(status1.OrderId);
199+
200+
order.ShouldNotBeNull();
201+
order.Shipped.HasValue.ShouldBeTrue();
202+
}
177203

178204
[Fact]
179205
public async Task use_stream_collision_policy()

src/Http/Wolverine.Http/HttpChain.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,23 @@ private void applyMetadata()
418418

419419
return variable;
420420
}
421+
422+
public bool FindQuerystringVariable(Type variableType, string routeOrParameterName, [NotNullWhen(true)]out Variable? variable)
423+
{
424+
var matched = Method.Method.GetParameters()
425+
.FirstOrDefault(x => x.ParameterType == variableType && x.Name != null && x.Name.EqualsIgnoreCase(routeOrParameterName));
426+
if (matched is not null)
427+
{
428+
variable = TryFindOrCreateQuerystringValue(matched);
429+
if (variable is not null)
430+
{
431+
return true;
432+
}
433+
}
434+
435+
variable = null;
436+
return false;
437+
}
421438

422439
public HttpElementVariable? TryFindOrCreateQuerystringValue(ParameterInfo parameter)
423440
{

src/Http/WolverineWebApi/Marten/Orders.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,20 @@ public static OrderShipped Ship4([Aggregate] Order order)
172172
return new OrderShipped();
173173
}
174174

175+
#region sample_using_aggregate_attribute_query_parameter
176+
177+
[WolverinePost("/orders/ship/from-query"), EmptyResponse]
178+
// The OrderShipped return value is treated as an event being posted
179+
// to a Marten even stream
180+
// instead of as the HTTP response body because of the presence of
181+
// the [EmptyResponse] attribute
182+
public static OrderShipped ShipFromQuery([FromQuery] Guid id, [Aggregate] Order order)
183+
{
184+
return new OrderShipped();
185+
}
186+
187+
#endregion
188+
175189
[Transactional]
176190
[WolverinePost("/orders/create")]
177191
public static OrderStatus StartOrder(StartOrder command, IDocumentSession session)

0 commit comments

Comments
 (0)