-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented content type search. (#31)
- Loading branch information
Showing
13 changed files
with
251 additions
and
5 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
backend/src/Logitar.Cms.Contracts/ContentTypes/ContentTypeSort.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,8 @@ | ||
namespace Logitar.Cms.Contracts.ContentTypes; | ||
|
||
public enum ContentTypeSort | ||
{ | ||
DisplayName, | ||
UniqueName, | ||
UpdatedOn | ||
} |
20 changes: 20 additions & 0 deletions
20
backend/src/Logitar.Cms.Contracts/ContentTypes/ContentTypeSortOption.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,20 @@ | ||
using Logitar.Cms.Contracts.Search; | ||
|
||
namespace Logitar.Cms.Contracts.ContentTypes; | ||
|
||
public record ContentTypeSortOption : SortOption | ||
{ | ||
public new ContentTypeSort Field | ||
{ | ||
get => Enum.Parse<ContentTypeSort>(base.Field); | ||
set => base.Field = value.ToString(); | ||
} | ||
|
||
public ContentTypeSortOption() : this(ContentTypeSort.UpdatedOn, isDescending: true) | ||
{ | ||
} | ||
|
||
public ContentTypeSortOption(ContentTypeSort field, bool isDescending = false) : base(field.ToString(), isDescending) | ||
{ | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
backend/src/Logitar.Cms.Contracts/ContentTypes/SearchContentTypesPayload.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,10 @@ | ||
using Logitar.Cms.Contracts.Search; | ||
|
||
namespace Logitar.Cms.Contracts.ContentTypes; | ||
|
||
public record SearchContentTypesPayload : SearchPayload | ||
{ | ||
public bool? IsInvariant { get; set; } | ||
|
||
public new List<ContentTypeSortOption>? Sort { get; set; } | ||
} |
4 changes: 0 additions & 4 deletions
4
backend/src/Logitar.Cms.Core/ContentTypes/ContentTypeAggregate.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
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
7 changes: 7 additions & 0 deletions
7
backend/src/Logitar.Cms.Core/ContentTypes/Queries/SearchContentTypesQuery.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 @@ | ||
using Logitar.Cms.Contracts.ContentTypes; | ||
using Logitar.Cms.Contracts.Search; | ||
using MediatR; | ||
|
||
namespace Logitar.Cms.Core.ContentTypes.Queries; | ||
|
||
public record SearchContentTypesQuery(SearchContentTypesPayload Payload) : IRequest<SearchResults<CmsContentType>>; |
20 changes: 20 additions & 0 deletions
20
backend/src/Logitar.Cms.Core/ContentTypes/Queries/SearchContentTypesQueryHandler.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,20 @@ | ||
using Logitar.Cms.Contracts.ContentTypes; | ||
using Logitar.Cms.Contracts.Search; | ||
using MediatR; | ||
|
||
namespace Logitar.Cms.Core.ContentTypes.Queries; | ||
|
||
internal class SearchContentTypesQueryHandler : IRequestHandler<SearchContentTypesQuery, SearchResults<CmsContentType>> | ||
{ | ||
private readonly IContentTypeQuerier _fieldTypeQuerier; | ||
|
||
public SearchContentTypesQueryHandler(IContentTypeQuerier fieldTypeQuerier) | ||
{ | ||
_fieldTypeQuerier = fieldTypeQuerier; | ||
} | ||
|
||
public async Task<SearchResults<CmsContentType>> Handle(SearchContentTypesQuery query, CancellationToken cancellationToken) | ||
{ | ||
return await _fieldTypeQuerier.SearchAsync(query.Payload, cancellationToken); | ||
} | ||
} |
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
37 changes: 37 additions & 0 deletions
37
backend/src/Logitar.Cms.Web/Models/ContentTypes/SearchContentTypesParameters.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,37 @@ | ||
using Logitar.Cms.Contracts.ContentTypes; | ||
using Logitar.Cms.Contracts.Search; | ||
using Logitar.Cms.Web.Models.Search; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace Logitar.Cms.Web.Models.ContentTypes; | ||
|
||
public record SearchContentTypesParameters : SearchParameters | ||
{ | ||
[FromQuery(Name = "invariant")] | ||
public bool? IsInvariant { get; set; } | ||
|
||
public SearchContentTypesPayload ToPayload() | ||
{ | ||
SearchContentTypesPayload payload = new() | ||
{ | ||
IsInvariant = IsInvariant | ||
}; | ||
|
||
FillPayload(payload); | ||
|
||
List<SortOption>? sortOptions = ((SearchPayload)payload).Sort; | ||
if (sortOptions != null) | ||
{ | ||
payload.Sort = new List<ContentTypeSortOption>(capacity: sortOptions.Count); | ||
foreach (SortOption sort in sortOptions) | ||
{ | ||
if (Enum.TryParse(sort.Field, out ContentTypeSort field)) | ||
{ | ||
payload.Sort.Add(new ContentTypeSortOption(field, sort.IsDescending)); | ||
} | ||
} | ||
} | ||
|
||
return payload; | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
...tar.Cms.Core.IntegrationTests/ContentTypes/Queries/SearchContentTypesQueryHandlerTests.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,73 @@ | ||
using Logitar.Cms.Contracts.ContentTypes; | ||
using Logitar.Cms.Contracts.Search; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Logitar.Cms.Core.ContentTypes.Queries; | ||
|
||
[Trait(Traits.Category, Categories.Integration)] | ||
public class SearchContentTypesQueryHandlerTests : IntegrationTests | ||
{ | ||
private readonly IContentTypeRepository _fieldTypeRepository; | ||
|
||
private readonly ContentTypeAggregate _article; | ||
private readonly ContentTypeAggregate _blog; | ||
private readonly ContentTypeAggregate _author; | ||
private readonly ContentTypeAggregate _magazine; | ||
private readonly ContentTypeAggregate _product; | ||
|
||
public SearchContentTypesQueryHandlerTests() : base() | ||
{ | ||
_fieldTypeRepository = ServiceProvider.GetRequiredService<IContentTypeRepository>(); | ||
|
||
_article = new(new IdentifierUnit("BlogArticle"), isInvariant: false); | ||
_blog = new(new IdentifierUnit("Blog"), isInvariant: false); | ||
_author = new(new IdentifierUnit("BlogAuthor"), isInvariant: true); | ||
_magazine = new(new IdentifierUnit("Magazine"), isInvariant: false); | ||
_product = new(new IdentifierUnit("Product"), isInvariant: false); | ||
} | ||
|
||
public override async Task InitializeAsync() | ||
{ | ||
await base.InitializeAsync(); | ||
|
||
await _fieldTypeRepository.SaveAsync([_article, _blog, _author, _magazine, _product]); | ||
} | ||
|
||
[Fact(DisplayName = "It should return empty results when no field type matches.")] | ||
public async Task It_should_return_empty_results_when_no_field_type_matches() | ||
{ | ||
SearchContentTypesPayload payload = new() | ||
{ | ||
Search = new TextSearch([new SearchTerm("%test%")]) | ||
}; | ||
SearchContentTypesQuery query = new(payload); | ||
|
||
SearchResults<CmsContentType> results = await Pipeline.ExecuteAsync(query); | ||
|
||
Assert.Empty(results.Items); | ||
Assert.Equal(0, results.Total); | ||
} | ||
|
||
[Fact(DisplayName = "It should return the correct matching field types.")] | ||
public async Task It_should_return_the_correct_matching_field_types() | ||
{ | ||
SearchContentTypesPayload payload = new() | ||
{ | ||
IsInvariant = false, | ||
IdIn = (await _fieldTypeRepository.LoadAsync()).Select(fieldType => fieldType.Id.ToGuid()).ToList(), | ||
Search = new TextSearch([new SearchTerm("blog%"), new SearchTerm("%z%")], SearchOperator.Or), | ||
Sort = [new ContentTypeSortOption(ContentTypeSort.UniqueName, isDescending: true)], | ||
Skip = 1, | ||
Limit = 1 | ||
}; | ||
payload.IdIn.Add(Guid.Empty); | ||
payload.IdIn.Remove(_blog.Id.ToGuid()); | ||
SearchContentTypesQuery query = new(payload); | ||
|
||
SearchResults<CmsContentType> results = await Pipeline.ExecuteAsync(query); | ||
|
||
Assert.Equal(2, results.Total); | ||
CmsContentType fieldType = Assert.Single(results.Items); | ||
Assert.Equal(_article.Id.ToGuid(), fieldType.Id); | ||
} | ||
} |