-
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.
- Loading branch information
Showing
12 changed files
with
255 additions
and
1 deletion.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
backend/src/Logitar.Cms.Contracts/FieldTypes/FieldTypeSort.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.FieldTypes; | ||
|
||
public enum FieldTypeSort | ||
{ | ||
DisplayName, | ||
UniqueName, | ||
UpdatedOn | ||
} |
20 changes: 20 additions & 0 deletions
20
backend/src/Logitar.Cms.Contracts/FieldTypes/FieldTypeSortOption.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.FieldTypes; | ||
|
||
public record FieldTypeSortOption : SortOption | ||
{ | ||
public new FieldTypeSort Field | ||
{ | ||
get => Enum.Parse<FieldTypeSort>(base.Field); | ||
set => base.Field = value.ToString(); | ||
} | ||
|
||
public FieldTypeSortOption() : this(FieldTypeSort.UpdatedOn, isDescending: true) | ||
{ | ||
} | ||
|
||
public FieldTypeSortOption(FieldTypeSort field, bool isDescending = false) : base(field.ToString(), isDescending) | ||
{ | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
backend/src/Logitar.Cms.Contracts/FieldTypes/SearchFieldTypesPayload.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.FieldTypes; | ||
|
||
public record SearchFieldTypesPayload : SearchPayload | ||
{ | ||
public DataType? DataType { get; set; } | ||
|
||
public new List<FieldTypeSortOption>? Sort { get; set; } | ||
} |
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/FieldTypes/Queries/SearchFieldTypesQuery.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.FieldTypes; | ||
using Logitar.Cms.Contracts.Search; | ||
using MediatR; | ||
|
||
namespace Logitar.Cms.Core.FieldTypes.Queries; | ||
|
||
public record SearchFieldTypesQuery(SearchFieldTypesPayload Payload) : IRequest<SearchResults<FieldType>>; |
20 changes: 20 additions & 0 deletions
20
backend/src/Logitar.Cms.Core/FieldTypes/Queries/SearchFieldTypesQueryHandler.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.FieldTypes; | ||
using Logitar.Cms.Contracts.Search; | ||
using MediatR; | ||
|
||
namespace Logitar.Cms.Core.FieldTypes.Queries; | ||
|
||
internal class SearchFieldTypesQueryHandler : IRequestHandler<SearchFieldTypesQuery, SearchResults<FieldType>> | ||
{ | ||
private readonly IFieldTypeQuerier _fieldTypeQuerier; | ||
|
||
public SearchFieldTypesQueryHandler(IFieldTypeQuerier fieldTypeQuerier) | ||
{ | ||
_fieldTypeQuerier = fieldTypeQuerier; | ||
} | ||
|
||
public async Task<SearchResults<FieldType>> Handle(SearchFieldTypesQuery 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/FieldTypes/SearchFieldTypesParameters.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.FieldTypes; | ||
using Logitar.Cms.Contracts.Search; | ||
using Logitar.Cms.Web.Models.Search; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace Logitar.Cms.Web.Models.FieldTypes; | ||
|
||
public record SearchFieldTypesParameters : SearchParameters | ||
{ | ||
[FromQuery(Name = "type")] | ||
public DataType? DataType { get; set; } | ||
|
||
public SearchFieldTypesPayload ToPayload() | ||
{ | ||
SearchFieldTypesPayload payload = new() | ||
{ | ||
DataType = DataType | ||
}; | ||
|
||
FillPayload(payload); | ||
|
||
List<SortOption>? sortOptions = ((SearchPayload)payload).Sort; | ||
if (sortOptions != null) | ||
{ | ||
payload.Sort = new List<FieldTypeSortOption>(capacity: sortOptions.Count); | ||
foreach (SortOption sort in sortOptions) | ||
{ | ||
if (Enum.TryParse(sort.Field, out FieldTypeSort field)) | ||
{ | ||
payload.Sort.Add(new FieldTypeSortOption(field, sort.IsDescending)); | ||
} | ||
} | ||
} | ||
|
||
return payload; | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
...Logitar.Cms.Core.IntegrationTests/FieldTypes/Queries/SearchFieldTypesQueryHandlerTests.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,77 @@ | ||
using Logitar.Cms.Contracts.FieldTypes; | ||
using Logitar.Cms.Contracts.Search; | ||
using Logitar.Cms.Core.FieldTypes.Properties; | ||
using Logitar.Identity.Contracts.Settings; | ||
using Logitar.Identity.Domain.Shared; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Logitar.Cms.Core.FieldTypes.Queries; | ||
|
||
[Trait(Traits.Category, Categories.Integration)] | ||
public class SearchFieldTypesQueryHandlerTests : IntegrationTests | ||
{ | ||
private readonly IFieldTypeRepository _fieldTypeRepository; | ||
|
||
private readonly FieldTypeAggregate _contents; | ||
private readonly FieldTypeAggregate _metaTitle; | ||
private readonly FieldTypeAggregate _slug; | ||
private readonly FieldTypeAggregate _subTitle; | ||
private readonly FieldTypeAggregate _title; | ||
|
||
public SearchFieldTypesQueryHandlerTests() : base() | ||
{ | ||
_fieldTypeRepository = ServiceProvider.GetRequiredService<IFieldTypeRepository>(); | ||
|
||
IUniqueNameSettings uniqueNameSettings = FieldTypeAggregate.UniqueNameSettings; | ||
_contents = new(new UniqueNameUnit(uniqueNameSettings, "ArticleContents"), new ReadOnlyTextProperties()); | ||
_metaTitle = new(new UniqueNameUnit(uniqueNameSettings, "ArticleMetaTitle"), new ReadOnlyStringProperties()); | ||
_slug = new(new UniqueNameUnit(uniqueNameSettings, "Slug"), new ReadOnlyStringProperties()); | ||
_subTitle = new(new UniqueNameUnit(uniqueNameSettings, "ArticleSubTitle"), new ReadOnlyStringProperties()); | ||
_title = new(new UniqueNameUnit(uniqueNameSettings, "ArticleTitle"), new ReadOnlyStringProperties()); | ||
} | ||
|
||
public override async Task InitializeAsync() | ||
{ | ||
await base.InitializeAsync(); | ||
|
||
await _fieldTypeRepository.SaveAsync([_contents, _metaTitle, _slug, _subTitle, _title]); | ||
} | ||
|
||
[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() | ||
{ | ||
SearchFieldTypesPayload payload = new() | ||
{ | ||
Search = new TextSearch([new SearchTerm("%test%")]) | ||
}; | ||
SearchFieldTypesQuery query = new(payload); | ||
|
||
SearchResults<FieldType> 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() | ||
{ | ||
SearchFieldTypesPayload payload = new() | ||
{ | ||
DataType = DataType.String, | ||
IdIn = (await _fieldTypeRepository.LoadAsync()).Select(fieldType => fieldType.Id.ToGuid()).ToList(), | ||
Search = new TextSearch([new SearchTerm("%title"), new SearchTerm("con%")], SearchOperator.Or), | ||
Sort = [new FieldTypeSortOption(FieldTypeSort.UniqueName, isDescending: false)], | ||
Skip = 1, | ||
Limit = 1 | ||
}; | ||
payload.IdIn.Add(Guid.Empty); | ||
payload.IdIn.Remove(_metaTitle.Id.ToGuid()); | ||
SearchFieldTypesQuery query = new(payload); | ||
|
||
SearchResults<FieldType> results = await Pipeline.ExecuteAsync(query); | ||
|
||
Assert.Equal(2, results.Total); | ||
FieldType fieldType = Assert.Single(results.Items); | ||
Assert.Equal(_title.Id.ToGuid(), fieldType.Id); | ||
} | ||
} |