Skip to content
This repository has been archived by the owner on Aug 1, 2022. It is now read-only.

Commit

Permalink
refactor(autocomplete): Refactor and optimize the user experience (#112)
Browse files Browse the repository at this point in the history
* feat(AutoComplete): Add AudoCompleteDocument

* refactor(AutoComplete): Optimize the user experience
  • Loading branch information
zhenlei520 authored Jul 21, 2022
1 parent 3e39ac0 commit 7a108f6
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,40 @@

namespace Masa.BuildingBlocks.SearchEngine.AutoComplete;

public class AutoCompleteDocument<TValue> 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<TValue> : AutoCompleteDocument where TValue : notnull
{
private string _id;

Expand All @@ -25,8 +58,6 @@ public string Id
}
}

public string Text { get; set; }

public TValue Value { get; set; }

public AutoCompleteDocument()
Expand All @@ -44,4 +75,6 @@ public AutoCompleteDocument(string id, string text, TValue value) : this()
Text = text;
Value = value;
}

public override string GetDocumentId() => Id;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,92 @@ namespace Masa.BuildingBlocks.SearchEngine.AutoComplete;

public abstract class BaseAutoCompleteClient : IAutoCompleteClient
{
public virtual Task<GetResponse<AutoCompleteDocument<Guid>, Guid>> GetAsync(string keyword, AutoCompleteOptions? options = null,
public virtual Task<GetResponse<AutoCompleteDocument<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,
public virtual Task<GetResponse<AutoCompleteDocument<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,
public virtual Task<GetResponse<TAudoCompleteDocument>> GetAsync<TAudoCompleteDocument, TValue>(
string keyword,
AutoCompleteOptions? options = null,
CancellationToken cancellationToken = default) where TAudoCompleteDocument : AutoCompleteDocument
=> GetBySpecifyDocumentAsync<TAudoCompleteDocument>(keyword, options, cancellationToken);

public abstract Task<GetResponse<TAudoCompleteDocument>> GetBySpecifyDocumentAsync<TAudoCompleteDocument>(
string keyword,
AutoCompleteOptions? options = null,
CancellationToken cancellationToken = default) where TAudoCompleteDocument : AutoCompleteDocument<TValue> where TValue : notnull;
CancellationToken cancellationToken = default) where TAudoCompleteDocument : AutoCompleteDocument;

public virtual Task<SetResponse> SetAsync(AutoCompleteDocument<Guid> document, SetOptions? options = null,
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,
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,
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,
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
public virtual Task<SetResponse> SetAsync<TAudoCompleteDocument, TValue>(
TAudoCompleteDocument document,
SetOptions? options = null,
CancellationToken cancellationToken = default) where TAudoCompleteDocument : AutoCompleteDocument
=> SetAsync<TAudoCompleteDocument, TValue>(new List<TAudoCompleteDocument> { document }, options, cancellationToken);

public abstract Task<SetResponse> SetAsync<TAudoCompleteDocument, TValue>(IEnumerable<TAudoCompleteDocument> documents,
public virtual Task<SetResponse> SetAsync<TAudoCompleteDocument, TValue>(
IEnumerable<TAudoCompleteDocument> documents,
SetOptions? options = null,
CancellationToken cancellationToken = default) where TAudoCompleteDocument : AutoCompleteDocument
=> SetBySpecifyDocumentAsync(documents, options, cancellationToken);

public virtual Task<SetResponse> SetBySpecifyDocumentAsync<TAudoCompleteDocument>(
TAudoCompleteDocument document,
SetOptions? options = null,
CancellationToken cancellationToken = default) where TAudoCompleteDocument : AutoCompleteDocument<TValue> where TValue : notnull;
CancellationToken cancellationToken = default) where TAudoCompleteDocument : AutoCompleteDocument
=> SetBySpecifyDocumentAsync(new List<TAudoCompleteDocument> { document }, options, cancellationToken);

public abstract Task<SetResponse> SetBySpecifyDocumentAsync<TAudoCompleteDocument>(
IEnumerable<TAudoCompleteDocument> documents,
SetOptions? options = null,
CancellationToken cancellationToken = default) where TAudoCompleteDocument : AutoCompleteDocument;


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);
=> 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
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);
return DeleteAsync(ids.Select(id => id.ToString() ?? throw new ArgumentNullException($"{id} is not null", nameof(id))),
cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,28 @@ namespace Masa.BuildingBlocks.SearchEngine.AutoComplete;

public interface IAutoCompleteClient
{
Task<GetResponse<AutoCompleteDocument<Guid>, Guid>> GetAsync(
Task<GetResponse<AutoCompleteDocument<Guid>>> GetAsync(
string keyword,
AutoCompleteOptions? options = null,
CancellationToken cancellationToken = default);

Task<GetResponse<AutoCompleteDocument<TValue>, TValue>> GetAsync<TValue>(
Task<GetResponse<AutoCompleteDocument<TValue>>> GetAsync<TValue>(
string keyword,
AutoCompleteOptions? options = null,
CancellationToken cancellationToken = default) where TValue : notnull;

Task<GetResponse<TAudoCompleteDocument, TValue>> GetAsync<TAudoCompleteDocument, TValue>(
[Obsolete($"{nameof(GetAsync)} expired, please use {nameof(GetBySpecifyDocumentAsync)}")]
Task<GetResponse<TAudoCompleteDocument>> GetAsync<TAudoCompleteDocument, TValue>(
string keyword,
AutoCompleteOptions? options = null,
CancellationToken cancellationToken = default)
where TAudoCompleteDocument : AutoCompleteDocument<TValue> where TValue : notnull;
where TAudoCompleteDocument : AutoCompleteDocument;

Task<GetResponse<TAudoCompleteDocument>> GetBySpecifyDocumentAsync<TAudoCompleteDocument>(
string keyword,
AutoCompleteOptions? options = null,
CancellationToken cancellationToken = default)
where TAudoCompleteDocument : AutoCompleteDocument;

Task<SetResponse> SetAsync(
AutoCompleteDocument<Guid> document,
Expand All @@ -41,15 +48,27 @@ Task<SetResponse> SetAsync<TValue>(
SetOptions? options = null,
CancellationToken cancellationToken = default) where TValue : notnull;

[Obsolete($"{nameof(SetAsync)} expired, please use {nameof(SetBySpecifyDocumentAsync)}")]
Task<SetResponse> SetAsync<TAudoCompleteDocument, TValue>(
TAudoCompleteDocument document,
SetOptions? options = null,
CancellationToken cancellationToken = default) where TAudoCompleteDocument : AutoCompleteDocument<TValue> where TValue : notnull;
CancellationToken cancellationToken = default) where TAudoCompleteDocument : AutoCompleteDocument;

[Obsolete($"{nameof(SetAsync)} expired, please use {nameof(SetBySpecifyDocumentAsync)}")]
Task<SetResponse> SetAsync<TAudoCompleteDocument, TValue>(
IEnumerable<TAudoCompleteDocument> documents,
SetOptions? options = null,
CancellationToken cancellationToken = default) where TAudoCompleteDocument : AutoCompleteDocument<TValue> where TValue : notnull;
CancellationToken cancellationToken = default) where TAudoCompleteDocument : AutoCompleteDocument;

Task<SetResponse> SetBySpecifyDocumentAsync<TAudoCompleteDocument>(
TAudoCompleteDocument document,
SetOptions? options = null,
CancellationToken cancellationToken = default) where TAudoCompleteDocument : AutoCompleteDocument;

Task<SetResponse> SetBySpecifyDocumentAsync<TAudoCompleteDocument>(
IEnumerable<TAudoCompleteDocument> documents,
SetOptions? options = null,
CancellationToken cancellationToken = default) where TAudoCompleteDocument : AutoCompleteDocument;

Task<DeleteResponse> DeleteAsync(string id, CancellationToken cancellationToken = default);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

namespace Masa.BuildingBlocks.SearchEngine.AutoComplete.Response;

public class GetResponse<TDropdownBox, TValue> : ResponseBase
where TDropdownBox : AutoCompleteDocument<TValue> where TValue : notnull
public class GetResponse<TDropdownBox> : ResponseBase
where TDropdownBox : AutoCompleteDocument
{
public long Total { get; set; }

Expand Down

0 comments on commit 7a108f6

Please sign in to comment.