diff --git a/src/BitzArt.Flux.Json/BitzArt.Flux.Json.csproj b/src/BitzArt.Flux.Json/BitzArt.Flux.Json.csproj index c0c1e91..e28ec79 100644 --- a/src/BitzArt.Flux.Json/BitzArt.Flux.Json.csproj +++ b/src/BitzArt.Flux.Json/BitzArt.Flux.Json.csproj @@ -12,7 +12,7 @@ MIT git https://github.com/BitzArt/Flux - https://github.com/BitzArt/Flux + https://bitzart.github.io/Flux README.md flux-logo-128.png diff --git a/src/BitzArt.Flux.Json/Exceptions/FluxItemNotFoundException.cs b/src/BitzArt.Flux.Json/Exceptions/FluxItemNotFoundException.cs new file mode 100644 index 0000000..bf0eb23 --- /dev/null +++ b/src/BitzArt.Flux.Json/Exceptions/FluxItemNotFoundException.cs @@ -0,0 +1,7 @@ +namespace BitzArt.Flux; + +internal class FluxItemNotFoundException : Exception +{ + public FluxItemNotFoundException(object? id) : base($"{typeof(TModel).Name} with key {id} was not found") + { } +} \ No newline at end of file diff --git a/src/BitzArt.Flux.Json/Builder/Exceptions/FluxJsonDeserializationException.cs b/src/BitzArt.Flux.Json/Exceptions/FluxJsonDeserializationException.cs similarity index 100% rename from src/BitzArt.Flux.Json/Builder/Exceptions/FluxJsonDeserializationException.cs rename to src/BitzArt.Flux.Json/Exceptions/FluxJsonDeserializationException.cs diff --git a/src/BitzArt.Flux.Json/Builder/Exceptions/FluxJsonFileReadException.cs b/src/BitzArt.Flux.Json/Exceptions/FluxJsonFileReadException.cs similarity index 100% rename from src/BitzArt.Flux.Json/Builder/Exceptions/FluxJsonFileReadException.cs rename to src/BitzArt.Flux.Json/Exceptions/FluxJsonFileReadException.cs diff --git a/src/BitzArt.Flux.Json/Exceptions/FluxJsonMissingDataException.cs b/src/BitzArt.Flux.Json/Exceptions/FluxJsonMissingDataException.cs new file mode 100644 index 0000000..ea50396 --- /dev/null +++ b/src/BitzArt.Flux.Json/Exceptions/FluxJsonMissingDataException.cs @@ -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.") + { } +} diff --git a/src/BitzArt.Flux.Json/Exceptions/FluxKeyPropertyExpressionMissingException.cs b/src/BitzArt.Flux.Json/Exceptions/FluxKeyPropertyExpressionMissingException.cs new file mode 100644 index 0000000..b86b6a7 --- /dev/null +++ b/src/BitzArt.Flux.Json/Exceptions/FluxKeyPropertyExpressionMissingException.cs @@ -0,0 +1,8 @@ +namespace BitzArt.Flux; + +internal class FluxKeyPropertyExpressionMissingException : Exception +{ + public FluxKeyPropertyExpressionMissingException() : base($"KeyPropertyExpression is required for {typeof(TModel).Name}. Consider using .WithKey() when configuring a Set.") + { + } +} \ No newline at end of file diff --git a/src/BitzArt.Flux.Json/Builder/Extensions/AddSetExtension.cs b/src/BitzArt.Flux.Json/Extensions/AddSetExtension.cs similarity index 100% rename from src/BitzArt.Flux.Json/Builder/Extensions/AddSetExtension.cs rename to src/BitzArt.Flux.Json/Extensions/AddSetExtension.cs diff --git a/src/BitzArt.Flux.Json/Builder/Extensions/ConfigureJsonExtension.cs b/src/BitzArt.Flux.Json/Extensions/ConfigureJsonExtension.cs similarity index 100% rename from src/BitzArt.Flux.Json/Builder/Extensions/ConfigureJsonExtension.cs rename to src/BitzArt.Flux.Json/Extensions/ConfigureJsonExtension.cs diff --git a/src/BitzArt.Flux.Json/Builder/Extensions/FromJsonExtension.cs b/src/BitzArt.Flux.Json/Extensions/FromJsonExtension.cs similarity index 100% rename from src/BitzArt.Flux.Json/Builder/Extensions/FromJsonExtension.cs rename to src/BitzArt.Flux.Json/Extensions/FromJsonExtension.cs diff --git a/src/BitzArt.Flux.Json/Builder/Extensions/FromJsonFileExtension.cs b/src/BitzArt.Flux.Json/Extensions/FromJsonFileExtension.cs similarity index 100% rename from src/BitzArt.Flux.Json/Builder/Extensions/FromJsonFileExtension.cs rename to src/BitzArt.Flux.Json/Extensions/FromJsonFileExtension.cs diff --git a/src/BitzArt.Flux.Json/Builder/Extensions/UsingJsonExtension.cs b/src/BitzArt.Flux.Json/Extensions/UsingJsonExtension.cs similarity index 100% rename from src/BitzArt.Flux.Json/Builder/Extensions/UsingJsonExtension.cs rename to src/BitzArt.Flux.Json/Extensions/UsingJsonExtension.cs diff --git a/src/BitzArt.Flux.Json/Builder/Extensions/WithBaseFilePathExtension.cs b/src/BitzArt.Flux.Json/Extensions/WithBaseFilePathExtension.cs similarity index 100% rename from src/BitzArt.Flux.Json/Builder/Extensions/WithBaseFilePathExtension.cs rename to src/BitzArt.Flux.Json/Extensions/WithBaseFilePathExtension.cs diff --git a/src/BitzArt.Flux.Json/Builder/Extensions/WithKeyExtension.cs b/src/BitzArt.Flux.Json/Extensions/WithKeyExtension.cs similarity index 100% rename from src/BitzArt.Flux.Json/Builder/Extensions/WithKeyExtension.cs rename to src/BitzArt.Flux.Json/Extensions/WithKeyExtension.cs diff --git a/src/BitzArt.Flux.Json/Builder/Interfaces/IFluxJsonServiceBuilder.cs b/src/BitzArt.Flux.Json/Interfaces/IFluxJsonServiceBuilder.cs similarity index 100% rename from src/BitzArt.Flux.Json/Builder/Interfaces/IFluxJsonServiceBuilder.cs rename to src/BitzArt.Flux.Json/Interfaces/IFluxJsonServiceBuilder.cs diff --git a/src/BitzArt.Flux.Json/Builder/Interfaces/IFluxJsonSetBuilder.cs b/src/BitzArt.Flux.Json/Interfaces/IFluxJsonSetBuilder.cs similarity index 76% rename from src/BitzArt.Flux.Json/Builder/Interfaces/IFluxJsonSetBuilder.cs rename to src/BitzArt.Flux.Json/Interfaces/IFluxJsonSetBuilder.cs index 7f569d1..8c9d962 100644 --- a/src/BitzArt.Flux.Json/Builder/Interfaces/IFluxJsonSetBuilder.cs +++ b/src/BitzArt.Flux.Json/Interfaces/IFluxJsonSetBuilder.cs @@ -1,6 +1,6 @@ namespace BitzArt.Flux; -public interface IFluxJsonSetBuilder : IFluxModelBuilder, IFluxJsonServiceBuilder +public interface IFluxJsonSetBuilder : IFluxJsonServiceBuilder where TModel : class { public FluxJsonSetOptions SetOptions { get; } diff --git a/src/BitzArt.Flux.Json/Builder/Models/FluxJsonServiceBuilder.cs b/src/BitzArt.Flux.Json/Models/FluxJsonServiceBuilder.cs similarity index 100% rename from src/BitzArt.Flux.Json/Builder/Models/FluxJsonServiceBuilder.cs rename to src/BitzArt.Flux.Json/Models/FluxJsonServiceBuilder.cs diff --git a/src/BitzArt.Flux.Json/Factory/Models/FluxJsonServiceFactory.cs b/src/BitzArt.Flux.Json/Models/FluxJsonServiceFactory.cs similarity index 91% rename from src/BitzArt.Flux.Json/Factory/Models/FluxJsonServiceFactory.cs rename to src/BitzArt.Flux.Json/Models/FluxJsonServiceFactory.cs index 1d1a6a8..7a2333f 100644 --- a/src/BitzArt.Flux.Json/Factory/Models/FluxJsonServiceFactory.cs +++ b/src/BitzArt.Flux.Json/Models/FluxJsonServiceFactory.cs @@ -125,15 +125,3 @@ public IFluxSetContext CreateSetContext(IServiceProv return new FluxJsonSetContext(_serviceOptions, logger, options); } } - -internal class SetConfigurationNotFoundException : Exception -{ - public SetConfigurationNotFoundException() : base("Requested Set Configuration was not found.") - { } -} - -internal class SetAlreadyRegisteredException : Exception -{ - public SetAlreadyRegisteredException(string name) : base($"An unnamed Flux Set for a model '{name}' was already registered previously. Consider giving specific names to different sets for this model.") - { } -} \ No newline at end of file diff --git a/src/BitzArt.Flux.Json/Builder/Models/FluxJsonSetBuilder.cs b/src/BitzArt.Flux.Json/Models/FluxJsonSetBuilder.cs similarity index 100% rename from src/BitzArt.Flux.Json/Builder/Models/FluxJsonSetBuilder.cs rename to src/BitzArt.Flux.Json/Models/FluxJsonSetBuilder.cs diff --git a/src/BitzArt.Flux.Json/Models/FluxJsonSetContext{TModel,TKey}.cs b/src/BitzArt.Flux.Json/Models/FluxJsonSetContext{TModel,TKey}.cs new file mode 100644 index 0000000..5865e66 --- /dev/null +++ b/src/BitzArt.Flux.Json/Models/FluxJsonSetContext{TModel,TKey}.cs @@ -0,0 +1,42 @@ +using Microsoft.Extensions.Logging; + +namespace BitzArt.Flux; + +internal class FluxJsonSetContext : FluxJsonSetContext, IFluxSetContext + where TModel : class +{ + // ================ Flux internal wiring ================ + + internal new FluxJsonSetOptions SetOptions + { + get => (FluxJsonSetOptions)_setOptions; + set => _setOptions = value; + } + + // ==================== Constructor ==================== + + public FluxJsonSetContext(FluxJsonServiceOptions serviceOptions, ILogger logger, FluxJsonSetOptions setOptions) + : base(serviceOptions, logger, setOptions) + { + SetOptions = setOptions; + } + + // ============== Methods implementation ============== + + public override Task GetAsync(object? id, params object[]? parameters) => GetAsync((TKey?)id, parameters); + + public Task 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(); + + var itemId = SetOptions.KeyPropertyExpression.Compile().Invoke(item); + return Equals(itemId, id); + }) ?? throw new FluxItemNotFoundException(id); + + return Task.FromResult(existingItem); + } +} \ No newline at end of file diff --git a/src/BitzArt.Flux.Json/Context/Models/FluxJsonSetContext.cs b/src/BitzArt.Flux.Json/Models/FluxJsonSetContext{TModel}.cs similarity index 54% rename from src/BitzArt.Flux.Json/Context/Models/FluxJsonSetContext.cs rename to src/BitzArt.Flux.Json/Models/FluxJsonSetContext{TModel}.cs index 2ffb224..addb472 100644 --- a/src/BitzArt.Flux.Json/Context/Models/FluxJsonSetContext.cs +++ b/src/BitzArt.Flux.Json/Models/FluxJsonSetContext{TModel}.cs @@ -1,11 +1,15 @@ using BitzArt.Pagination; using Microsoft.Extensions.Logging; +using System.Collections; +using System.Linq.Expressions; namespace BitzArt.Flux; -internal class FluxJsonSetContext : IFluxSetContext +internal class FluxJsonSetContext : IFluxSetContext where TModel : class { + // ================ Flux internal wiring ================ + internal readonly FluxJsonServiceOptions ServiceOptions; internal readonly ILogger _logger; @@ -16,13 +20,35 @@ internal virtual FluxJsonSetOptions SetOptions set => _setOptions = value; } + // ==================== Constructor ==================== + public FluxJsonSetContext(FluxJsonServiceOptions serviceOptions, ILogger logger, FluxJsonSetOptions setOptions) { ServiceOptions = serviceOptions; _logger = logger; _setOptions = setOptions; } - + + // ================ Linking parsed data ================ + + public ICollection Items => SetOptions.Items ?? throw new FluxJsonMissingDataException(); + private IQueryable Query => Items.AsQueryable(); + + // ============== IEnumerable implementation ============== + + public IEnumerator GetEnumerator() => Items.GetEnumerator(); + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + + // ============== IQueryable implementation ============== + + public Type ElementType => typeof(TModel); + + public Expression Expression => Query.Expression; + + public IQueryProvider Provider => Query.Provider; + + // ============== Data methods implementation ============== + public virtual Task> GetAllAsync(params object[]? parameters) { _logger.LogInformation("GetAll {type}", typeof(TModel).Name); @@ -57,49 +83,3 @@ public virtual Task GetAsync(object? id, params object[]? parameters) return Task.FromResult(existingItem); } } - -internal class FluxJsonSetContext : FluxJsonSetContext, IFluxSetContext - where TModel : class -{ - internal new FluxJsonSetOptions SetOptions - { - get => (FluxJsonSetOptions)_setOptions; - set => _setOptions = value; - } - - public FluxJsonSetContext(FluxJsonServiceOptions serviceOptions, ILogger logger, FluxJsonSetOptions setOptions) - : base(serviceOptions, logger, setOptions) - { - SetOptions = setOptions; - } - - public override Task GetAsync(object? id, params object[]? parameters) => GetAsync((TKey?)id, parameters); - - public Task 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(); - - var itemId = SetOptions.KeyPropertyExpression.Compile().Invoke(item); - return Equals(itemId, id); - }) ?? throw new FluxItemNotFoundException(id); - - return Task.FromResult(existingItem); - } -} - -internal class FluxItemNotFoundException : Exception -{ - public FluxItemNotFoundException(object? id) : base($"{typeof(TModel).Name} with key {id} was not found") - { } -} - -internal class FluxKeyPropertyExpressionMissingException : Exception -{ - public FluxKeyPropertyExpressionMissingException() : base($"KeyPropertyExpression is required for {typeof(TModel).Name}. Consider using .WithKey() when configuring a Set.") - { - } -} \ No newline at end of file diff --git a/src/BitzArt.Flux.Json/Context/Models/FluxJsonServiceOptions.cs b/src/BitzArt.Flux.Json/Options/FluxJsonServiceOptions.cs similarity index 100% rename from src/BitzArt.Flux.Json/Context/Models/FluxJsonServiceOptions.cs rename to src/BitzArt.Flux.Json/Options/FluxJsonServiceOptions.cs diff --git a/src/BitzArt.Flux.Json/Context/Models/FluxJsonSetOptions.cs b/src/BitzArt.Flux.Json/Options/FluxJsonSetOptions{TModel,TKey}.cs similarity index 75% rename from src/BitzArt.Flux.Json/Context/Models/FluxJsonSetOptions.cs rename to src/BitzArt.Flux.Json/Options/FluxJsonSetOptions{TModel,TKey}.cs index b6c32d1..c8286a7 100644 --- a/src/BitzArt.Flux.Json/Context/Models/FluxJsonSetOptions.cs +++ b/src/BitzArt.Flux.Json/Options/FluxJsonSetOptions{TModel,TKey}.cs @@ -1,20 +1,7 @@ -using System.Linq.Expressions; +using System.Linq.Expressions; namespace BitzArt.Flux; -public class FluxJsonSetOptions - where TModel : class -{ - public ICollection? Items { get; set; } - protected Expression>? _keyPropertyExpression; - - public Expression>? KeyPropertyExpression - { - get => _keyPropertyExpression; - set => _keyPropertyExpression = value; - } -} - public class FluxJsonSetOptions : FluxJsonSetOptions where TModel : class { diff --git a/src/BitzArt.Flux.Json/Options/FluxJsonSetOptions{TModel}.cs b/src/BitzArt.Flux.Json/Options/FluxJsonSetOptions{TModel}.cs new file mode 100644 index 0000000..429fd01 --- /dev/null +++ b/src/BitzArt.Flux.Json/Options/FluxJsonSetOptions{TModel}.cs @@ -0,0 +1,17 @@ +using System.Linq.Expressions; + +namespace BitzArt.Flux; + +public class FluxJsonSetOptions + where TModel : class +{ + public ICollection? Items { get; set; } + + protected Expression>? _keyPropertyExpression; + + public Expression>? KeyPropertyExpression + { + get => _keyPropertyExpression; + set => _keyPropertyExpression = value; + } +} diff --git a/src/BitzArt.Flux.REST/BitzArt.Flux.REST.csproj b/src/BitzArt.Flux.REST/BitzArt.Flux.REST.csproj index 1957975..99f4710 100644 --- a/src/BitzArt.Flux.REST/BitzArt.Flux.REST.csproj +++ b/src/BitzArt.Flux.REST/BitzArt.Flux.REST.csproj @@ -12,7 +12,7 @@ MIT git https://github.com/BitzArt/Flux - https://github.com/BitzArt/Flux + https://bitzart.github.io/Flux README.md flux-logo-128.png diff --git a/src/BitzArt.Flux.REST/Exceptions/FluxRestKeyNotFoundException.cs b/src/BitzArt.Flux.REST/Exceptions/FluxRestKeyNotFoundException.cs new file mode 100644 index 0000000..c3f9b62 --- /dev/null +++ b/src/BitzArt.Flux.REST/Exceptions/FluxRestKeyNotFoundException.cs @@ -0,0 +1,7 @@ +namespace BitzArt.Flux; + +internal class FluxRestKeyNotFoundException : 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) { } +} diff --git a/src/BitzArt.Flux.REST/Builder/Extensions/AddSetExtension.cs b/src/BitzArt.Flux.REST/Extensions/AddSetExtension.cs similarity index 100% rename from src/BitzArt.Flux.REST/Builder/Extensions/AddSetExtension.cs rename to src/BitzArt.Flux.REST/Extensions/AddSetExtension.cs diff --git a/src/BitzArt.Flux.REST/Builder/Extensions/ConfigureHttpClientExtension.cs b/src/BitzArt.Flux.REST/Extensions/ConfigureHttpClientExtension.cs similarity index 100% rename from src/BitzArt.Flux.REST/Builder/Extensions/ConfigureHttpClientExtension.cs rename to src/BitzArt.Flux.REST/Extensions/ConfigureHttpClientExtension.cs diff --git a/src/BitzArt.Flux.REST/Builder/Extensions/ConfigureJsonExtension.cs b/src/BitzArt.Flux.REST/Extensions/ConfigureJsonExtension.cs similarity index 100% rename from src/BitzArt.Flux.REST/Builder/Extensions/ConfigureJsonExtension.cs rename to src/BitzArt.Flux.REST/Extensions/ConfigureJsonExtension.cs diff --git a/src/BitzArt.Flux.REST/Builder/Extensions/UsingRestExtension.cs b/src/BitzArt.Flux.REST/Extensions/UsingRestExtension.cs similarity index 100% rename from src/BitzArt.Flux.REST/Builder/Extensions/UsingRestExtension.cs rename to src/BitzArt.Flux.REST/Extensions/UsingRestExtension.cs diff --git a/src/BitzArt.Flux.REST/Builder/Extensions/WithEndpointExtension.cs b/src/BitzArt.Flux.REST/Extensions/WithEndpointExtension.cs similarity index 100% rename from src/BitzArt.Flux.REST/Builder/Extensions/WithEndpointExtension.cs rename to src/BitzArt.Flux.REST/Extensions/WithEndpointExtension.cs diff --git a/src/BitzArt.Flux.REST/Builder/Extensions/WithIdEndpointExtension.cs b/src/BitzArt.Flux.REST/Extensions/WithIdEndpointExtension.cs similarity index 100% rename from src/BitzArt.Flux.REST/Builder/Extensions/WithIdEndpointExtension.cs rename to src/BitzArt.Flux.REST/Extensions/WithIdEndpointExtension.cs diff --git a/src/BitzArt.Flux.REST/Builder/Extensions/WithPageEndpointExtension.cs b/src/BitzArt.Flux.REST/Extensions/WithPageEndpointExtension.cs similarity index 100% rename from src/BitzArt.Flux.REST/Builder/Extensions/WithPageEndpointExtension.cs rename to src/BitzArt.Flux.REST/Extensions/WithPageEndpointExtension.cs diff --git a/src/BitzArt.Flux.REST/Builder/Interfaces/IFluxRestServiceBuilder.cs b/src/BitzArt.Flux.REST/Interfaces/IFluxRestServiceBuilder.cs similarity index 100% rename from src/BitzArt.Flux.REST/Builder/Interfaces/IFluxRestServiceBuilder.cs rename to src/BitzArt.Flux.REST/Interfaces/IFluxRestServiceBuilder.cs diff --git a/src/BitzArt.Flux.REST/Builder/Interfaces/IFluxRestSetBuilder.cs b/src/BitzArt.Flux.REST/Interfaces/IFluxRestSetBuilder.cs similarity index 76% rename from src/BitzArt.Flux.REST/Builder/Interfaces/IFluxRestSetBuilder.cs rename to src/BitzArt.Flux.REST/Interfaces/IFluxRestSetBuilder.cs index b7a12a7..d9b3a2c 100644 --- a/src/BitzArt.Flux.REST/Builder/Interfaces/IFluxRestSetBuilder.cs +++ b/src/BitzArt.Flux.REST/Interfaces/IFluxRestSetBuilder.cs @@ -1,6 +1,6 @@ namespace BitzArt.Flux; -public interface IFluxRestSetBuilder : IFluxModelBuilder, IFluxRestServiceBuilder +public interface IFluxRestSetBuilder : IFluxRestServiceBuilder where TModel : class { public FluxRestSetOptions SetOptions { get; } diff --git a/src/BitzArt.Flux.REST/Builder/Models/FluxRestServiceBuilder.cs b/src/BitzArt.Flux.REST/Models/FluxRestServiceBuilder.cs similarity index 100% rename from src/BitzArt.Flux.REST/Builder/Models/FluxRestServiceBuilder.cs rename to src/BitzArt.Flux.REST/Models/FluxRestServiceBuilder.cs diff --git a/src/BitzArt.Flux.REST/Factory/Models/FluxRestServiceFactory.cs b/src/BitzArt.Flux.REST/Models/FluxRestServiceFactory.cs similarity index 92% rename from src/BitzArt.Flux.REST/Factory/Models/FluxRestServiceFactory.cs rename to src/BitzArt.Flux.REST/Models/FluxRestServiceFactory.cs index 276e3e8..bced1f0 100644 --- a/src/BitzArt.Flux.REST/Factory/Models/FluxRestServiceFactory.cs +++ b/src/BitzArt.Flux.REST/Models/FluxRestServiceFactory.cs @@ -127,15 +127,3 @@ public IFluxSetContext CreateSetContext(IServiceProv return new FluxRestSetContext(httpClient, _serviceOptions, logger, options); } } - -internal class SetConfigurationNotFoundException : Exception -{ - public SetConfigurationNotFoundException() : base("Requested Set Configuration was not found.") - { } -} - -internal class SetAlreadyRegisteredException : Exception -{ - public SetAlreadyRegisteredException(string name) : base($"An unnamed Flux Set for a model '{name}' was already registered previously. Consider giving specific names to different sets for this model.") - { } -} \ No newline at end of file diff --git a/src/BitzArt.Flux.REST/Models/FluxRestSetBuilder{TModel, TKey}.cs b/src/BitzArt.Flux.REST/Models/FluxRestSetBuilder{TModel, TKey}.cs new file mode 100644 index 0000000..9a39807 --- /dev/null +++ b/src/BitzArt.Flux.REST/Models/FluxRestSetBuilder{TModel, TKey}.cs @@ -0,0 +1,14 @@ +using Microsoft.Extensions.DependencyInjection; + +namespace BitzArt.Flux; + +internal class FluxRestSetBuilder : FluxRestSetBuilder, IFluxRestSetBuilder + where TModel : class +{ + public new FluxRestSetOptions SetOptions { get; set; } + + public FluxRestSetBuilder(IFluxRestServiceBuilder serviceBuilder) : base(serviceBuilder) + { + SetOptions = new(); + } +} diff --git a/src/BitzArt.Flux.REST/Builder/Models/FluxRestSetBuilder.cs b/src/BitzArt.Flux.REST/Models/FluxRestSetBuilder{TModel}.cs similarity index 72% rename from src/BitzArt.Flux.REST/Builder/Models/FluxRestSetBuilder.cs rename to src/BitzArt.Flux.REST/Models/FluxRestSetBuilder{TModel}.cs index e8406be..6c3b25b 100644 --- a/src/BitzArt.Flux.REST/Builder/Models/FluxRestSetBuilder.cs +++ b/src/BitzArt.Flux.REST/Models/FluxRestSetBuilder{TModel}.cs @@ -26,14 +26,3 @@ public FluxRestSetBuilder(IFluxRestServiceBuilder serviceBuilder) SetOptions = new(); } } - -internal class FluxRestSetBuilder : FluxRestSetBuilder, IFluxRestSetBuilder - where TModel : class -{ - public new FluxRestSetOptions SetOptions { get; set; } - - public FluxRestSetBuilder(IFluxRestServiceBuilder serviceBuilder) : base(serviceBuilder) - { - SetOptions = new(); - } -} diff --git a/src/BitzArt.Flux.REST/Models/FluxRestSetContext{TModel,TKey}.cs b/src/BitzArt.Flux.REST/Models/FluxRestSetContext{TModel,TKey}.cs new file mode 100644 index 0000000..bc20307 --- /dev/null +++ b/src/BitzArt.Flux.REST/Models/FluxRestSetContext{TModel,TKey}.cs @@ -0,0 +1,51 @@ +using Microsoft.Extensions.Logging; + +namespace BitzArt.Flux; + +internal class FluxRestSetContext : FluxRestSetContext, IFluxSetContext + where TModel : class +{ + // ================ Flux internal wiring ================ + + internal new FluxRestSetOptions SetOptions + { + get => (FluxRestSetOptions)_setOptions; + set => _setOptions = value; + } + + // ==================== Constructor ==================== + + public FluxRestSetContext(HttpClient httpClient, FluxRestServiceOptions serviceOptions, ILogger logger, FluxRestSetOptions setOptions) + : base(httpClient, serviceOptions, logger, setOptions) + { + SetOptions = setOptions; + } + + // ============== Data methods implementation ============== + + public override Task GetAsync(object? id, params object[]? parameters) => GetAsync((TKey?)id, parameters); + + public async Task 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(message); + + return result; + } +} diff --git a/src/BitzArt.Flux.REST/Context/Models/FluxRestSetContext.cs b/src/BitzArt.Flux.REST/Models/FluxRestSetContext{TModel}.cs similarity index 70% rename from src/BitzArt.Flux.REST/Context/Models/FluxRestSetContext.cs rename to src/BitzArt.Flux.REST/Models/FluxRestSetContext{TModel}.cs index 1f025bf..7e6d6e8 100644 --- a/src/BitzArt.Flux.REST/Context/Models/FluxRestSetContext.cs +++ b/src/BitzArt.Flux.REST/Models/FluxRestSetContext{TModel}.cs @@ -1,5 +1,7 @@ using BitzArt.Pagination; using Microsoft.Extensions.Logging; +using System.Collections; +using System.Linq.Expressions; using System.Text.Json; using System.Web; @@ -8,6 +10,8 @@ namespace BitzArt.Flux; internal class FluxRestSetContext : IFluxSetContext where TModel : class { + // ================ Flux internal wiring ================ + internal readonly HttpClient HttpClient; internal readonly FluxRestServiceOptions ServiceOptions; internal readonly ILogger _logger; @@ -19,6 +23,29 @@ internal virtual FluxRestSetOptions SetOptions set => _setOptions = value; } + // ==================== Constructor ==================== + + public FluxRestSetContext(HttpClient httpClient, FluxRestServiceOptions serviceOptions, ILogger logger, FluxRestSetOptions setOptions) + { + HttpClient = httpClient; + ServiceOptions = serviceOptions; + _logger = logger; + _setOptions = setOptions; + } + + // ============== IEnumerable implementation ============== + + public IEnumerator GetEnumerator() => throw new NotImplementedException(); + IEnumerator IEnumerable.GetEnumerator() => throw new NotImplementedException(); + + // ============== IQueryable implementation ============== + + public Type ElementType => throw new NotImplementedException(); + public Expression Expression => throw new NotImplementedException(); + public IQueryProvider Provider => throw new NotImplementedException(); + + // ============== Data methods implementation ============== + internal RequestUrlParameterParsingResult GetFullPath(string path, bool handleParameters, object[]? parameters = null) { if (ServiceOptions.BaseUrl is null) return new RequestUrlParameterParsingResult(path, string.Empty); @@ -49,20 +76,6 @@ internal async Task HandleRequestAsync(HttpRequestMessage mess } } - private class KeyNotFoundException : Exception - { - private static readonly string Msg = $"Unable to find TKey for type '{typeof(TModel).Name}'. Consider specifying a key when registering the set."; - public KeyNotFoundException() : base(Msg) { } - } - - public FluxRestSetContext(HttpClient httpClient, FluxRestServiceOptions serviceOptions, ILogger logger, FluxRestSetOptions setOptions) - { - HttpClient = httpClient; - ServiceOptions = serviceOptions; - _logger = logger; - _setOptions = setOptions; - } - public virtual async Task> GetAllAsync(params object[]? parameters) { var path = SetOptions.Endpoint is not null ? SetOptions.Endpoint : string.Empty; @@ -105,7 +118,7 @@ public virtual async Task> GetPageAsync(PageRequest pageReque public virtual async Task GetAsync(object? id, params object[]? parameters) { - if (SetOptions.GetIdEndpointAction is null) throw new KeyNotFoundException(); + if (SetOptions.GetIdEndpointAction is null) throw new FluxRestKeyNotFoundException(); var idEndpoint = SetOptions.GetIdEndpointAction(id, parameters); var parse = GetFullPath(idEndpoint, false); @@ -129,48 +142,3 @@ protected string GetEndpoint() return SetOptions.Endpoint; } } - -internal class FluxRestSetContext : FluxRestSetContext, IFluxSetContext - where TModel : class -{ - internal new FluxRestSetOptions SetOptions - { - get => (FluxRestSetOptions)_setOptions; - set - { - _setOptions = value; - } - } - - public FluxRestSetContext(HttpClient httpClient, FluxRestServiceOptions serviceOptions, ILogger logger, FluxRestSetOptions setOptions) - : base(httpClient, serviceOptions, logger, setOptions) - { - SetOptions = setOptions; - } - - public override Task GetAsync(object? id, params object[]? parameters) => GetAsync((TKey?)id, parameters); - - public async Task 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(message); - - return result; - } -} diff --git a/src/BitzArt.Flux.REST/Context/Models/RequestUrlParameterParsingResult.cs b/src/BitzArt.Flux.REST/Models/RequestUrlParameterParsingResult.cs similarity index 100% rename from src/BitzArt.Flux.REST/Context/Models/RequestUrlParameterParsingResult.cs rename to src/BitzArt.Flux.REST/Models/RequestUrlParameterParsingResult.cs diff --git a/src/BitzArt.Flux.REST/Context/Models/FluxRestServiceOptions.cs b/src/BitzArt.Flux.REST/Options/FluxRestServiceOptions.cs similarity index 100% rename from src/BitzArt.Flux.REST/Context/Models/FluxRestServiceOptions.cs rename to src/BitzArt.Flux.REST/Options/FluxRestServiceOptions.cs diff --git a/src/BitzArt.Flux.REST/Context/Models/FluxRestSetOptions.cs b/src/BitzArt.Flux.REST/Options/FluxRestSetOptions{TModel,TKey}.cs similarity index 60% rename from src/BitzArt.Flux.REST/Context/Models/FluxRestSetOptions.cs rename to src/BitzArt.Flux.REST/Options/FluxRestSetOptions{TModel,TKey}.cs index ebed5ca..34985c1 100644 --- a/src/BitzArt.Flux.REST/Context/Models/FluxRestSetOptions.cs +++ b/src/BitzArt.Flux.REST/Options/FluxRestSetOptions{TModel,TKey}.cs @@ -1,23 +1,5 @@ namespace BitzArt.Flux; -public class FluxRestSetOptions - where TModel : class -{ - public string? Endpoint { get; set; } - public string? PageEndpoint { get; set; } - protected Func? _getIdEndpointAction; - public Func? GetIdEndpointAction - { - get => _getIdEndpointAction; - set => _getIdEndpointAction = value; - } - - public FluxRestSetOptions() - { - GetIdEndpointAction = null; - } -} - public class FluxRestSetOptions : FluxRestSetOptions where TModel : class { diff --git a/src/BitzArt.Flux.REST/Options/FluxRestSetOptions{TModel}.cs b/src/BitzArt.Flux.REST/Options/FluxRestSetOptions{TModel}.cs new file mode 100644 index 0000000..8b7de86 --- /dev/null +++ b/src/BitzArt.Flux.REST/Options/FluxRestSetOptions{TModel}.cs @@ -0,0 +1,19 @@ +namespace BitzArt.Flux; + +public class FluxRestSetOptions + where TModel : class +{ + public string? Endpoint { get; set; } + public string? PageEndpoint { get; set; } + protected Func? _getIdEndpointAction; + public Func? GetIdEndpointAction + { + get => _getIdEndpointAction; + set => _getIdEndpointAction = value; + } + + public FluxRestSetOptions() + { + GetIdEndpointAction = null; + } +} \ No newline at end of file diff --git a/src/BitzArt.Flux.REST/Context/Utility/RequestParameterParsingUtility.cs b/src/BitzArt.Flux.REST/Utility/RequestParameterParsingUtility.cs similarity index 100% rename from src/BitzArt.Flux.REST/Context/Utility/RequestParameterParsingUtility.cs rename to src/BitzArt.Flux.REST/Utility/RequestParameterParsingUtility.cs diff --git a/src/BitzArt.Flux/BitzArt.Flux.csproj b/src/BitzArt.Flux/BitzArt.Flux.csproj index 6292295..65a633f 100644 --- a/src/BitzArt.Flux/BitzArt.Flux.csproj +++ b/src/BitzArt.Flux/BitzArt.Flux.csproj @@ -12,7 +12,7 @@ MIT git https://github.com/BitzArt/Flux - https://github.com/BitzArt/Flux + https://bitzart.github.io/Flux README.md flux-logo-128.png diff --git a/src/BitzArt.Flux/Builder/Interfaces/IFluxModelBuilder.cs b/src/BitzArt.Flux/Builder/Interfaces/IFluxModelBuilder.cs deleted file mode 100644 index 75f5d15..0000000 --- a/src/BitzArt.Flux/Builder/Interfaces/IFluxModelBuilder.cs +++ /dev/null @@ -1,5 +0,0 @@ -namespace BitzArt.Flux; - -public interface IFluxModelBuilder : IFluxServiceBuilder -{ -} diff --git a/src/BitzArt.Flux/Builder/Interfaces/IFluxServicePreBuilder.cs b/src/BitzArt.Flux/Builder/Interfaces/IFluxServicePreBuilder.cs deleted file mode 100644 index 7d6f867..0000000 --- a/src/BitzArt.Flux/Builder/Interfaces/IFluxServicePreBuilder.cs +++ /dev/null @@ -1,10 +0,0 @@ -using Microsoft.Extensions.DependencyInjection; - -namespace BitzArt.Flux; - -public interface IFluxServicePreBuilder -{ - internal string? Name { get; set; } - internal IServiceCollection Services { get; } - internal IFluxFactory Factory { get; } -} diff --git a/src/BitzArt.Flux/Context/Interfaces/IFluxContext.cs b/src/BitzArt.Flux/Context/Interfaces/IFluxContext.cs deleted file mode 100644 index b63b085..0000000 --- a/src/BitzArt.Flux/Context/Interfaces/IFluxContext.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace BitzArt.Flux; - -public interface IFluxContext -{ - public IFluxServiceContext Service(string serviceName); - - public IFluxSetContext Set(string? service = null, string? set = null) where TModel : class; - public IFluxSetContext Set(string? service = null, string? set = null) where TModel : class; -} \ No newline at end of file diff --git a/src/BitzArt.Flux/Exceptions/FluxServiceProviderNotFoundException.cs b/src/BitzArt.Flux/Exceptions/FluxServiceProviderNotFoundException.cs new file mode 100644 index 0000000..d230852 --- /dev/null +++ b/src/BitzArt.Flux/Exceptions/FluxServiceProviderNotFoundException.cs @@ -0,0 +1,8 @@ +namespace BitzArt.Flux; + +internal class FluxServiceProviderNotFoundException : Exception +{ + public FluxServiceProviderNotFoundException() + : base("Requested Flux Service Provider was not found.") + { } +} diff --git a/src/BitzArt.Flux/Exceptions/MultipleFluxServiceProviderFoundException.cs b/src/BitzArt.Flux/Exceptions/MultipleFluxServiceProviderFoundException.cs new file mode 100644 index 0000000..404f2b6 --- /dev/null +++ b/src/BitzArt.Flux/Exceptions/MultipleFluxServiceProviderFoundException.cs @@ -0,0 +1,8 @@ +namespace BitzArt.Flux; + +internal class MultipleFluxServiceProviderFoundException : Exception +{ + public MultipleFluxServiceProviderFoundException() + : base("Multiple matching Flux Service Providers were found.") + { } +} \ No newline at end of file diff --git a/src/BitzArt.Flux/Exceptions/SetAlreadyRegisteredException.cs b/src/BitzArt.Flux/Exceptions/SetAlreadyRegisteredException.cs new file mode 100644 index 0000000..df75402 --- /dev/null +++ b/src/BitzArt.Flux/Exceptions/SetAlreadyRegisteredException.cs @@ -0,0 +1,7 @@ +namespace BitzArt.Flux; + +internal class SetAlreadyRegisteredException : Exception +{ + public SetAlreadyRegisteredException(string name) : base($"An unnamed Flux Set for a model '{name}' was already registered previously. Consider giving specific names to different sets for this model.") + { } +} \ No newline at end of file diff --git a/src/BitzArt.Flux/Exceptions/SetConfigurationNotFoundException.cs b/src/BitzArt.Flux/Exceptions/SetConfigurationNotFoundException.cs new file mode 100644 index 0000000..cae83b5 --- /dev/null +++ b/src/BitzArt.Flux/Exceptions/SetConfigurationNotFoundException.cs @@ -0,0 +1,7 @@ +namespace BitzArt.Flux; + +internal class SetConfigurationNotFoundException : Exception +{ + public SetConfigurationNotFoundException() : base("Requested Set Configuration was not found.") + { } +} diff --git a/src/BitzArt.Flux/Builder/Extensions/AddFluxExtension.cs b/src/BitzArt.Flux/Extensions/AddFluxExtension.cs similarity index 84% rename from src/BitzArt.Flux/Builder/Extensions/AddFluxExtension.cs rename to src/BitzArt.Flux/Extensions/AddFluxExtension.cs index 1b82989..840b992 100644 --- a/src/BitzArt.Flux/Builder/Extensions/AddFluxExtension.cs +++ b/src/BitzArt.Flux/Extensions/AddFluxExtension.cs @@ -2,6 +2,10 @@ namespace BitzArt.Flux; +/// +/// Adds Flux to the IServiceCollection. +/// See Configure Flux for more information. +/// public static class AddFluxExtension { public static IServiceCollection AddFlux(this IServiceCollection services, Action configure) diff --git a/src/BitzArt.Flux/Builder/Extensions/AddServiceExtension.cs b/src/BitzArt.Flux/Extensions/AddServiceExtension.cs similarity index 56% rename from src/BitzArt.Flux/Builder/Extensions/AddServiceExtension.cs rename to src/BitzArt.Flux/Extensions/AddServiceExtension.cs index fcff367..f62f5f1 100644 --- a/src/BitzArt.Flux/Builder/Extensions/AddServiceExtension.cs +++ b/src/BitzArt.Flux/Extensions/AddServiceExtension.cs @@ -2,6 +2,10 @@ public static class AddServiceExtension { + /// + /// Adds a Service to the IFluxBuilder. + /// See Configure Flux for more information. + /// public static IFluxServicePreBuilder AddService(this IFluxBuilder builder, string name) { return new FluxServicePreBuilder(builder.Services, builder.Factory, name); diff --git a/src/BitzArt.Flux/Builder/Interfaces/IFluxBuilder.cs b/src/BitzArt.Flux/Interfaces/IFluxBuilder.cs similarity index 54% rename from src/BitzArt.Flux/Builder/Interfaces/IFluxBuilder.cs rename to src/BitzArt.Flux/Interfaces/IFluxBuilder.cs index 3b6baf4..93f1697 100644 --- a/src/BitzArt.Flux/Builder/Interfaces/IFluxBuilder.cs +++ b/src/BitzArt.Flux/Interfaces/IFluxBuilder.cs @@ -2,6 +2,10 @@ namespace BitzArt.Flux; +/// +/// Flux Builder instance. +/// See Configure Flux for more information. +/// public interface IFluxBuilder { internal IFluxFactory Factory { get; } diff --git a/src/BitzArt.Flux/Interfaces/IFluxContext.cs b/src/BitzArt.Flux/Interfaces/IFluxContext.cs new file mode 100644 index 0000000..6e9f93b --- /dev/null +++ b/src/BitzArt.Flux/Interfaces/IFluxContext.cs @@ -0,0 +1,22 @@ +namespace BitzArt.Flux; + +/// +/// Allows access to configured Services and their Sets.
+/// See Use Flux for more information. +///
+public interface IFluxContext +{ + /// + /// Returns a context for a specific Service.
+ /// See Use Flux for more information. + ///
+ public IFluxServiceContext Service(string serviceName); + + /// + /// Returns a context for a specific preconfigured Flux Set with the Service resolved implicitly.
+ /// See Use Flux for more information. + ///
+ public IFluxSetContext Set(string? service = null, string? set = null) where TModel : class; + + public IFluxSetContext Set(string? service = null, string? set = null) where TModel : class; +} \ No newline at end of file diff --git a/src/BitzArt.Flux/Factory/Interfaces/IFluxFactory.cs b/src/BitzArt.Flux/Interfaces/IFluxFactory.cs similarity index 80% rename from src/BitzArt.Flux/Factory/Interfaces/IFluxFactory.cs rename to src/BitzArt.Flux/Interfaces/IFluxFactory.cs index 7625641..c975f69 100644 --- a/src/BitzArt.Flux/Factory/Interfaces/IFluxFactory.cs +++ b/src/BitzArt.Flux/Interfaces/IFluxFactory.cs @@ -1,6 +1,9 @@ namespace BitzArt.Flux; -internal interface IFluxFactory +/// +/// Internal Flux factory. Stores factories for configured services. +/// +public interface IFluxFactory { internal ICollection ServiceContexts { get; } diff --git a/src/BitzArt.Flux/Builder/Interfaces/IFluxServiceBuilder.cs b/src/BitzArt.Flux/Interfaces/IFluxServiceBuilder.cs similarity index 60% rename from src/BitzArt.Flux/Builder/Interfaces/IFluxServiceBuilder.cs rename to src/BitzArt.Flux/Interfaces/IFluxServiceBuilder.cs index 2970f38..4b51599 100644 --- a/src/BitzArt.Flux/Builder/Interfaces/IFluxServiceBuilder.cs +++ b/src/BitzArt.Flux/Interfaces/IFluxServiceBuilder.cs @@ -2,6 +2,10 @@ namespace BitzArt.Flux; +/// +/// Flux Service Builder instance. +/// See Configure Flux for more information. +/// public interface IFluxServiceBuilder { internal IServiceCollection Services { get; } diff --git a/src/BitzArt.Flux/Context/Interfaces/IFluxServiceContext.cs b/src/BitzArt.Flux/Interfaces/IFluxServiceContext.cs similarity index 58% rename from src/BitzArt.Flux/Context/Interfaces/IFluxServiceContext.cs rename to src/BitzArt.Flux/Interfaces/IFluxServiceContext.cs index e2fd656..8150fb1 100644 --- a/src/BitzArt.Flux/Context/Interfaces/IFluxServiceContext.cs +++ b/src/BitzArt.Flux/Interfaces/IFluxServiceContext.cs @@ -1,5 +1,9 @@ namespace BitzArt.Flux; +/// +/// Flux context for a specific configured Service. +/// See Use Flux for more information. +/// public interface IFluxServiceContext { public IFluxSetContext Set(string? name = null) where TModel : class; diff --git a/src/BitzArt.Flux/Factory/Interfaces/IFluxServiceFactory.cs b/src/BitzArt.Flux/Interfaces/IFluxServiceFactory.cs similarity index 91% rename from src/BitzArt.Flux/Factory/Interfaces/IFluxServiceFactory.cs rename to src/BitzArt.Flux/Interfaces/IFluxServiceFactory.cs index 081e489..377d89b 100644 --- a/src/BitzArt.Flux/Factory/Interfaces/IFluxServiceFactory.cs +++ b/src/BitzArt.Flux/Interfaces/IFluxServiceFactory.cs @@ -1,5 +1,8 @@ namespace BitzArt.Flux; +/// +/// Internal Flux Service factory. +/// public interface IFluxServiceFactory { internal string ServiceName { get; } diff --git a/src/BitzArt.Flux/Interfaces/IFluxServicePreBuilder.cs b/src/BitzArt.Flux/Interfaces/IFluxServicePreBuilder.cs new file mode 100644 index 0000000..cb0fdac --- /dev/null +++ b/src/BitzArt.Flux/Interfaces/IFluxServicePreBuilder.cs @@ -0,0 +1,16 @@ +using Microsoft.Extensions.DependencyInjection; + +namespace BitzArt.Flux; + +/// +/// Flux Service Prebuilder instance.
+/// Stores properties prepared for the Flux Service Builder.
+/// Use a Flux Implementation +/// in order to create an actual Flux Service Builder from this. +///
+public interface IFluxServicePreBuilder +{ + internal string? Name { get; set; } + internal IServiceCollection Services { get; } + internal IFluxFactory Factory { get; } +} diff --git a/src/BitzArt.Flux/Context/Interfaces/IFluxSetContext.cs b/src/BitzArt.Flux/Interfaces/IFluxSetContext.cs similarity index 70% rename from src/BitzArt.Flux/Context/Interfaces/IFluxSetContext.cs rename to src/BitzArt.Flux/Interfaces/IFluxSetContext.cs index e7cd820..94e7365 100644 --- a/src/BitzArt.Flux/Context/Interfaces/IFluxSetContext.cs +++ b/src/BitzArt.Flux/Interfaces/IFluxSetContext.cs @@ -2,9 +2,12 @@ namespace BitzArt.Flux; -public interface IFluxSetContext { } - -public interface IFluxSetContext : IFluxSetContext +/// +/// Flux context for a preconfigured data Set.
+/// See Use Flux +/// for more information on how to use it. +///
+public interface IFluxSetContext : IQueryable where TModel : class { public Task> GetAllAsync(params object[]? parameters); diff --git a/src/BitzArt.Flux/Builder/Models/FluxBuilder.cs b/src/BitzArt.Flux/Models/FluxBuilder.cs similarity index 100% rename from src/BitzArt.Flux/Builder/Models/FluxBuilder.cs rename to src/BitzArt.Flux/Models/FluxBuilder.cs diff --git a/src/BitzArt.Flux/Context/Models/FluxContext.cs b/src/BitzArt.Flux/Models/FluxContext.cs similarity index 100% rename from src/BitzArt.Flux/Context/Models/FluxContext.cs rename to src/BitzArt.Flux/Models/FluxContext.cs diff --git a/src/BitzArt.Flux/Factory/Models/FluxFactory.cs b/src/BitzArt.Flux/Models/FluxFactory.cs similarity index 86% rename from src/BitzArt.Flux/Factory/Models/FluxFactory.cs rename to src/BitzArt.Flux/Models/FluxFactory.cs index 40e9c02..d0feb94 100644 --- a/src/BitzArt.Flux/Factory/Models/FluxFactory.cs +++ b/src/BitzArt.Flux/Models/FluxFactory.cs @@ -66,17 +66,3 @@ public IFluxSetContext GetSetContext( return serviceContext.CreateSetContext(services, setName); } } - -internal class FluxServiceProviderNotFoundException : Exception -{ - public FluxServiceProviderNotFoundException() - : base("Requested Flux Service Provider was not found.") - { } -} - -internal class MultipleFluxServiceProviderFoundException : Exception -{ - public MultipleFluxServiceProviderFoundException() - : base("Multiple matching Flux Service Providers were found.") - { } -} \ No newline at end of file diff --git a/src/BitzArt.Flux/Context/Models/FluxServiceContext.cs b/src/BitzArt.Flux/Models/FluxServiceContext.cs similarity index 100% rename from src/BitzArt.Flux/Context/Models/FluxServiceContext.cs rename to src/BitzArt.Flux/Models/FluxServiceContext.cs diff --git a/src/BitzArt.Flux/Builder/Models/FluxServicePreBuilder.cs b/src/BitzArt.Flux/Models/FluxServicePreBuilder.cs similarity index 100% rename from src/BitzArt.Flux/Builder/Models/FluxServicePreBuilder.cs rename to src/BitzArt.Flux/Models/FluxServicePreBuilder.cs diff --git a/src/BitzArt.Flux/Builder/Models/FluxSetSignature.cs b/src/BitzArt.Flux/Models/FluxSetSignature.cs similarity index 100% rename from src/BitzArt.Flux/Builder/Models/FluxSetSignature.cs rename to src/BitzArt.Flux/Models/FluxSetSignature.cs diff --git a/tests/BitzArt.Flux.Json.Tests/Data/test-model.set.json b/tests/BitzArt.Flux.Json.Tests/Data/test-model.set.json index 11dd16c..cb4dba2 100644 --- a/tests/BitzArt.Flux.Json.Tests/Data/test-model.set.json +++ b/tests/BitzArt.Flux.Json.Tests/Data/test-model.set.json @@ -1,32 +1,42 @@ [ { - "id": 1 + "id": 1, + "name": "item 1" }, { - "id": 2 + "id": 2, + "name": "item 2" }, { - "id": 3 + "id": 3, + "name": "item 3" }, { - "id": 4 + "id": 4, + "name": "item 4" }, { - "id": 5 + "id": 5, + "name": "item 5" }, { - "id": 6 + "id": 6, + "name": "item 6" }, { - "id": 7 + "id": 7, + "name": "item 7" }, { - "id": 8 + "id": 8, + "name": "item 8" }, { - "id": 9 + "id": 9, + "name": "item 9" }, { - "id": 10 + "id": 10, + "name": "item 10" } ] \ No newline at end of file diff --git a/tests/BitzArt.Flux.Json.Tests/TestModel.cs b/tests/BitzArt.Flux.Json.Tests/Models/TestModel.cs similarity index 69% rename from tests/BitzArt.Flux.Json.Tests/TestModel.cs rename to tests/BitzArt.Flux.Json.Tests/Models/TestModel.cs index 5617757..7cdf395 100644 --- a/tests/BitzArt.Flux.Json.Tests/TestModel.cs +++ b/tests/BitzArt.Flux.Json.Tests/Models/TestModel.cs @@ -6,4 +6,7 @@ internal class TestModel { [JsonPropertyName("id")] public int? Id { get; set; } + + [JsonPropertyName("name")] + public string? Name { get; set; } } diff --git a/tests/BitzArt.Flux.Json.Tests/TestModelContext.cs b/tests/BitzArt.Flux.Json.Tests/Models/TestModelContext.cs similarity index 100% rename from tests/BitzArt.Flux.Json.Tests/TestModelContext.cs rename to tests/BitzArt.Flux.Json.Tests/Models/TestModelContext.cs diff --git a/tests/BitzArt.Flux.Json.Tests/FromJsonExtensionTests.cs b/tests/BitzArt.Flux.Json.Tests/Tests/FromJsonExtensionTests.cs similarity index 100% rename from tests/BitzArt.Flux.Json.Tests/FromJsonExtensionTests.cs rename to tests/BitzArt.Flux.Json.Tests/Tests/FromJsonExtensionTests.cs diff --git a/tests/BitzArt.Flux.Json.Tests/FromJsonFileExtensionTests.cs b/tests/BitzArt.Flux.Json.Tests/Tests/FromJsonFileExtensionTests.cs similarity index 100% rename from tests/BitzArt.Flux.Json.Tests/FromJsonFileExtensionTests.cs rename to tests/BitzArt.Flux.Json.Tests/Tests/FromJsonFileExtensionTests.cs diff --git a/tests/BitzArt.Flux.Json.Tests/JsonServiceTests.cs b/tests/BitzArt.Flux.Json.Tests/Tests/JsonServiceTests.cs similarity index 100% rename from tests/BitzArt.Flux.Json.Tests/JsonServiceTests.cs rename to tests/BitzArt.Flux.Json.Tests/Tests/JsonServiceTests.cs diff --git a/tests/BitzArt.Flux.Json.Tests/Tests/QueryableSetTests.cs b/tests/BitzArt.Flux.Json.Tests/Tests/QueryableSetTests.cs new file mode 100644 index 0000000..6255650 --- /dev/null +++ b/tests/BitzArt.Flux.Json.Tests/Tests/QueryableSetTests.cs @@ -0,0 +1,105 @@ +using Microsoft.Extensions.DependencyInjection; + +namespace BitzArt.Flux; + +public class QueryableSetTests +{ + private const string Data = + """ + [ + { + "id": 1, + "name": "item 1" + }, + { + "id": 2, + "name": "item 2" + }, + { + "id": 3, + "name": "item 3" + }, + { + "id": 4, + "name": "item 4" + }, + { + "id": 5, + "name": "item 5" + } + ] + """; + + private static IServiceProvider PrepareServices() + { + var services = new ServiceCollection(); + + services.AddFlux(flux => + { + flux.AddService("test") + .UsingJson() + .AddSet() + .FromJson(Data) + .WithKey(x => x.Id!); + }); + + return services.BuildServiceProvider(); + } + + [Fact] + public void GetEnumerator_FromJsonSet_ReturnsEnumerator() + { + var serviceProvider = PrepareServices(); + + var setContext = serviceProvider.GetRequiredService>(); + + var enumerator = setContext.GetEnumerator(); + + Assert.NotNull(enumerator); + Assert.True(enumerator.MoveNext()); + } + + [Fact] + public void ToList_OnJsonSet_ReturnsListWithAllElements() + { + var serviceProvider = PrepareServices(); + + var setContext = serviceProvider.GetRequiredService>(); + + var data = setContext.ToList(); + + Assert.NotNull(data); + Assert.True(data.Any()); + Assert.Equal(5, data.Count); + } + + [Fact] + public void FirstOrDefault_OnJsonSet_ReturnsFirst() + { + var serviceProvider = PrepareServices(); + + var setContext = serviceProvider.GetRequiredService>(); + + var item = setContext.FirstOrDefault(); + + Assert.NotNull(item); + Assert.Equal(1, item.Id); + Assert.Equal("item 1", item.Name); + } + + [Fact] + public void Where_OnJsonSet_Filters() + { + var serviceProvider = PrepareServices(); + + var setContext = serviceProvider.GetRequiredService>(); + + var q = setContext.Where(x => x.Id == 1); + + var item = q.FirstOrDefault(); + + Assert.NotNull(item); + Assert.Equal(1, item.Id); + Assert.Equal("item 1", item.Name); + } +} diff --git a/tests/BitzArt.Flux.Json.Tests/ServiceRegistrationTests.cs b/tests/BitzArt.Flux.Json.Tests/Tests/ServiceRegistrationTests.cs similarity index 100% rename from tests/BitzArt.Flux.Json.Tests/ServiceRegistrationTests.cs rename to tests/BitzArt.Flux.Json.Tests/Tests/ServiceRegistrationTests.cs