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

Commit 3bceab2

Browse files
associating schemas with their respective resources
1 parent f2260d5 commit 3bceab2

10 files changed

Lines changed: 27 additions & 18 deletions

File tree

src/SqlStreamStore.HAL/ResourceMethods.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Net.Http;
55
using System.Reflection;
6+
using System.Threading.Tasks;
67

78
internal static class ResourceMethods
89
{
@@ -11,7 +12,8 @@ public static HttpMethod[] Discover<TResource>()
1112
{
1213
var httpMethods = typeof(TResource)
1314
.GetMethods(
14-
(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
15+
BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
16+
.Where(method => method.ReturnType == typeof(Response) || method.ReturnType == typeof(Task<Response>))
1517
.Select(method => new HttpMethod(method.Name.ToUpperInvariant()))
1618
.Concat(new[] { HttpMethod.Options })
1719
.ToList();

src/SqlStreamStore.HAL/Resources/Schemas.cs renamed to src/SqlStreamStore.HAL/Schema.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace SqlStreamStore.HAL.Resources
1+
namespace SqlStreamStore.HAL
22
{
33
using System;
44
using System.Collections.Concurrent;
@@ -8,23 +8,18 @@
88
using Newtonsoft.Json;
99
using Newtonsoft.Json.Linq;
1010

11-
internal static class Schemas
11+
internal class SchemaSet<TResource> where TResource : IResource
1212
{
1313
private static readonly ConcurrentDictionary<string, HALResponse> s_schemas
1414
= new ConcurrentDictionary<string, HALResponse>();
1515

16-
public static HALResponse AppendToStream => GetSchema(nameof(AppendToStream));
17-
public static HALResponse SetStreamMetadata => GetSchema(nameof(SetStreamMetadata));
18-
public static HALResponse DeleteStream => GetSchema(nameof(DeleteStream));
19-
public static HALResponse DeleteStreamMessage => GetSchema(nameof(DeleteStreamMessage));
20-
21-
private static HALResponse GetSchema(string name) => s_schemas.GetOrAdd(name, ReadSchema);
16+
public HALResponse GetSchema(string name) => s_schemas.GetOrAdd(name, ReadSchema);
2217

2318
private static HALResponse ReadSchema(string name)
2419
{
25-
using(Stream stream = typeof(Schemas)
20+
using(Stream stream = typeof(TResource)
2621
.GetTypeInfo().Assembly
27-
.GetManifestResourceStream(typeof(Schemas), $"Schema.{name}.json"))
22+
.GetManifestResourceStream(typeof(TResource), $"Schema.{name}.json"))
2823
{
2924
if(stream == null)
3025
{

src/SqlStreamStore.HAL/SqlStreamStore.HAL.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
<PackageReference Include="SqlStreamStore" Version="1.2.0-*" />
1818
</ItemGroup>
1919
<ItemGroup>
20-
<EmbeddedResource Include="Resources\Schema\*.json" />
20+
<EmbeddedResource Include="**\Schema\*.json">
21+
<Link>%(RecursiveDir)%(Filename)%(Extension)</Link>
22+
</EmbeddedResource>
2123
</ItemGroup>
2224
</Project>

src/SqlStreamStore.HAL/Resources/Schema/DeleteStreamMessage.json renamed to src/SqlStreamStore.HAL/StreamMessage/Schema/DeleteStreamMessage.json

File renamed without changes.

src/SqlStreamStore.HAL/StreamMessage/StreamMessageResource.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ namespace SqlStreamStore.HAL.StreamMessage
44
using System.Threading;
55
using System.Threading.Tasks;
66
using Halcyon.HAL;
7-
using SqlStreamStore.HAL.Resources;
87
using SqlStreamStore.Streams;
98

109
internal class StreamMessageResource : IResource
1110
{
1211
private readonly IStreamStore _streamStore;
12+
private readonly SchemaSet<StreamMessageResource> _schema = new SchemaSet<StreamMessageResource>();
1313

1414
public StreamMessageResource(IStreamStore streamStore)
1515
{
@@ -18,6 +18,8 @@ public StreamMessageResource(IStreamStore streamStore)
1818
_streamStore = streamStore;
1919
}
2020

21+
private HALResponse DeleteStreamMessage => _schema.GetSchema(nameof(DeleteStreamMessage));
22+
2123
public async Task<Response> Get(
2224
ReadStreamMessageByStreamVersionOperation operation,
2325
CancellationToken cancellationToken)
@@ -71,7 +73,7 @@ public async Task<Response> Get(
7173
})
7274
.AddEmbeddedResource(
7375
Constants.Relations.Delete,
74-
Schemas.DeleteStreamMessage)
76+
DeleteStreamMessage)
7577
.AddLinks(links))
7678
{
7779
Headers =

src/SqlStreamStore.HAL/Resources/Schema/SetStreamMetadata.json renamed to src/SqlStreamStore.HAL/StreamMetadata/Schema/SetStreamMetadata.json

File renamed without changes.

src/SqlStreamStore.HAL/StreamMetadata/StreamMetadataResource.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,22 @@ namespace SqlStreamStore.HAL.StreamMetadata
44
using System.Threading;
55
using System.Threading.Tasks;
66
using Halcyon.HAL;
7-
using SqlStreamStore.HAL.Resources;
87

98
internal class StreamMetadataResource : IResource
109
{
1110
private readonly IStreamStore _streamStore;
11+
private readonly SchemaSet<StreamMetadataResource> _schema;
1212

1313
public StreamMetadataResource(IStreamStore streamStore)
1414
{
1515
if(streamStore == null)
1616
throw new ArgumentNullException(nameof(streamStore));
1717
_streamStore = streamStore;
18+
_schema = new SchemaSet<StreamMetadataResource>();
1819
}
1920

21+
private HALResponse SetStreamMetadata => _schema.GetSchema(nameof(SetStreamMetadata));
22+
2023
public async Task<Response> Get(
2124
GetStreamMetadataOperation operation,
2225
CancellationToken cancellationToken)
@@ -39,7 +42,7 @@ public async Task<Response> Get(
3942
.MetadataNavigation(operation))
4043
.AddEmbeddedResource(
4144
Constants.Relations.Metadata,
42-
Schemas.SetStreamMetadata),
45+
SetStreamMetadata),
4346
result.MetadataStreamVersion >= 0 ? 200 : 404)
4447
{
4548
Headers =

src/SqlStreamStore.HAL/Resources/Schema/AppendToStream.json renamed to src/SqlStreamStore.HAL/Streams/Schema/AppendToStream.json

File renamed without changes.

src/SqlStreamStore.HAL/Resources/Schema/DeleteStream.json renamed to src/SqlStreamStore.HAL/Streams/Schema/DeleteStream.json

File renamed without changes.

src/SqlStreamStore.HAL/Streams/StreamResource.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,20 @@ internal class StreamResource : IResource
1212
{
1313
private readonly IStreamStore _streamStore;
1414
private readonly string _relativePathToRoot;
15+
private readonly SchemaSet<StreamResource> _schema;
1516

1617
public StreamResource(IStreamStore streamStore)
1718
{
1819
if(streamStore == null)
1920
throw new ArgumentNullException(nameof(streamStore));
2021
_streamStore = streamStore;
2122
_relativePathToRoot = "../";
23+
_schema = new SchemaSet<StreamResource>();
2224
}
2325

26+
private HALResponse AppendToStream => _schema.GetSchema(nameof(AppendToStream));
27+
private HALResponse DeleteStream => _schema.GetSchema(nameof(DeleteStream));
28+
2429
public async Task<Response> Post(
2530
AppendStreamOperation operation,
2631
CancellationToken cancellationToken)
@@ -81,10 +86,10 @@ public async Task<Response> Get(ReadStreamOperation operation, CancellationToken
8186
.StreamsNavigation(page, operation))
8287
.AddEmbeddedResource(
8388
Constants.Relations.AppendToStream,
84-
Schemas.AppendToStream)
89+
AppendToStream)
8590
.AddEmbeddedResource(
8691
Constants.Relations.Delete,
87-
Schemas.DeleteStream)
92+
DeleteStream)
8893
.AddEmbeddedCollection(
8994
Constants.Relations.Message,
9095
streamMessages.Zip(

0 commit comments

Comments
 (0)