This repository has been archived by the owner on Aug 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(AutoComplete): refactor autocomplete and add delete documents (
#41) * refactor(AutoComplete): refactor autocomplete * chore(AutoComplete): Added CancellationToken parameter * chore: Modify the method name DeleteMultiAsync to DeleteAsync, SetMultiAsync to SetAsync * chore(AutoComplete): Added constraint to id * chore: Masa.BuildingBlocks modified to MASA.BuildingBlocks
- Loading branch information
1 parent
692f774
commit 15d2042
Showing
15 changed files
with
267 additions
and
17 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
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
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
56 changes: 56 additions & 0 deletions
56
src/SearchEngine/Masa.BuildingBlocks.SearchEngine.AutoComplete/BaseAutoCompleteClient.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,56 @@ | ||
namespace Masa.BuildingBlocks.SearchEngine.AutoComplete; | ||
public abstract class BaseAutoCompleteClient : IAutoCompleteClient | ||
{ | ||
public virtual Task<GetResponse<AutoCompleteDocument<Guid>, Guid>> GetAsync(string keyword, AutoCompleteOptions? options = null, | ||
CancellationToken cancellationToken = default) | ||
=> GetAsync<Guid>(keyword, options, cancellationToken); | ||
|
||
public virtual Task<GetResponse<AutoCompleteDocument<TValue>, TValue>> GetAsync<TValue>(string keyword, | ||
AutoCompleteOptions? options = null, | ||
CancellationToken cancellationToken = default) where TValue : notnull | ||
=> GetAsync<AutoCompleteDocument<TValue>, TValue>(keyword, options, cancellationToken); | ||
|
||
public abstract Task<GetResponse<TAudoCompleteDocument, TValue>> GetAsync<TAudoCompleteDocument, TValue>(string keyword, | ||
AutoCompleteOptions? options = null, | ||
CancellationToken cancellationToken = default) where TAudoCompleteDocument : AutoCompleteDocument<TValue> where TValue : notnull; | ||
|
||
public virtual Task<SetResponse> SetAsync(AutoCompleteDocument<Guid> document, SetOptions? options = null, | ||
CancellationToken cancellationToken = default) | ||
=> SetAsync<AutoCompleteDocument<Guid>, Guid>(document, options, cancellationToken); | ||
|
||
public virtual Task<SetResponse> SetAsync(IEnumerable<AutoCompleteDocument<Guid>> documents, SetOptions? options = null, | ||
CancellationToken cancellationToken = default) | ||
=> SetAsync<AutoCompleteDocument<Guid>, Guid>(documents, options, cancellationToken); | ||
|
||
public virtual Task<SetResponse> SetAsync<TValue>(AutoCompleteDocument<TValue> document, SetOptions? options = null, | ||
CancellationToken cancellationToken = default) where TValue : notnull | ||
=> SetAsync<AutoCompleteDocument<TValue>, TValue>(document, options, cancellationToken); | ||
|
||
public virtual Task<SetResponse> SetAsync<TValue>(IEnumerable<AutoCompleteDocument<TValue>> documents, SetOptions? options = null, | ||
CancellationToken cancellationToken = default) where TValue : notnull | ||
=> SetAsync<AutoCompleteDocument<TValue>, TValue>(documents, options, cancellationToken); | ||
|
||
public virtual Task<SetResponse> SetAsync<TAudoCompleteDocument, TValue>(TAudoCompleteDocument document, SetOptions? options = null, | ||
CancellationToken cancellationToken = default) where TAudoCompleteDocument : AutoCompleteDocument<TValue> where TValue : notnull | ||
=> SetAsync<TAudoCompleteDocument, TValue>(new List<TAudoCompleteDocument> { document }, options, cancellationToken); | ||
|
||
public abstract Task<SetResponse> SetAsync<TAudoCompleteDocument, TValue>(IEnumerable<TAudoCompleteDocument> documents, | ||
SetOptions? options = null, | ||
CancellationToken cancellationToken = default) where TAudoCompleteDocument : AutoCompleteDocument<TValue> where TValue : notnull; | ||
|
||
public abstract Task<DeleteResponse> DeleteAsync(string id, CancellationToken cancellationToken = default); | ||
|
||
public virtual Task<DeleteResponse> DeleteAsync<T>(T id, CancellationToken cancellationToken = default) where T : IComparable | ||
=> DeleteAsync(id!.ToString() ?? throw new ArgumentNullException($"{id} is not null", nameof(id)), cancellationToken); | ||
|
||
public abstract Task<DeleteMultiResponse> DeleteAsync(IEnumerable<string> ids, CancellationToken cancellationToken = default); | ||
|
||
public virtual Task<DeleteMultiResponse> DeleteAsync<T>(IEnumerable<T> ids, CancellationToken cancellationToken = default) where T : IComparable | ||
{ | ||
var type = typeof(T); | ||
if (!type.IsPrimitive && type != typeof(Guid) && type != typeof(string)) | ||
throw new NotSupportedException("Unsupported types, id only supports simple types or guid, string"); | ||
|
||
return DeleteAsync(ids.Select(id => id.ToString() ?? throw new ArgumentNullException($"{id} is not null", nameof(id))), cancellationToken); | ||
} | ||
} |
46 changes: 40 additions & 6 deletions
46
src/SearchEngine/Masa.BuildingBlocks.SearchEngine.AutoComplete/IAutoCompleteClient.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 |
---|---|---|
@@ -1,24 +1,58 @@ | ||
namespace Masa.BuildingBlocks.SearchEngine.AutoComplete; | ||
|
||
public interface IAutoCompleteClient | ||
{ | ||
Task<GetResponse<AutoCompleteDocument<TValue>, TValue>> GetAsync<TValue>( | ||
Task<GetResponse<AutoCompleteDocument<Guid>, Guid>> GetAsync( | ||
string keyword, | ||
AutoCompleteOptions? options = null, | ||
CancellationToken cancellationToken = default); | ||
|
||
Task<GetResponse<AutoCompleteDocument<TValue>, TValue>> GetAsync<TValue>( | ||
string keyword, | ||
AutoCompleteOptions? options = null, | ||
CancellationToken cancellationToken = default) where TValue : notnull; | ||
|
||
Task<GetResponse<TAudoCompleteDocument, TValue>> GetAsync<TAudoCompleteDocument, TValue>( | ||
string keyword, | ||
AutoCompleteOptions? options = null, | ||
CancellationToken cancellationToken = default) | ||
where TAudoCompleteDocument : AutoCompleteDocument<TValue>; | ||
where TAudoCompleteDocument : AutoCompleteDocument<TValue> where TValue : notnull; | ||
|
||
Task<SetResponse> SetAsync<TValue>( | ||
AutoCompleteDocument<TValue>[] results, | ||
Task<SetResponse> SetAsync( | ||
AutoCompleteDocument<Guid> document, | ||
SetOptions? options = null, | ||
CancellationToken cancellationToken = default); | ||
|
||
Task<SetResponse> SetAsync( | ||
IEnumerable<AutoCompleteDocument<Guid>> documents, | ||
SetOptions? options = null, | ||
CancellationToken cancellationToken = default); | ||
|
||
Task<SetResponse> SetAsync<TValue>( | ||
AutoCompleteDocument<TValue> document, | ||
SetOptions? options = null, | ||
CancellationToken cancellationToken = default) where TValue : notnull; | ||
|
||
Task<SetResponse> SetAsync<TValue>( | ||
IEnumerable<AutoCompleteDocument<TValue>> documents, | ||
SetOptions? options = null, | ||
CancellationToken cancellationToken = default) where TValue : notnull; | ||
|
||
Task<SetResponse> SetAsync<TAudoCompleteDocument, TValue>( | ||
TAudoCompleteDocument[] documents, | ||
TAudoCompleteDocument document, | ||
SetOptions? options = null, | ||
CancellationToken cancellationToken = default) where TAudoCompleteDocument : AutoCompleteDocument<TValue> ; | ||
CancellationToken cancellationToken = default) where TAudoCompleteDocument : AutoCompleteDocument<TValue> where TValue : notnull; | ||
|
||
Task<SetResponse> SetAsync<TAudoCompleteDocument, TValue>( | ||
IEnumerable<TAudoCompleteDocument> documents, | ||
SetOptions? options = null, | ||
CancellationToken cancellationToken = default) where TAudoCompleteDocument : AutoCompleteDocument<TValue> where TValue : notnull; | ||
|
||
Task<DeleteResponse> DeleteAsync(string id, CancellationToken cancellationToken = default); | ||
|
||
Task<DeleteResponse> DeleteAsync<T>(T id, CancellationToken cancellationToken = default) where T : IComparable; | ||
|
||
Task<DeleteMultiResponse> DeleteAsync(IEnumerable<string> ids, CancellationToken cancellationToken = default); | ||
|
||
Task<DeleteMultiResponse> DeleteAsync<T>(IEnumerable<T> ids, CancellationToken cancellationToken = default) where T : IComparable; | ||
} |
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
16 changes: 16 additions & 0 deletions
16
...earchEngine/Masa.BuildingBlocks.SearchEngine.AutoComplete/Response/DeleteMultiResponse.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,16 @@ | ||
namespace Masa.BuildingBlocks.SearchEngine.AutoComplete.Response; | ||
public class DeleteMultiResponse : ResponseBase | ||
{ | ||
public List<DeleteRangeResponseItems> Data { get; set; } | ||
|
||
public DeleteMultiResponse(bool isValid, string message) : base(isValid, message) | ||
{ | ||
} | ||
|
||
public DeleteMultiResponse(bool isValid, string message, IEnumerable<DeleteRangeResponseItems>? data) : this(isValid, message) | ||
{ | ||
ArgumentNullException.ThrowIfNull(data, nameof(data)); | ||
|
||
Data = data.ToList(); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...Engine/Masa.BuildingBlocks.SearchEngine.AutoComplete/Response/DeleteRangeResponseItems.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,16 @@ | ||
namespace Masa.BuildingBlocks.SearchEngine.AutoComplete.Response; | ||
public class DeleteRangeResponseItems | ||
{ | ||
public string Id { get; } | ||
|
||
public bool IsValid { get; } | ||
|
||
public string Message { get; } | ||
|
||
public DeleteRangeResponseItems(string id, bool isValid, string message) | ||
{ | ||
this.Id = id; | ||
this.IsValid = isValid; | ||
this.Message = message; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/SearchEngine/Masa.BuildingBlocks.SearchEngine.AutoComplete/Response/DeleteResponse.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 Masa.BuildingBlocks.SearchEngine.AutoComplete.Response; | ||
public class DeleteResponse : ResponseBase | ||
{ | ||
public DeleteResponse(bool isValid, string message) : base(isValid, message) | ||
{ | ||
} | ||
} |
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
1 change: 0 additions & 1 deletion
1
src/SearchEngine/Masa.BuildingBlocks.SearchEngine.AutoComplete/Response/SetResponse.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
25 changes: 25 additions & 0 deletions
25
test/Masa.BuildingBlocks.SearchEngine.AutoComplete.Tests/CustomAutoCompleteClient.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,25 @@ | ||
namespace Masa.BuildingBlocks.SearchEngine.AutoComplete.Tests; | ||
public class CustomAutoCompleteClient : BaseAutoCompleteClient | ||
{ | ||
public override Task<GetResponse<TAudoCompleteDocument, TValue>> GetAsync<TAudoCompleteDocument, TValue>(string keyword, | ||
AutoCompleteOptions? options = null, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public override Task<SetResponse> SetMultiAsync<TAudoCompleteDocument, TValue>(IEnumerable<TAudoCompleteDocument> documents, | ||
SetOptions? options = null, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public override Task<DeleteResponse> DeleteAsync(string id) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public override Task<DeleteMultiResponse> DeleteMultiAsync(IEnumerable<string> ids) | ||
=> Task.FromResult(new DeleteMultiResponse(true, "")); | ||
} |
21 changes: 21 additions & 0 deletions
21
...earchEngine.AutoComplete.Tests/Masa.BuildingBlocks.SearchEngine.AutoComplete.Tests.csproj
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,21 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" /> | ||
<PackageReference Include="MSTest.TestAdapter" Version="2.2.3" /> | ||
<PackageReference Include="MSTest.TestFramework" Version="2.2.3" /> | ||
<PackageReference Include="coverlet.collector" Version="3.0.2" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\SearchEngine\Masa.BuildingBlocks.SearchEngine.AutoComplete\Masa.BuildingBlocks.SearchEngine.AutoComplete.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
36 changes: 36 additions & 0 deletions
36
test/Masa.BuildingBlocks.SearchEngine.AutoComplete.Tests/TestAutoCompleteClient.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,36 @@ | ||
namespace Masa.BuildingBlocks.SearchEngine.AutoComplete.Tests; | ||
|
||
[TestClass] | ||
public class TestAutoCompleteClient | ||
{ | ||
[TestMethod] | ||
public async Task TestDeleteMultiAsyncReturnThrowNotSupportedException() | ||
{ | ||
var client = new CustomAutoCompleteClient(); | ||
await Assert.ThrowsExceptionAsync<NotSupportedException>(() => client.DeleteMultiAsync(new List<AutoCompleteDocument<int>>() | ||
{ | ||
new("2", 2), | ||
new("1", 1), | ||
})); | ||
} | ||
|
||
[TestMethod] | ||
public async Task TestDeleteMultiAsyncReturnSuccess() | ||
{ | ||
var client = new CustomAutoCompleteClient(); | ||
var response = await client.DeleteMultiAsync(new[] { 1, 2 }); | ||
Assert.IsTrue(response.IsValid); | ||
|
||
response = await client.DeleteMultiAsync(new[] { "1", "2" }); | ||
Assert.IsTrue(response.IsValid); | ||
|
||
response = await client.DeleteMultiAsync(new[] { 1d, 2d }); | ||
Assert.IsTrue(response.IsValid); | ||
|
||
response = await client.DeleteMultiAsync(new[] { 1l, 2l }); | ||
Assert.IsTrue(response.IsValid); | ||
|
||
response = await client.DeleteMultiAsync(new[] { Guid.NewGuid(), Guid.NewGuid() }); | ||
Assert.IsTrue(response.IsValid); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
test/Masa.BuildingBlocks.SearchEngine.AutoComplete.Tests/_Imports.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,4 @@ | ||
global using Masa.BuildingBlocks.SearchEngine.AutoComplete.Options; | ||
global using Masa.BuildingBlocks.SearchEngine.AutoComplete.Response; | ||
global using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|