From 51052e88eea9536c858d46de71da2cdd274b1ce3 Mon Sep 17 00:00:00 2001 From: Bernd Dongus Date: Thu, 28 Mar 2024 16:02:47 +0100 Subject: [PATCH] 3.3.1: Support empty/missing search value while searching resource keys --- ...hResourceKeysForResourceSetQueryHandler.cs | 10 ++++--- .../SearchResourceKeysQueryHandler.cs | 7 +++-- .../idee5.Globalization.EFCore.csproj | 2 +- .../QueryWithSQLiteTests.cs | 27 +++++++++++++++++++ .../SearchResourceKeysForResourceSetQuery.cs | 6 ++--- .../Queries/SearchResourceKeysQuery.cs | 4 +-- .../idee5.Globalization.csproj | 2 +- 7 files changed, 46 insertions(+), 12 deletions(-) diff --git a/idee5.Globalization.EFCore/SearchResourceKeysForResourceSetQueryHandler.cs b/idee5.Globalization.EFCore/SearchResourceKeysForResourceSetQueryHandler.cs index 21e6aeb..37fd0a2 100644 --- a/idee5.Globalization.EFCore/SearchResourceKeysForResourceSetQueryHandler.cs +++ b/idee5.Globalization.EFCore/SearchResourceKeysForResourceSetQueryHandler.cs @@ -4,6 +4,8 @@ using Microsoft.EntityFrameworkCore; +using NSpecifications; + using System; using System.Collections.Generic; using System.Linq; @@ -31,14 +33,16 @@ public class SearchResourceKeysForResourceSetQueryHandler : IQueryHandlerAsync - /// Query the resource ids in a resource set. Including all parlances. + /// Search the resource keys in a resource set /// /// The query. /// is null. public async Task> HandleAsync(SearchResourceKeysForResourceSetQuery query, CancellationToken cancellationToken = default) { ArgumentNullException.ThrowIfNull(query); - return await _context.Resources.Where(Specifications.ContainsInResourceSet(query.ResourceSet, query.SearchValue)).Select(r => new ResourceKey() { - // just casting results in all records being read + + ASpec predicate = query.SearchValue == null ? Specifications.InResourceSet(query.ResourceSet) : Specifications.ContainsInResourceSet(query.ResourceSet, query.SearchValue); + return await _context.Resources.Where(predicate).Select(r => new ResourceKey() { + // just casting, results in all records being read ResourceSet = r.ResourceSet, Id = r.Id, Industry = r.Industry, diff --git a/idee5.Globalization.EFCore/SearchResourceKeysQueryHandler.cs b/idee5.Globalization.EFCore/SearchResourceKeysQueryHandler.cs index 49d137b..46302a0 100644 --- a/idee5.Globalization.EFCore/SearchResourceKeysQueryHandler.cs +++ b/idee5.Globalization.EFCore/SearchResourceKeysQueryHandler.cs @@ -4,6 +4,8 @@ using Microsoft.EntityFrameworkCore; +using NSpecifications; + using System; using System.Collections.Generic; using System.Linq; @@ -31,13 +33,14 @@ public class SearchResourceKeysQueryHandler : IQueryHandlerAsync - /// Query the resource ids in a resource set. Including all parlances. + /// Search the resource keys /// /// The query. /// is null. public async Task> HandleAsync(SearchResourceKeysQuery query, CancellationToken cancellationToken = default) { ArgumentNullException.ThrowIfNull(query); - return await _context.Resources.Where(Specifications.Contains(query.SearchValue)).Select(r => new ResourceKey() { + ASpec predicate = query.SearchValue == null ? new Spec(_ => true) : Specifications.Contains(query.SearchValue); + return await _context.Resources.Where(predicate).Select(r => new ResourceKey() { // just casting results in all records being read ResourceSet = r.ResourceSet, Id = r.Id, diff --git a/idee5.Globalization.EFCore/idee5.Globalization.EFCore.csproj b/idee5.Globalization.EFCore/idee5.Globalization.EFCore.csproj index 7052144..8a9e0ca 100644 --- a/idee5.Globalization.EFCore/idee5.Globalization.EFCore.csproj +++ b/idee5.Globalization.EFCore/idee5.Globalization.EFCore.csproj @@ -15,7 +15,7 @@ © idee5 2022-2024 idee5, Globalization, Localization, EFCore Search queries added - 3.3 + 3.3.1 Bernd Dongus enable EF core data layer for idee5.Globalization diff --git a/idee5.Globalization.Test/QueryWithSQLiteTests.cs b/idee5.Globalization.Test/QueryWithSQLiteTests.cs index ea95835..7912051 100644 --- a/idee5.Globalization.Test/QueryWithSQLiteTests.cs +++ b/idee5.Globalization.Test/QueryWithSQLiteTests.cs @@ -57,6 +57,20 @@ public async Task CanFindResourceKeysInResourceSetByCustomer() { // Assert Assert.AreEqual(2, result.Count); } + + [TestMethod] + public async Task CanFindAllResourceKeysInResourceSet() { + // Arrange + var qh = new SearchResourceKeysForResourceSetQueryHandler(context); + + // Act + var query = new SearchResourceKeysForResourceSetQuery(Constants.CommonTerms); + var result = await qh.HandleAsync(query, CancellationToken.None).ConfigureAwait(false); + + // Assert + Assert.AreEqual(4, result.Count); + } + [TestMethod] public async Task CanFindValueInResourceKeys() { // Arrange @@ -70,6 +84,19 @@ public async Task CanFindValueInResourceKeys() { Assert.AreEqual(4, result.Count); } + [TestMethod] + public async Task CanFindAllResourceKeys() { + // Arrange + var qh = new SearchResourceKeysQueryHandler(context); + + // Act + var query = new SearchResourceKeysQuery(); + var result = await qh.HandleAsync(query, CancellationToken.None).ConfigureAwait(false); + + // Assert + Assert.AreEqual(4, result.Count); + } + [TestMethod] public async Task CanFindResourceSetInResourceKeys() { // Arrange diff --git a/idee5.Globalization/Queries/SearchResourceKeysForResourceSetQuery.cs b/idee5.Globalization/Queries/SearchResourceKeysForResourceSetQuery.cs index c6134de..18e0bf0 100644 --- a/idee5.Globalization/Queries/SearchResourceKeysForResourceSetQuery.cs +++ b/idee5.Globalization/Queries/SearchResourceKeysForResourceSetQuery.cs @@ -6,8 +6,8 @@ namespace idee5.Globalization.Queries; /// -/// Search a value in a resource set query. +/// Search keys with a value in a resource set query. /// /// Resource set to search in -/// Value to search for -public record SearchResourceKeysForResourceSetQuery(string ResourceSet, string SearchValue) : IQuery>; +/// Value to search for. NULL means get all keys. +public record SearchResourceKeysForResourceSetQuery(string ResourceSet, string? SearchValue = null) : IQuery>; diff --git a/idee5.Globalization/Queries/SearchResourceKeysQuery.cs b/idee5.Globalization/Queries/SearchResourceKeysQuery.cs index 3aef3ad..ac05510 100644 --- a/idee5.Globalization/Queries/SearchResourceKeysQuery.cs +++ b/idee5.Globalization/Queries/SearchResourceKeysQuery.cs @@ -8,5 +8,5 @@ namespace idee5.Globalization.Queries; /// /// Search a value in all resource sets query. /// -/// Value to search for -public record SearchResourceKeysQuery(string SearchValue) : IQuery>; +/// Value to search for. NULL means get all keys. +public record SearchResourceKeysQuery(string? SearchValue = null) : IQuery>; diff --git a/idee5.Globalization/idee5.Globalization.csproj b/idee5.Globalization/idee5.Globalization.csproj index 3cc007d..a99a9b7 100644 --- a/idee5.Globalization/idee5.Globalization.csproj +++ b/idee5.Globalization/idee5.Globalization.csproj @@ -13,7 +13,7 @@ Globalization extensions. Enables database support for localization resources and parlances for industries and customers.. idee5 © idee5 2016 - 2024 - 3.3 + 3.3.1 idee5, Globalization, Localization Search queries added enable