Skip to content

Commit 8cbc4fa

Browse files
committed
fixes
1 parent ec05472 commit 8cbc4fa

File tree

14 files changed

+509
-60
lines changed

14 files changed

+509
-60
lines changed

Foundatio.CommandQuery.slnx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
<Project Path="tests/Foundatio.CommandQuery.MongoDB.Tests/Foundatio.CommandQuery.MongoDB.Tests.csproj" Id="29f65eb8-1b16-4ab9-85d4-ee5173137c89" />
99
<Project Path="tests/Foundatio.CommandQuery.Tests/Foundatio.CommandQuery.Tests.csproj" Id="eb09253e-86ea-43c9-9640-d72ddab0de07" />
1010
</Folder>
11+
<Project Path="../Foundatio.Mediator/src/Foundatio.Mediator.Abstractions/Foundatio.Mediator.Abstractions.csproj" />
12+
<Project Path="../Foundatio.Mediator/src/Foundatio.Mediator/Foundatio.Mediator.csproj" />
1113
<Project Path="src/Foundatio.CommandQuery.EntityFramework/Foundatio.CommandQuery.EntityFramework.csproj" Id="94dc262e-6e49-4ad4-9767-7c25a0c4065e" />
1214
<Project Path="src/Foundatio.CommandQuery.MongoDB/Foundatio.CommandQuery.MongoDB.csproj" Id="7252b936-1787-43f3-867c-0f2727abff4a" />
1315
<Project Path="src/Foundatio.CommandQuery/Foundatio.CommandQuery.csproj" Id="1e67f0dc-5ce8-4d5e-a61e-3f92b7aeb512" />

src/Foundatio.CommandQuery.EntityFramework/EntityQueryHandler.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
using Foundatio.CommandQuery.Commands;
44
using Foundatio.CommandQuery.Definitions;
5-
using Foundatio.CommandQuery.Results;
5+
using Foundatio.CommandQuery.Queries;
66
using Foundatio.Mediator;
77

88
using Microsoft.EntityFrameworkCore;
@@ -92,7 +92,7 @@ public virtual async ValueTask<Result<IReadOnlyList<TReadModel>>> HandleAsync(
9292
return Result<IReadOnlyList<TReadModel>>.Success(results);
9393
}
9494

95-
public virtual async ValueTask<QueryResult<TReadModel>> HandleAsync(
95+
public virtual async ValueTask<Result<QueryResult<TReadModel>>> HandleAsync(
9696
QueryEntities<TReadModel> request,
9797
CancellationToken cancellationToken = default)
9898
{
@@ -121,7 +121,7 @@ public virtual async ValueTask<QueryResult<TReadModel>> HandleAsync(
121121

122122
// short circuit if total is zero
123123
if (total == 0)
124-
return new QueryResult<TReadModel>([]);
124+
return Result<QueryResult<TReadModel>>.Success(new());
125125

126126
query = query.Page(queryDefinition.Page.Value, queryDefinition.PageSize.Value);
127127
}
@@ -133,6 +133,12 @@ public virtual async ValueTask<QueryResult<TReadModel>> HandleAsync(
133133
.ToListAsync(cancellationToken)
134134
.ConfigureAwait(false);
135135

136-
return new QueryResult<TReadModel>(results) { Total = total };
136+
var queryResult = new QueryResult<TReadModel>
137+
{
138+
Total = total,
139+
Data = results
140+
};
141+
142+
return Result<QueryResult<TReadModel>>.Success(queryResult);
137143
}
138144
}

src/Foundatio.CommandQuery.MongoDB/EntityQueryHandler.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
using Foundatio.CommandQuery.Commands;
44
using Foundatio.CommandQuery.Definitions;
5-
using Foundatio.CommandQuery.Results;
5+
using Foundatio.CommandQuery.Queries;
66
using Foundatio.Mediator;
77

88
using MongoDB.Driver;
@@ -57,7 +57,7 @@ public virtual async ValueTask<Result<IReadOnlyList<TReadModel>>> HandleAsync(
5757
return Result<IReadOnlyList<TReadModel>>.Success(results);
5858
}
5959

60-
public virtual async ValueTask<QueryResult<TReadModel>> HandleAsync(
60+
public virtual async ValueTask<Result<QueryResult<TReadModel>>> HandleAsync(
6161
QueryEntities<TReadModel> request,
6262
CancellationToken cancellationToken = default)
6363
{
@@ -83,7 +83,7 @@ public virtual async ValueTask<QueryResult<TReadModel>> HandleAsync(
8383

8484
// short circuit if total is zero
8585
if (total == 0)
86-
return new QueryResult<TReadModel>([]);
86+
return Result<QueryResult<TReadModel>>.Success(new());
8787

8888
query = query.Page(queryDefinition.Page.Value, queryDefinition.PageSize.Value);
8989
}
@@ -97,7 +97,13 @@ public virtual async ValueTask<QueryResult<TReadModel>> HandleAsync(
9797
.ToListAsync(cancellationToken)
9898
.ConfigureAwait(false);
9999

100-
return new QueryResult<TReadModel>(results) { Total = total };
100+
var queryResult = new QueryResult<TReadModel>
101+
{
102+
Total = total,
103+
Data = results
104+
};
105+
106+
return Result<QueryResult<TReadModel>>.Success(queryResult);
101107

102108
}
103109
}

src/Foundatio.CommandQuery/Commands/QueryEntities.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using Foundatio.CommandQuery.Abstracts;
55
using Foundatio.CommandQuery.Queries;
6-
using Foundatio.CommandQuery.Results;
76

87
namespace Foundatio.CommandQuery.Commands;
98

src/Foundatio.CommandQuery/Foundatio.CommandQuery.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Foundatio.Mediator.Abstractions" Version="1.0.0-preview9.3" />
10+
<ProjectReference Include="..\..\..\Foundatio.Mediator\src\Foundatio.Mediator.Abstractions\Foundatio.Mediator.Abstractions.csproj" />
1111
</ItemGroup>
1212

1313
</Project>

src/Foundatio.CommandQuery/Results/QueryResult.cs renamed to src/Foundatio.CommandQuery/Queries/QueryResult.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1+
using System.Collections.Immutable;
2+
using System.ComponentModel;
13
using System.Text.Json.Serialization;
24

35
using Foundatio.Mediator;
46

5-
namespace Foundatio.CommandQuery.Results;
7+
using static System.Runtime.InteropServices.JavaScript.JSType;
8+
9+
namespace Foundatio.CommandQuery.Queries;
610

711
/// <summary>
812
/// A result for an entity query.
913
/// </summary>
1014
/// <typeparam name="TReadModel">The type of the read model.</typeparam>
11-
public class QueryResult<TReadModel> : Result<IReadOnlyList<TReadModel>>
15+
public class QueryResult<TReadModel>
1216
{
13-
public QueryResult(IReadOnlyList<TReadModel> results)
14-
: base(results)
15-
{
16-
}
17-
1817
/// <summary>
1918
/// Gets or sets the continuation token for retrieving the next page of results.
2019
/// </summary>
@@ -32,4 +31,11 @@ public QueryResult(IReadOnlyList<TReadModel> results)
3231
[JsonPropertyName("total")]
3332
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
3433
public long? Total { get; set; }
34+
35+
/// <summary>
36+
/// The data returned by the query.
37+
/// </summary>
38+
[JsonPropertyName("data")]
39+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
40+
public IReadOnlyList<TReadModel>? Data { get; set; }
3541
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using System.ComponentModel;
2+
using System.Text.Json.Serialization;
3+
4+
namespace Foundatio.CommandQuery.Results;
5+
6+
/// <summary>
7+
/// Represents an error with a message and associated metadata.
8+
/// </summary>
9+
[ImmutableObject(true)]
10+
public class Error : IError
11+
{
12+
private static readonly int DefaultStatus = 500;
13+
private const string DefaultError = "An unknown error occurred";
14+
15+
public static Error Empty { get; } = new();
16+
17+
/// <summary>
18+
/// Initializes a new instance of the <see cref="Error" /> class.
19+
/// </summary>
20+
public Error() : this(DefaultStatus, DefaultError)
21+
{
22+
}
23+
24+
/// <summary>Initializes a new instance of the <see cref="Error" /> class with the specified error message.</summary>
25+
/// <param name="status">The status code generated by the origin server</param>
26+
/// <param name="message">The error message.</param>
27+
public Error(int status, string message)
28+
{
29+
Status = status;
30+
Message = message;
31+
}
32+
33+
/// <summary>Initializes a new instance of the <see cref="Error" /> class with the specified metadata.</summary>
34+
/// <param name="extensions">The metadata associated with the error.</param>
35+
public Error((string Key, object? Value) extensions)
36+
: this(DefaultStatus, DefaultError, extensions)
37+
{
38+
}
39+
40+
/// <summary>Initializes a new instance of the <see cref="Error" /> class with the specified error message and metadata.</summary>
41+
/// <param name="status">The status code generated by the origin server</param>
42+
/// <param name="message">The error message.</param>
43+
/// <param name="extensions">The metadata associated with the error.</param>
44+
public Error(int status, string message, (string Key, object? Value) extensions)
45+
{
46+
Status = status;
47+
Message = message;
48+
49+
Extensions = new Dictionary<string, object?>
50+
{
51+
[extensions.Key] = extensions.Value
52+
};
53+
}
54+
55+
/// <summary>Initializes a new instance of the <see cref="Error" /> class with the specified metadata.</summary>
56+
/// <param name="extensions">The metadata associated with the error.</param>
57+
public Error(IReadOnlyDictionary<string, object?> extensions)
58+
: this(DefaultStatus, DefaultError, extensions)
59+
{
60+
}
61+
62+
/// <summary>Initializes a new instance of the <see cref="Error" /> class with the specified error message and metadata.</summary>
63+
/// <param name="status">The status code generated by the origin server</param>
64+
/// <param name="message">The error message.</param>
65+
/// <param name="extensions">The metadata associated with the error.</param>
66+
public Error(int status, string message, IReadOnlyDictionary<string, object?> extensions)
67+
{
68+
Status = status;
69+
Message = message;
70+
Extensions = extensions;
71+
}
72+
73+
/// <inheritdoc />
74+
public int Status { get; }
75+
76+
/// <inheritdoc />
77+
public string Message { get; }
78+
79+
/// <inheritdoc />
80+
public IReadOnlyDictionary<string, object?>? Extensions { get; }
81+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace Foundatio.CommandQuery.Results;
2+
3+
/// <summary>Defines an error with a message and associated metadata.</summary>
4+
public interface IError
5+
{
6+
/// <summary>The status code generated by the origin server for this occurrence of the problem.</summary>
7+
int Status { get; }
8+
9+
/// <summary>Gets the error message.</summary>
10+
string Message { get; }
11+
12+
/// <summary>Gets the metadata associated with the error.</summary>
13+
/// <remarks>The metadata is represented as a dictionary of key-value pairs.</remarks>
14+
IReadOnlyDictionary<string, object?>? Extensions { get; }
15+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
namespace Foundatio.CommandQuery.Results;
2+
3+
/// <summary>Defines a result.</summary>
4+
public interface IResult
5+
{
6+
/// <summary>Gets a value indicating whether the result was successful.</summary>
7+
/// <returns><c>true</c> if the result was successful; otherwise, <c>false</c>.</returns>
8+
bool IsSuccess { get; }
9+
10+
/// <summary>Gets a value indicating whether the result failed.</summary>
11+
/// <returns><c>true</c> if the result failed; otherwise, <c>false</c>.</returns>
12+
bool IsFailed { get; }
13+
14+
/// <summary>Gets a collection of errors associated with the result.</summary>
15+
/// <returns>An <see cref="IReadOnlyCollection{T}" /> of <see cref="IError" /> representing the errors.</returns>
16+
IReadOnlyCollection<IError> Errors { get; }
17+
18+
/// <summary>Checks if the result contains an error of the specific type.</summary>
19+
/// <typeparam name="TError">The type of error to check for.</typeparam>
20+
/// <returns><c>true</c> if an error of the specified type is present; otherwise, <c>false</c>.</returns>
21+
bool HasError<TError>() where TError : IError;
22+
}
23+
24+
/// <summary>Defines a result with a value.</summary>
25+
public interface IResult<out TValue> : IResult
26+
{
27+
/// <summary>Gets the value of the result, throwing an exception if the result is failed.</summary>
28+
/// <returns>The value of the result.</returns>
29+
/// <exception cref="InvalidOperationException">Thrown when attempting to get or set the value of a failed result.</exception>
30+
public TValue Value { get; }
31+
}

0 commit comments

Comments
 (0)