From 7a108f67f4feee193c9531060077fbf68c3a632e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A3=8A=E7=A3=8A?= Date: Thu, 21 Jul 2022 10:49:13 +0800 Subject: [PATCH] refactor(autocomplete): Refactor and optimize the user experience (#112) * feat(AutoComplete): Add AudoCompleteDocument * refactor(AutoComplete): Optimize the user experience --- .../AutoCompleteDocument.cs | 39 ++++++++++- .../BaseAutoCompleteClient.cs | 66 ++++++++++++++----- .../IAutoCompleteClient.cs | 31 +++++++-- .../Response/GetResponse.cs | 4 +- 4 files changed, 114 insertions(+), 26 deletions(-) diff --git a/src/SearchEngine/Masa.BuildingBlocks.SearchEngine.AutoComplete/AutoCompleteDocument.cs b/src/SearchEngine/Masa.BuildingBlocks.SearchEngine.AutoComplete/AutoCompleteDocument.cs index d4e2760..8175cc1 100644 --- a/src/SearchEngine/Masa.BuildingBlocks.SearchEngine.AutoComplete/AutoCompleteDocument.cs +++ b/src/SearchEngine/Masa.BuildingBlocks.SearchEngine.AutoComplete/AutoCompleteDocument.cs @@ -3,7 +3,40 @@ namespace Masa.BuildingBlocks.SearchEngine.AutoComplete; -public class AutoCompleteDocument where TValue : notnull +public class AutoCompleteDocument +{ + private string? _documentId; + + private string? _text; + + public string Text + { + get + { + return _text ??= GetText(); + } + init + { + if (value != null!) + _text = value; + } + } + + public AutoCompleteDocument() + { + } + + public AutoCompleteDocument(string text) : this() + { + Text = text; + } + + public virtual string GetDocumentId() => _documentId ??= Guid.NewGuid().ToString(); + + protected virtual string GetText() => string.Empty; +} + +public class AutoCompleteDocument : AutoCompleteDocument where TValue : notnull { private string _id; @@ -25,8 +58,6 @@ public string Id } } - public string Text { get; set; } - public TValue Value { get; set; } public AutoCompleteDocument() @@ -44,4 +75,6 @@ public AutoCompleteDocument(string id, string text, TValue value) : this() Text = text; Value = value; } + + public override string GetDocumentId() => Id; } diff --git a/src/SearchEngine/Masa.BuildingBlocks.SearchEngine.AutoComplete/BaseAutoCompleteClient.cs b/src/SearchEngine/Masa.BuildingBlocks.SearchEngine.AutoComplete/BaseAutoCompleteClient.cs index 34e8e27..7b7aafe 100644 --- a/src/SearchEngine/Masa.BuildingBlocks.SearchEngine.AutoComplete/BaseAutoCompleteClient.cs +++ b/src/SearchEngine/Masa.BuildingBlocks.SearchEngine.AutoComplete/BaseAutoCompleteClient.cs @@ -5,56 +5,92 @@ namespace Masa.BuildingBlocks.SearchEngine.AutoComplete; public abstract class BaseAutoCompleteClient : IAutoCompleteClient { - public virtual Task, Guid>> GetAsync(string keyword, AutoCompleteOptions? options = null, + public virtual Task>> GetAsync( + string keyword, + AutoCompleteOptions? options = null, CancellationToken cancellationToken = default) => GetAsync(keyword, options, cancellationToken); - public virtual Task, TValue>> GetAsync(string keyword, + public virtual Task>> GetAsync( + string keyword, AutoCompleteOptions? options = null, CancellationToken cancellationToken = default) where TValue : notnull => GetAsync, TValue>(keyword, options, cancellationToken); - public abstract Task> GetAsync(string keyword, + public virtual Task> GetAsync( + string keyword, + AutoCompleteOptions? options = null, + CancellationToken cancellationToken = default) where TAudoCompleteDocument : AutoCompleteDocument + => GetBySpecifyDocumentAsync(keyword, options, cancellationToken); + + public abstract Task> GetBySpecifyDocumentAsync( + string keyword, AutoCompleteOptions? options = null, - CancellationToken cancellationToken = default) where TAudoCompleteDocument : AutoCompleteDocument where TValue : notnull; + CancellationToken cancellationToken = default) where TAudoCompleteDocument : AutoCompleteDocument; - public virtual Task SetAsync(AutoCompleteDocument document, SetOptions? options = null, + public virtual Task SetAsync( + AutoCompleteDocument document, + SetOptions? options = null, CancellationToken cancellationToken = default) => SetAsync, Guid>(document, options, cancellationToken); - public virtual Task SetAsync(IEnumerable> documents, SetOptions? options = null, + public virtual Task SetAsync( + IEnumerable> documents, + SetOptions? options = null, CancellationToken cancellationToken = default) => SetAsync, Guid>(documents, options, cancellationToken); - public virtual Task SetAsync(AutoCompleteDocument document, SetOptions? options = null, + public virtual Task SetAsync( + AutoCompleteDocument document, + SetOptions? options = null, CancellationToken cancellationToken = default) where TValue : notnull => SetAsync, TValue>(document, options, cancellationToken); - public virtual Task SetAsync(IEnumerable> documents, SetOptions? options = null, + public virtual Task SetAsync( + IEnumerable> documents, + SetOptions? options = null, CancellationToken cancellationToken = default) where TValue : notnull => SetAsync, TValue>(documents, options, cancellationToken); - public virtual Task SetAsync(TAudoCompleteDocument document, SetOptions? options = null, - CancellationToken cancellationToken = default) where TAudoCompleteDocument : AutoCompleteDocument where TValue : notnull + public virtual Task SetAsync( + TAudoCompleteDocument document, + SetOptions? options = null, + CancellationToken cancellationToken = default) where TAudoCompleteDocument : AutoCompleteDocument => SetAsync(new List { document }, options, cancellationToken); - public abstract Task SetAsync(IEnumerable documents, + public virtual Task SetAsync( + IEnumerable documents, + SetOptions? options = null, + CancellationToken cancellationToken = default) where TAudoCompleteDocument : AutoCompleteDocument + => SetBySpecifyDocumentAsync(documents, options, cancellationToken); + + public virtual Task SetBySpecifyDocumentAsync( + TAudoCompleteDocument document, SetOptions? options = null, - CancellationToken cancellationToken = default) where TAudoCompleteDocument : AutoCompleteDocument where TValue : notnull; + CancellationToken cancellationToken = default) where TAudoCompleteDocument : AutoCompleteDocument + => SetBySpecifyDocumentAsync(new List { document }, options, cancellationToken); + + public abstract Task SetBySpecifyDocumentAsync( + IEnumerable documents, + SetOptions? options = null, + CancellationToken cancellationToken = default) where TAudoCompleteDocument : AutoCompleteDocument; + public abstract Task DeleteAsync(string id, CancellationToken cancellationToken = default); public virtual Task DeleteAsync(T id, CancellationToken cancellationToken = default) where T : IComparable - => DeleteAsync(id!.ToString() ?? throw new ArgumentNullException($"{id} is not null", nameof(id)), cancellationToken); + => DeleteAsync(id.ToString() ?? throw new ArgumentNullException($"{id} is not null", nameof(id)), cancellationToken); public abstract Task DeleteAsync(IEnumerable ids, CancellationToken cancellationToken = default); - public virtual Task DeleteAsync(IEnumerable ids, CancellationToken cancellationToken = default) where T : IComparable + public virtual Task DeleteAsync(IEnumerable 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); + return DeleteAsync(ids.Select(id => id.ToString() ?? throw new ArgumentNullException($"{id} is not null", nameof(id))), + cancellationToken); } } diff --git a/src/SearchEngine/Masa.BuildingBlocks.SearchEngine.AutoComplete/IAutoCompleteClient.cs b/src/SearchEngine/Masa.BuildingBlocks.SearchEngine.AutoComplete/IAutoCompleteClient.cs index 5f4dc6e..ba29aed 100644 --- a/src/SearchEngine/Masa.BuildingBlocks.SearchEngine.AutoComplete/IAutoCompleteClient.cs +++ b/src/SearchEngine/Masa.BuildingBlocks.SearchEngine.AutoComplete/IAutoCompleteClient.cs @@ -5,21 +5,28 @@ namespace Masa.BuildingBlocks.SearchEngine.AutoComplete; public interface IAutoCompleteClient { - Task, Guid>> GetAsync( + Task>> GetAsync( string keyword, AutoCompleteOptions? options = null, CancellationToken cancellationToken = default); - Task, TValue>> GetAsync( + Task>> GetAsync( string keyword, AutoCompleteOptions? options = null, CancellationToken cancellationToken = default) where TValue : notnull; - Task> GetAsync( + [Obsolete($"{nameof(GetAsync)} expired, please use {nameof(GetBySpecifyDocumentAsync)}")] + Task> GetAsync( string keyword, AutoCompleteOptions? options = null, CancellationToken cancellationToken = default) - where TAudoCompleteDocument : AutoCompleteDocument where TValue : notnull; + where TAudoCompleteDocument : AutoCompleteDocument; + + Task> GetBySpecifyDocumentAsync( + string keyword, + AutoCompleteOptions? options = null, + CancellationToken cancellationToken = default) + where TAudoCompleteDocument : AutoCompleteDocument; Task SetAsync( AutoCompleteDocument document, @@ -41,15 +48,27 @@ Task SetAsync( SetOptions? options = null, CancellationToken cancellationToken = default) where TValue : notnull; + [Obsolete($"{nameof(SetAsync)} expired, please use {nameof(SetBySpecifyDocumentAsync)}")] Task SetAsync( TAudoCompleteDocument document, SetOptions? options = null, - CancellationToken cancellationToken = default) where TAudoCompleteDocument : AutoCompleteDocument where TValue : notnull; + CancellationToken cancellationToken = default) where TAudoCompleteDocument : AutoCompleteDocument; + [Obsolete($"{nameof(SetAsync)} expired, please use {nameof(SetBySpecifyDocumentAsync)}")] Task SetAsync( IEnumerable documents, SetOptions? options = null, - CancellationToken cancellationToken = default) where TAudoCompleteDocument : AutoCompleteDocument where TValue : notnull; + CancellationToken cancellationToken = default) where TAudoCompleteDocument : AutoCompleteDocument; + + Task SetBySpecifyDocumentAsync( + TAudoCompleteDocument document, + SetOptions? options = null, + CancellationToken cancellationToken = default) where TAudoCompleteDocument : AutoCompleteDocument; + + Task SetBySpecifyDocumentAsync( + IEnumerable documents, + SetOptions? options = null, + CancellationToken cancellationToken = default) where TAudoCompleteDocument : AutoCompleteDocument; Task DeleteAsync(string id, CancellationToken cancellationToken = default); diff --git a/src/SearchEngine/Masa.BuildingBlocks.SearchEngine.AutoComplete/Response/GetResponse.cs b/src/SearchEngine/Masa.BuildingBlocks.SearchEngine.AutoComplete/Response/GetResponse.cs index 1cc3b7a..a02a7f7 100644 --- a/src/SearchEngine/Masa.BuildingBlocks.SearchEngine.AutoComplete/Response/GetResponse.cs +++ b/src/SearchEngine/Masa.BuildingBlocks.SearchEngine.AutoComplete/Response/GetResponse.cs @@ -3,8 +3,8 @@ namespace Masa.BuildingBlocks.SearchEngine.AutoComplete.Response; -public class GetResponse : ResponseBase - where TDropdownBox : AutoCompleteDocument where TValue : notnull +public class GetResponse : ResponseBase + where TDropdownBox : AutoCompleteDocument { public long Total { get; set; }