Skip to content

Commit fdf5bec

Browse files
committed
Resolves PR request
1 parent ac625bf commit fdf5bec

File tree

3 files changed

+41
-24
lines changed

3 files changed

+41
-24
lines changed

src/Elders.Cronus/EventStore/PagingOptions.cs

+21-8
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ public PagingOptions(int take, byte[] paginationToken, Order order)
2424

2525
public static PagingOptions Empty() => new PagingOptions();
2626

27-
public string Serialize()
27+
public override string ToString()
2828
{
2929
return Convert.ToBase64String(JsonSerializer.SerializeToUtf8Bytes(this));
3030
}
3131

32-
public static PagingOptions Deserialize(string paginationToken)
32+
public static PagingOptions From(string paginationToken)
3333
{
3434
PagingOptions pagingOptions = Empty();
3535
if (string.IsNullOrEmpty(paginationToken) == false)
@@ -42,19 +42,32 @@ public static PagingOptions Deserialize(string paginationToken)
4242
}
4343
public sealed class Order : ValueObject<Order>
4444
{
45-
Order() { }
46-
47-
Order(string order)
45+
private const string AscendingOrder = "ascending";
46+
private const string DescendingOrder = "descending";
47+
public Order(string order)
4848
{
49-
this.order = order;
49+
this.order = GetOrderType(order);
5050
}
5151

5252
[JsonInclude]
5353
private readonly string order;
5454

55-
public static Order Ascending = new Order("ascending");
55+
public static Order Ascending = new Order(AscendingOrder);
56+
57+
public static Order Descending = new Order(DescendingOrder);
5658

57-
public static Order Descending = new Order("descending");
59+
string GetOrderType(string value)
60+
{
61+
if(string.IsNullOrEmpty(value))
62+
throw new ArgumentNullException(nameof(value));
63+
64+
if (value.Equals(AscendingOrder, StringComparison.OrdinalIgnoreCase))
65+
return Ascending;
66+
else if (value.Equals(DescendingOrder, StringComparison.OrdinalIgnoreCase))
67+
return Descending;
68+
else
69+
throw new InvalidOperationException("Maimuna");
70+
}
5871

5972
public static implicit operator string(Order order)
6073
{

src/Elders.Cronus/Projections/ProjectionQueryOptions.cs

+18-6
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,33 @@
33

44
namespace Elders.Cronus.Projections.Cassandra;
55

6-
public readonly struct ProjectionQueryOptions(IBlobId id, ProjectionVersion version, PagingOptions pagingOptions, DateTimeOffset? asOf, int? batchSize)
6+
public readonly struct ProjectionQueryOptions
77
{
88
private const int DefaultBatchSize = 1000;
99

10+
ProjectionQueryOptions(IBlobId id, ProjectionVersion version, PagingOptions pagingOptions, DateTimeOffset? asOf, int? batchSize)
11+
{
12+
Id = id;
13+
Version = version;
14+
PagingOptions = pagingOptions;
15+
AsOf = asOf;
16+
batchSize ??= DefaultBatchSize;
17+
BatchSize = batchSize.Value;
18+
}
19+
1020
public ProjectionQueryOptions(IBlobId id, ProjectionVersion version, DateTimeOffset asOf) : this(id, version, default, asOf, DefaultBatchSize) { }
1121

1222
public ProjectionQueryOptions(IBlobId id, ProjectionVersion version, PagingOptions options) : this(id, version, options, null, options.Take) { }
1323

14-
public IBlobId Id { get; } = id;
24+
public ProjectionQueryOptions(IBlobId id, ProjectionVersion version) : this(id, version, null, null, DefaultBatchSize) { }
25+
26+
public IBlobId Id { get; }
1527

16-
public ProjectionVersion Version { get; } = version;
28+
public ProjectionVersion Version { get; }
1729

18-
public DateTimeOffset? AsOf { get; } = asOf;
30+
public DateTimeOffset? AsOf { get; }
1931

20-
public PagingOptions PagingOptions { get; } = pagingOptions;
32+
public PagingOptions PagingOptions { get; }
2133

22-
public int BatchSize { get; } = batchSize ?? DefaultBatchSize;
34+
public int BatchSize { get; }
2335
}

src/Elders.Cronus/Projections/ProjectionStream.cs

+2-10
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,12 @@ public ProjectionStream(ProjectionVersion version, IBlobId projectionId, IEnumer
2929
this.events = events;
3030
}
3131

32-
public async Task<T> RestoreFromHistoryAsync<T>(T projection, Order order = null) where T : IProjectionDefinition
32+
public async Task<T> RestoreFromHistoryAsync<T>(T projection) where T : IProjectionDefinition
3333
{
3434
if (events.Any() == false)
3535
return default(T);
3636

37-
IEnumerable<IEvent> eventsOrderedByTimestamp;
38-
if (order is null || order == Order.Ascending)
39-
{
40-
eventsOrderedByTimestamp = events.OrderBy(@event => @event.Timestamp);
41-
}
42-
else
43-
{
44-
eventsOrderedByTimestamp = events.OrderByDescending(@event => @event.Timestamp); // Use load by descening with discretion, it might go bum bam when initizlizing the state, if you dont know what you are doing.
45-
}
37+
IEnumerable<IEvent> eventsOrderedByTimestamp = events.OrderBy(@event => @event.Timestamp);
4638

4739
projection.InitializeState(projectionId, null);
4840
foreach (IEvent @event in eventsOrderedByTimestamp)

0 commit comments

Comments
 (0)