Skip to content

Commit

Permalink
3.3.1: Support empty/missing search value while searching resource keys
Browse files Browse the repository at this point in the history
  • Loading branch information
bdongus committed Mar 28, 2024
1 parent 107a42e commit 51052e8
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

using Microsoft.EntityFrameworkCore;

using NSpecifications;

using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -31,14 +33,16 @@ public class SearchResourceKeysForResourceSetQueryHandler : IQueryHandlerAsync<S
#region Public Methods

/// <summary>
/// Query the resource ids in a resource set. Including all parlances.
/// Search the resource keys in a resource set
/// </summary>
/// <param name="query">The query.</param>
/// <exception cref="ArgumentNullException"><paramref name="query"/> is <c>null</c>.</exception>
public async Task<IList<ResourceKey>> 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<Resource> 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,
Expand Down
7 changes: 5 additions & 2 deletions idee5.Globalization.EFCore/SearchResourceKeysQueryHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

using Microsoft.EntityFrameworkCore;

using NSpecifications;

using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -31,13 +33,14 @@ public class SearchResourceKeysQueryHandler : IQueryHandlerAsync<SearchResourceK
#region Public Methods

/// <summary>
/// Query the resource ids in a resource set. Including all parlances.
/// Search the resource keys
/// </summary>
/// <param name="query">The query.</param>
/// <exception cref="ArgumentNullException"><paramref name="query"/> is <c>null</c>.</exception>
public async Task<IList<ResourceKey>> HandleAsync(SearchResourceKeysQuery query, CancellationToken cancellationToken = default) {
ArgumentNullException.ThrowIfNull(query);
return await _context.Resources.Where(Specifications.Contains(query.SearchValue)).Select(r => new ResourceKey() {
ASpec<Resource> predicate = query.SearchValue == null ? new Spec<Resource>(_ => 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<Copyright>© idee5 2022-2024</Copyright>
<PackageTags>idee5, Globalization, Localization, EFCore</PackageTags>
<PackageReleaseNotes>Search queries added</PackageReleaseNotes>
<Version>3.3</Version>
<Version>3.3.1</Version>
<Authors>Bernd Dongus</Authors>
<Nullable>enable</Nullable>
<Title>EF core data layer for idee5.Globalization</Title>
Expand Down
27 changes: 27 additions & 0 deletions idee5.Globalization.Test/QueryWithSQLiteTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
namespace idee5.Globalization.Queries;

/// <summary>
/// Search a value in a resource set query.
/// Search keys with a value in a resource set query.
/// </summary>
/// <param name="ResourceSet">Resource set to search in</param>
/// <param name="SearchValue">Value to search for</param>
public record SearchResourceKeysForResourceSetQuery(string ResourceSet, string SearchValue) : IQuery<IList<ResourceKey>>;
/// <param name="SearchValue">Value to search for. <c>NULL</c> means get all keys.</param>
public record SearchResourceKeysForResourceSetQuery(string ResourceSet, string? SearchValue = null) : IQuery<IList<ResourceKey>>;
4 changes: 2 additions & 2 deletions idee5.Globalization/Queries/SearchResourceKeysQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ namespace idee5.Globalization.Queries;
/// <summary>
/// Search a value in all resource sets query.
/// </summary>
/// <param name="SearchValue">Value to search for</param>
public record SearchResourceKeysQuery(string SearchValue) : IQuery<IList<ResourceKey>>;
/// <param name="SearchValue">Value to search for. <c>NULL</c> means get all keys.</param>
public record SearchResourceKeysQuery(string? SearchValue = null) : IQuery<IList<ResourceKey>>;
2 changes: 1 addition & 1 deletion idee5.Globalization/idee5.Globalization.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<Description>Globalization extensions. Enables database support for localization resources and parlances for industries and customers..</Description>
<Company>idee5</Company>
<Copyright>© idee5 2016 - 2024</Copyright>
<Version>3.3</Version>
<Version>3.3.1</Version>
<PackageTags>idee5, Globalization, Localization</PackageTags>
<PackageReleaseNotes>Search queries added</PackageReleaseNotes>
<Nullable>enable</Nullable>
Expand Down

0 comments on commit 51052e8

Please sign in to comment.