-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Preparations for implementing IQueryable<TModel> with SetContexts
- Loading branch information
1 parent
6c97598
commit 18195e9
Showing
77 changed files
with
463 additions
and
232 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/BitzArt.Flux.Json/Exceptions/FluxItemNotFoundException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace BitzArt.Flux; | ||
|
||
internal class FluxItemNotFoundException<TModel> : Exception | ||
{ | ||
public FluxItemNotFoundException(object? id) : base($"{typeof(TModel).Name} with key {id} was not found") | ||
{ } | ||
} |
File renamed without changes.
File renamed without changes.
7 changes: 7 additions & 0 deletions
7
src/BitzArt.Flux.Json/Exceptions/FluxJsonMissingDataException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace BitzArt.Flux; | ||
|
||
internal class FluxJsonMissingDataException : Exception | ||
{ | ||
public FluxJsonMissingDataException() : base("Missing set data. Consider populating the set with json data when configuring Flux.") | ||
{ } | ||
} |
8 changes: 8 additions & 0 deletions
8
src/BitzArt.Flux.Json/Exceptions/FluxKeyPropertyExpressionMissingException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace BitzArt.Flux; | ||
|
||
internal class FluxKeyPropertyExpressionMissingException<TModel> : Exception | ||
{ | ||
public FluxKeyPropertyExpressionMissingException() : base($"KeyPropertyExpression is required for {typeof(TModel).Name}. Consider using .WithKey() when configuring a Set.") | ||
{ | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...Builder/Interfaces/IFluxJsonSetBuilder.cs → ...ux.Json/Interfaces/IFluxJsonSetBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
42 changes: 42 additions & 0 deletions
42
src/BitzArt.Flux.Json/Models/FluxJsonSetContext{TModel,TKey}.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace BitzArt.Flux; | ||
|
||
internal class FluxJsonSetContext<TModel, TKey> : FluxJsonSetContext<TModel>, IFluxSetContext<TModel, TKey> | ||
where TModel : class | ||
{ | ||
// ================ Flux internal wiring ================ | ||
|
||
internal new FluxJsonSetOptions<TModel, TKey> SetOptions | ||
{ | ||
get => (FluxJsonSetOptions<TModel, TKey>)_setOptions; | ||
set => _setOptions = value; | ||
} | ||
|
||
// ==================== Constructor ==================== | ||
|
||
public FluxJsonSetContext(FluxJsonServiceOptions serviceOptions, ILogger logger, FluxJsonSetOptions<TModel, TKey> setOptions) | ||
: base(serviceOptions, logger, setOptions) | ||
{ | ||
SetOptions = setOptions; | ||
} | ||
|
||
// ============== Methods implementation ============== | ||
|
||
public override Task<TModel> GetAsync(object? id, params object[]? parameters) => GetAsync((TKey?)id, parameters); | ||
|
||
public Task<TModel> GetAsync(TKey? id, params object[]? parameters) | ||
{ | ||
_logger.LogInformation("Get {type}[{id}]", typeof(TModel).Name, id is not null ? id.ToString() : "_"); | ||
|
||
var existingItem = SetOptions.Items!.FirstOrDefault(item => | ||
{ | ||
if (SetOptions.KeyPropertyExpression is null) throw new FluxKeyPropertyExpressionMissingException<TModel>(); | ||
|
||
var itemId = SetOptions.KeyPropertyExpression.Compile().Invoke(item); | ||
return Equals(itemId, id); | ||
}) ?? throw new FluxItemNotFoundException<TModel>(id); | ||
|
||
return Task.FromResult(existingItem); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
15 changes: 1 addition & 14 deletions
15
...Json/Context/Models/FluxJsonSetOptions.cs → ...ptions/FluxJsonSetOptions{TModel,TKey}.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/BitzArt.Flux.Json/Options/FluxJsonSetOptions{TModel}.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System.Linq.Expressions; | ||
|
||
namespace BitzArt.Flux; | ||
|
||
public class FluxJsonSetOptions<TModel> | ||
where TModel : class | ||
{ | ||
public ICollection<TModel>? Items { get; set; } | ||
|
||
protected Expression<Func<TModel, object>>? _keyPropertyExpression; | ||
|
||
public Expression<Func<TModel, object>>? KeyPropertyExpression | ||
{ | ||
get => _keyPropertyExpression; | ||
set => _keyPropertyExpression = value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/BitzArt.Flux.REST/Exceptions/FluxRestKeyNotFoundException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace BitzArt.Flux; | ||
|
||
internal class FluxRestKeyNotFoundException<TModel> : Exception | ||
{ | ||
private static readonly string Msg = $"Unable to find TKey for type '{typeof(TModel).Name}'. Consider specifying a key when registering the set."; | ||
public FluxRestKeyNotFoundException() : base(Msg) { } | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...Builder/Interfaces/IFluxRestSetBuilder.cs → ...ux.REST/Interfaces/IFluxRestSetBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/BitzArt.Flux.REST/Models/FluxRestSetBuilder{TModel, TKey}.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace BitzArt.Flux; | ||
|
||
internal class FluxRestSetBuilder<TModel, TKey> : FluxRestSetBuilder<TModel>, IFluxRestSetBuilder<TModel, TKey> | ||
where TModel : class | ||
{ | ||
public new FluxRestSetOptions<TModel, TKey> SetOptions { get; set; } | ||
|
||
public FluxRestSetBuilder(IFluxRestServiceBuilder serviceBuilder) : base(serviceBuilder) | ||
{ | ||
SetOptions = new(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/BitzArt.Flux.REST/Models/FluxRestSetContext{TModel,TKey}.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace BitzArt.Flux; | ||
|
||
internal class FluxRestSetContext<TModel, TKey> : FluxRestSetContext<TModel>, IFluxSetContext<TModel, TKey> | ||
where TModel : class | ||
{ | ||
// ================ Flux internal wiring ================ | ||
|
||
internal new FluxRestSetOptions<TModel, TKey> SetOptions | ||
{ | ||
get => (FluxRestSetOptions<TModel, TKey>)_setOptions; | ||
set => _setOptions = value; | ||
} | ||
|
||
// ==================== Constructor ==================== | ||
|
||
public FluxRestSetContext(HttpClient httpClient, FluxRestServiceOptions serviceOptions, ILogger logger, FluxRestSetOptions<TModel, TKey> setOptions) | ||
: base(httpClient, serviceOptions, logger, setOptions) | ||
{ | ||
SetOptions = setOptions; | ||
} | ||
|
||
// ============== Data methods implementation ============== | ||
|
||
public override Task<TModel> GetAsync(object? id, params object[]? parameters) => GetAsync((TKey?)id, parameters); | ||
|
||
public async Task<TModel> GetAsync(TKey? id, params object[]? parameters) | ||
{ | ||
string idEndpoint; | ||
|
||
bool handleParameters = false; | ||
if (SetOptions.GetIdEndpointAction is not null) | ||
{ | ||
idEndpoint = SetOptions.GetIdEndpointAction(id, parameters); | ||
} | ||
else | ||
{ | ||
idEndpoint = SetOptions.Endpoint is not null ? Path.Combine(SetOptions.Endpoint, id!.ToString()!) : id!.ToString()!; | ||
handleParameters = true; | ||
} | ||
var parse = GetFullPath(idEndpoint, handleParameters, parameters); | ||
|
||
_logger.LogInformation("Get {type}[{id}]: {route}{parsingLog}", typeof(TModel).Name, id!.ToString(), parse.Result, parse.Log); | ||
|
||
var message = new HttpRequestMessage(HttpMethod.Get, parse.Result); | ||
var result = await HandleRequestAsync<TModel>(message); | ||
|
||
return result; | ||
} | ||
} |
Oops, something went wrong.