Skip to content

Commit

Permalink
SearchLanguagesQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
Utar94 committed Oct 7, 2024
1 parent 81c0a9b commit 0f65908
Show file tree
Hide file tree
Showing 12 changed files with 191 additions and 0 deletions.
11 changes: 11 additions & 0 deletions backend/src/Logitar.Cms.Contracts/Languages/LanguageSort.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Logitar.Cms.Contracts.Languages;

public enum LanguageSort
{
Code,
CreatedOn,
DisplayName,
EnglishName,
NativeName,
UpdatedOn
}
20 changes: 20 additions & 0 deletions backend/src/Logitar.Cms.Contracts/Languages/LanguageSortOption.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Logitar.Cms.Contracts.Search;

namespace Logitar.Cms.Contracts.Languages;

public record LanguageSortOption : SortOption
{
public new LanguageSort Field
{
get => Enum.Parse<LanguageSort>(base.Field);
set => base.Field = value.ToString();
}

public LanguageSortOption() : this(LanguageSort.DisplayName)
{
}

public LanguageSortOption(LanguageSort field, bool isDescending = false) : base(field.ToString(), isDescending)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Logitar.Cms.Contracts.Search;

namespace Logitar.Cms.Contracts.Languages;

public record SearchLanguagesPayload : SearchPayload
{
public new List<LanguageSortOption> Sort { get; set; } = [];
}
7 changes: 7 additions & 0 deletions backend/src/Logitar.Cms.Contracts/Search/SearchOperator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Logitar.Cms.Contracts.Search;

public enum SearchOperator
{
And = 0,
Or = 1
}
12 changes: 12 additions & 0 deletions backend/src/Logitar.Cms.Contracts/Search/SearchPayload.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Logitar.Cms.Contracts.Search;

public record SearchPayload
{
public List<Guid> Ids { get; set; } = [];
public TextSearch Search { get; set; } = new();

public List<SortOption> Sort { get; set; } = [];

public int Skip { get; set; }
public int Limit { get; set; }
}
25 changes: 25 additions & 0 deletions backend/src/Logitar.Cms.Contracts/Search/SearchResults.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace Logitar.Cms.Contracts.Search;

public record SearchResults<T>
{
public List<T> Items { get; set; }
public long Total { get; set; }

public SearchResults() : this([])
{
}

public SearchResults(IEnumerable<T> items) : this(items, items.LongCount())
{
}

public SearchResults(long total) : this([], total)
{
}

public SearchResults(IEnumerable<T> items, long total)
{
Items = items.ToList();
Total = total;
}
}
15 changes: 15 additions & 0 deletions backend/src/Logitar.Cms.Contracts/Search/SearchTerm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Logitar.Cms.Contracts.Search;

public record SearchTerm
{
public string Value { get; set; }

public SearchTerm() : this(string.Empty)
{
}

public SearchTerm(string value)
{
Value = value;
}
}
17 changes: 17 additions & 0 deletions backend/src/Logitar.Cms.Contracts/Search/SortOption.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Logitar.Cms.Contracts.Search;

public record SortOption
{
public string Field { get; set; }
public bool IsDescending { get; set; }

public SortOption() : this(string.Empty)
{
}

public SortOption(string field, bool isDescending = false)
{
Field = field;
IsDescending = isDescending;
}
}
17 changes: 17 additions & 0 deletions backend/src/Logitar.Cms.Contracts/Search/TextSearch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Logitar.Cms.Contracts.Search;

public record TextSearch
{
public List<SearchTerm> Terms { get; set; }
public SearchOperator Operator { get; set; }

public TextSearch() : this([])
{
}

public TextSearch(IEnumerable<SearchTerm> terms, SearchOperator @operator = SearchOperator.And)
{
Terms = terms.ToList();
Operator = @operator;
}
}
3 changes: 3 additions & 0 deletions backend/src/Logitar.Cms.Core/Languages/ILanguageQuerier.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Logitar.Cms.Contracts.Languages;
using Logitar.Cms.Contracts.Search;

namespace Logitar.Cms.Core.Languages;

Expand All @@ -11,4 +12,6 @@ public interface ILanguageQuerier
Task<LanguageModel?> ReadAsync(Guid id, CancellationToken cancellationToken = default);
Task<LanguageModel?> ReadAsync(string locale, CancellationToken cancellationToken = default);
Task<LanguageModel> ReadDefaultAsync(CancellationToken cancellationToken = default);

Task<SearchResults<LanguageModel>> SearchAsync(SearchLanguagesPayload payload, CancellationToken cancellationToken = default);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Logitar.Cms.Contracts.Languages;
using Logitar.Cms.Contracts.Search;
using MediatR;

namespace Logitar.Cms.Core.Languages.Queries;

public record SearchLanguagesQuery(SearchLanguagesPayload Payload) : IRequest<SearchResults<LanguageModel>>;

internal class SearchLanguagesQueryHandler : IRequestHandler<SearchLanguagesQuery, SearchResults<LanguageModel>>
{
private readonly ILanguageQuerier _languageQuerier;

public SearchLanguagesQueryHandler(ILanguageQuerier languageQuerier)
{
_languageQuerier = languageQuerier;
}

public async Task<SearchResults<LanguageModel>> Handle(SearchLanguagesQuery query, CancellationToken cancellationToken)
{
return await _languageQuerier.SearchAsync(query.Payload, cancellationToken);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Logitar.Cms.Contracts.Languages;
using Logitar.Cms.Contracts.Search;
using Moq;

namespace Logitar.Cms.Core.Languages.Queries;

[Trait(Traits.Category, Categories.Unit)]
public class SearchLanguagesQueryHandlerTests
{
private readonly CancellationToken _cancellationToken = default;

private readonly Mock<ILanguageQuerier> _languageQuerier = new();

private readonly SearchLanguagesQueryHandler _handler;

public SearchLanguagesQueryHandlerTests()
{
_handler = new(_languageQuerier.Object);
}

[Fact(DisplayName = "It should return the correct search results.")]
public async Task It_should_return_the_correct_search_results()
{
SearchLanguagesPayload payload = new();

SearchResults<LanguageModel> results = new();
_languageQuerier.Setup(x => x.SearchAsync(payload, _cancellationToken)).ReturnsAsync(results);

SearchLanguagesQuery query = new(payload);

SearchResults<LanguageModel> searchResults = await _handler.Handle(query, _cancellationToken);
Assert.Same(results, searchResults);
}
}

0 comments on commit 0f65908

Please sign in to comment.