Skip to content
This repository was archived by the owner on Sep 3, 2024. It is now read-only.

Commit 4d486d2

Browse files
there is no need to create a Func every time this is invoked
1 parent 3c8ec28 commit 4d486d2

26 files changed

Lines changed: 296 additions & 306 deletions

src/SqlStreamStore.HAL/AppendStreamMiddleware.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ private static MidFunc AppendStream(StreamResource stream) => next => async env
3434
{
3535
var context = new OwinContext(env);
3636

37-
var options = await AppendStreamOptions.Create(context.Request, context.Request.CallCancelled);
37+
var options = await AppendStreamOperation.Create(context.Request, context.Request.CallCancelled);
3838

3939
var response = await stream.AppendMessages(options, context.Request.CallCancelled);
4040

src/SqlStreamStore.HAL/DeleteStreamMiddleware.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ private static MidFunc DeleteStream(StreamResource stream) => next => async env
3939
{
4040
var context = new OwinContext(env);
4141

42-
var options = new DeleteStreamOptions(context.Request);
42+
var options = new DeleteStreamOperation(context.Request);
4343

4444
var response = await stream.Delete(options, context.Request.CallCancelled);
4545

@@ -50,7 +50,7 @@ private static MidFunc DeleteStreamMessage(StreamMessageResource streamMessages)
5050
{
5151
var context = new OwinContext(env);
5252

53-
var options = new DeleteStreamMessageOptions(context.Request);
53+
var options = new DeleteStreamMessageOperation(context.Request);
5454

5555
var response = await streamMessages.DeleteMessage(options, context.Request.CallCancelled);
5656

src/SqlStreamStore.HAL/ReadAllStreamMiddleware.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace SqlStreamStore.HAL
1212

1313
internal static class ReadAllStreamMiddleware
1414
{
15-
public static MidFunc UseStreamStore(IReadonlyStreamStore streamStore)
15+
public static MidFunc UseStreamStore(IStreamStore streamStore)
1616
{
1717
var allStream = new AllStreamResource(streamStore);
1818
var allStreamMessages = new AllStreamMessageResource(streamStore);
@@ -40,7 +40,7 @@ private static MidFunc GetStream(AllStreamResource allStream) => next => async e
4040
{
4141
var context = new OwinContext(env);
4242

43-
var options = new ReadAllStreamOptions(context.Request);
43+
var options = new ReadAllStreamOperation(context.Request);
4444

4545
var response = await allStream.GetPage(options, context.Request.CallCancelled);
4646

@@ -55,7 +55,7 @@ private static MidFunc GetStreamMessage(AllStreamMessageResource allStreamMessag
5555
var context = new OwinContext(env);
5656

5757
var response = await allStreamMessages.GetMessage(
58-
new ReadAllStreamMessageOptions(context.Request),
58+
new ReadAllStreamMessageOperation(context.Request),
5959
context.Request.CallCancelled);
6060

6161
using(new OptionalHeadRequestWrapper(context))

src/SqlStreamStore.HAL/ReadStreamMiddleware.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ private static MidFunc GetStream(StreamResource streams) => next => async env =>
3939
{
4040
var context = new OwinContext(env);
4141

42-
var options = new ReadStreamOptions(context.Request);
42+
var options = new ReadStreamOperation(context.Request);
4343

4444
var response = await streams.GetPage(options, context.Request.CallCancelled);
4545

@@ -53,7 +53,7 @@ private static MidFunc GetStreamMessage(StreamMessageResource streamMessages) =>
5353
{
5454
var context = new OwinContext(env);
5555

56-
var options = new ReadStreamMessageByStreamVersionOptions(context.Request);
56+
var options = new ReadStreamMessageByStreamVersionOperation(context.Request);
5757

5858
var response = await streamMessages.GetMessage(options, context.Request.CallCancelled);
5959

src/SqlStreamStore.HAL/Resources/AllStreamMessageResource.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
internal class AllStreamMessageResource : IResource
1010
{
11-
private readonly IReadonlyStreamStore _streamStore;
11+
private readonly IStreamStore _streamStore;
1212

1313
public HttpMethod[] Options { get; } =
1414
{
@@ -17,26 +17,24 @@ internal class AllStreamMessageResource : IResource
1717
HttpMethod.Options
1818
};
1919

20-
public AllStreamMessageResource(IReadonlyStreamStore streamStore)
20+
public AllStreamMessageResource(IStreamStore streamStore)
2121
{
2222
if(streamStore == null)
2323
throw new ArgumentNullException(nameof(streamStore));
2424
_streamStore = streamStore;
2525
}
2626

2727
public async Task<Response> GetMessage(
28-
ReadAllStreamMessageOptions options,
28+
ReadAllStreamMessageOperation operation,
2929
CancellationToken cancellationToken)
3030
{
31-
var operation = options.GetReadOperation();
32-
3331
var message = await operation.Invoke(_streamStore, cancellationToken);
3432

3533
if(message.MessageId == Guid.Empty)
3634
{
3735
return new Response(
3836
new HALResponse(new HALModelConfig())
39-
.AddLinks(Links.All.Feed(options)),
37+
.AddLinks(Links.All.Feed(operation)),
4038
404);
4139
}
4240

@@ -55,7 +53,7 @@ public async Task<Response> GetMessage(
5553
metadata = message.JsonMetadata
5654
}).AddLinks(
5755
Links.All.SelfAll(message),
58-
Links.All.Feed(options)));
56+
Links.All.Feed(operation)));
5957
}
6058
}
6159
}

src/SqlStreamStore.HAL/Resources/AllStreamResource.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
internal class AllStreamResource : IResource
1212
{
13-
private readonly IReadonlyStreamStore _streamStore;
13+
private readonly IStreamStore _streamStore;
1414

1515
public HttpMethod[] Options { get; } =
1616
{
@@ -19,23 +19,21 @@ internal class AllStreamResource : IResource
1919
HttpMethod.Options
2020
};
2121

22-
public AllStreamResource(IReadonlyStreamStore streamStore)
22+
public AllStreamResource(IStreamStore streamStore)
2323
{
2424
if(streamStore == null)
2525
throw new ArgumentNullException(nameof(streamStore));
2626
_streamStore = streamStore;
2727
}
2828

2929
public async Task<Response> GetPage(
30-
ReadAllStreamOptions options,
30+
ReadAllStreamOperation operation,
3131
CancellationToken cancellationToken)
3232
{
33-
var operation = options.GetReadOperation();
34-
3533
var page = await operation.Invoke(_streamStore, cancellationToken);
3634

3735
var payloads = await Task.WhenAll(page.Messages
38-
.Select(message => options.EmbedPayload
36+
.Select(message => operation.EmbedPayload
3937
? message.GetJsonData(cancellationToken)
4038
: Task.FromResult<string>(null))
4139
.ToArray());
@@ -47,9 +45,9 @@ public async Task<Response> GetPage(
4745
page.NextPosition,
4846
page.IsEnd
4947
})
50-
.AddLinks(Links.All.SelfFeed(options))
51-
.AddLinks(Links.All.Navigation(page, options))
52-
.AddLinks(Links.All.Feed(options))
48+
.AddLinks(Links.All.SelfFeed(operation))
49+
.AddLinks(Links.All.Navigation(page, operation))
50+
.AddLinks(Links.All.Feed(operation))
5351
.AddEmbeddedCollection(
5452
Constants.Relations.Message,
5553
page.Messages.Zip(payloads,
@@ -66,7 +64,7 @@ public async Task<Response> GetPage(
6664
}).AddLinks(
6765
Links.All.Self(message)))));
6866

69-
if(options.FromPositionInclusive == Position.End)
67+
if(operation.FromPositionInclusive == Position.End)
7068
{
7169
var headPosition = page.Messages.Length > 0
7270
? page.Messages[0].Position

src/SqlStreamStore.HAL/Resources/AppendStreamOptions.cs renamed to src/SqlStreamStore.HAL/Resources/AppendStreamOperation.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ namespace SqlStreamStore.HAL.Resources
1010
using Newtonsoft.Json.Linq;
1111
using SqlStreamStore.Streams;
1212

13-
internal class AppendStreamOptions
13+
internal class AppendStreamOperation : IStreamStoreOperation<AppendResult>
1414
{
15-
public static async Task<AppendStreamOptions> Create(IOwinRequest request, CancellationToken ct)
15+
public static async Task<AppendStreamOperation> Create(IOwinRequest request, CancellationToken ct)
1616
{
1717
using(var reader = new JsonTextReader(new StreamReader(request.Body))
1818
{
@@ -24,30 +24,30 @@ public static async Task<AppendStreamOptions> Create(IOwinRequest request, Cance
2424
switch(body)
2525
{
2626
case JArray json:
27-
return new AppendStreamOptions(request, json);
27+
return new AppendStreamOperation(request, json);
2828
case JObject json:
29-
return new AppendStreamOptions(request, json);
29+
return new AppendStreamOperation(request, json);
3030
default:
3131
throw new InvalidOperationException();
3232
}
3333
}
3434
}
3535

36-
private AppendStreamOptions(IOwinRequest request)
36+
private AppendStreamOperation(IOwinRequest request)
3737
{
3838
StreamId = request.Path.Value.Remove(0, 1);
3939

4040
ExpectedVersion = request.GetExpectedVersion();
4141
}
4242

43-
private AppendStreamOptions(IOwinRequest request, JArray body)
43+
private AppendStreamOperation(IOwinRequest request, JArray body)
4444
: this(request)
4545
{
4646
NewStreamMessages = body.Select(ParseNewStreamMessage)
4747
.ToArray();
4848
}
4949

50-
private AppendStreamOptions(IOwinRequest request, JObject body)
50+
private AppendStreamOperation(IOwinRequest request, JObject body)
5151
: this(request, new JArray { body })
5252
{ }
5353

@@ -56,7 +56,7 @@ private static NewStreamMessage ParseNewStreamMessage(JToken newStreamMessage, i
5656
if(!Guid.TryParse(newStreamMessage.Value<string>("messageId"), out var messageId))
5757
{
5858
throw new InvalidAppendRequestException($"'{nameof(messageId)}' at index {index} was improperly formatted.");
59-
};
59+
}
6060
if(messageId == Guid.Empty)
6161
{
6262
throw new InvalidAppendRequestException($"'{nameof(messageId)}' at index {index} was empty.");
@@ -78,7 +78,7 @@ private static NewStreamMessage ParseNewStreamMessage(JToken newStreamMessage, i
7878
public int ExpectedVersion { get; }
7979
public NewStreamMessage[] NewStreamMessages { get; }
8080

81-
public Func<IStreamStore, CancellationToken, Task<AppendResult>> GetAppendOperation()
82-
=> (streamStore, ct) => streamStore.AppendToStream(StreamId, ExpectedVersion, NewStreamMessages, ct);
81+
public Task<AppendResult> Invoke(IStreamStore streamStore, CancellationToken ct)
82+
=> streamStore.AppendToStream(StreamId, ExpectedVersion, NewStreamMessages, ct);
8383
}
8484
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
namespace SqlStreamStore.HAL.Resources
2+
{
3+
using System;
4+
using System.Linq;
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
using Microsoft.Owin;
8+
9+
internal class DeleteStreamMessageOperation : IStreamStoreOperation<Unit>
10+
{
11+
public DeleteStreamMessageOperation(IOwinRequest request)
12+
{
13+
var pieces = request.Path.Value.Split('/').Reverse().Take(2).ToArray();
14+
15+
StreamId = pieces.LastOrDefault();
16+
17+
if(Guid.TryParse(pieces.First(), out var messageId))
18+
{
19+
MessageId = messageId;
20+
}
21+
else
22+
{
23+
StreamVersion = int.Parse(pieces.First());
24+
}
25+
}
26+
27+
public string StreamId { get; }
28+
public int? StreamVersion { get; }
29+
public Guid? MessageId { get; }
30+
31+
public Func<IStreamStore, CancellationToken, Task> GetDeleteOperation()
32+
=> async (streamStore, ct) => { await Invoke(streamStore, ct); };
33+
34+
public async Task<Unit> Invoke(IStreamStore streamStore, CancellationToken ct)
35+
{
36+
var messageId = MessageId ?? (await streamStore.ReadStreamBackwards(
37+
StreamId,
38+
StreamVersion.GetValueOrDefault(-1),
39+
1,
40+
true,
41+
ct))
42+
.Messages.FirstOrDefault(
43+
message => StreamVersion == Streams.StreamVersion.End
44+
|| message.StreamVersion == StreamVersion)
45+
.MessageId;
46+
await streamStore.DeleteMessage(StreamId, messageId, ct);
47+
48+
return Unit.Instance;
49+
}
50+
}
51+
}

src/SqlStreamStore.HAL/Resources/DeleteStreamMessageOptions.cs

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/SqlStreamStore.HAL/Resources/DeleteStreamMetadataOptions.cs

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)