Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CmsKit - Refactoring for TagAdminAppService #8143

Merged
merged 2 commits into from
Mar 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public CmsKitAdminApplicationAutoMapperProfile()

CreateMap<TagEntityTypeDefiniton, TagDefinitionDto>(MemberList.Destination);

CreateMap<Tag, TagDto>();

CreateMap<MediaDescriptor, MediaDescriptorDto>();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.CmsKit.Permissions;
Expand All @@ -12,39 +13,27 @@
namespace Volo.CmsKit.Admin.Tags
{
[Authorize(CmsKitAdminPermissions.Tags.Default)]
public class TagAdminAppService :
CrudAppService<
Tag,
TagDto,
Guid,
TagGetListInput,
TagCreateDto,
TagUpdateDto>,
ITagAdminAppService
public class TagAdminAppService : CmsKitAppServiceBase, ITagAdminAppService
{
protected ITagRepository Repository { get; }
protected TagManager TagManager { get; }
protected ITagDefinitionStore TagDefinitionStore { get; }
protected IStringLocalizerFactory StringLocalizerFactory { get; }

public TagAdminAppService(
IRepository<Tag, Guid> repository,
ITagRepository repository,
TagManager tagManager,
ITagDefinitionStore tagDefinitionStore,
IStringLocalizerFactory stringLocalizerFactory) : base(repository)
IStringLocalizerFactory stringLocalizerFactory)
{
Repository = repository;
TagManager = tagManager;
TagDefinitionStore = tagDefinitionStore;
StringLocalizerFactory = stringLocalizerFactory;

GetListPolicyName = CmsKitAdminPermissions.Tags.Default;
GetPolicyName = CmsKitAdminPermissions.Tags.Default;
CreatePolicyName = CmsKitAdminPermissions.Tags.Create;
UpdatePolicyName = CmsKitAdminPermissions.Tags.Update;
DeletePolicyName = CmsKitAdminPermissions.Tags.Delete;
}

[Authorize(CmsKitAdminPermissions.Tags.Create)]
public override async Task<TagDto> CreateAsync(TagCreateDto input)
public async Task<TagDto> CreateAsync(TagCreateDto input)
{
var tag = await TagManager.CreateAsync(
GuidGenerator.Create(),
Expand All @@ -53,43 +42,60 @@ public override async Task<TagDto> CreateAsync(TagCreateDto input)

await Repository.InsertAsync(tag);

return await MapToGetOutputDtoAsync(tag);
return ObjectMapper.Map<Tag, TagDto>(tag);
}

[Authorize(CmsKitAdminPermissions.Tags.Update)]
public override async Task<TagDto> UpdateAsync(Guid id, TagUpdateDto input)
public async Task<TagDto> UpdateAsync(Guid id, TagUpdateDto input)
{
var tag = await TagManager.UpdateAsync(
id,
input.Name);

await Repository.UpdateAsync(tag);

return await MapToGetOutputDtoAsync(tag);
}

protected override async Task<IQueryable<Tag>> CreateFilteredQueryAsync(TagGetListInput input)
{
return (await base.CreateFilteredQueryAsync(input))
.WhereIf(
!input.Filter.IsNullOrEmpty(),
x =>
x.Name.ToLower().Contains(input.Filter) ||
x.EntityType.ToLower().Contains(input.Filter));
return ObjectMapper.Map<Tag, TagDto>(tag);
}

[Authorize(CmsKitAdminPermissions.Tags.Default)]
public virtual async Task<List<TagDefinitionDto>> GetTagDefinitionsAsync()
{
var definitions = await TagDefinitionStore.GetTagEntityTypeDefinitionListAsync();

return definitions
.Select(s =>
.Select(s =>
new TagDefinitionDto
{
EntityType = s.EntityType,
EntityType = s.EntityType,
DisplayName = s.DisplayName?.Localize(StringLocalizerFactory) ?? s.EntityType
})
.ToList();
}

[Authorize(CmsKitAdminPermissions.Tags.Default)]
public async Task<TagDto> GetAsync(Guid id)
{
var tag = await Repository.GetAsync(id);

return ObjectMapper.Map<Tag, TagDto>(tag);
}

[Authorize(CmsKitAdminPermissions.Tags.Default)]
public async Task<PagedResultDto<TagDto>> GetListAsync(TagGetListInput input)
{
var tags = await Repository.GetListAsync(input.Filter);
var count = await Repository.GetCountAsync(input.Filter);

return new PagedResultDto<TagDto>(
count,
ObjectMapper.Map<List<Tag>, List<TagDto>>(tags)
);
}

[Authorize(CmsKitAdminPermissions.Tags.Delete)]
public async Task DeleteAsync(Guid id)
{
await Repository.DeleteAsync(id);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ public class TagDto : EntityDto<Guid>
{
public string EntityType { get; set; }

public string Name { get; protected set; }
public string Name { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public interface ITagRepository : IBasicRepository<Tag, Guid>
[NotNull] string name,
CancellationToken cancellationToken = default);

Task<List<Tag>> GetListAsync(string filter);

Task<int> GetCountAsync(string filter);

Task<List<Tag>> GetAllRelatedTagsAsync(
[NotNull] string entityType,
[NotNull] string entityId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public EfCoreTagRepository(IDbContextProvider<ICmsKitDbContext> dbContextProvide
{
Check.NotNullOrEmpty(entityType, nameof(entityType));
Check.NotNullOrEmpty(name, nameof(name));

return await (await GetDbSetAsync()).AnyAsync(x =>
x.EntityType == entityType &&
x.Name == name,
Expand All @@ -39,7 +39,7 @@ public EfCoreTagRepository(IDbContextProvider<ICmsKitDbContext> dbContextProvide
{
Check.NotNullOrEmpty(entityType, nameof(entityType));
Check.NotNullOrEmpty(name, nameof(name));

return GetAsync(x =>
x.EntityType == entityType &&
x.Name == name,
Expand All @@ -53,7 +53,7 @@ public EfCoreTagRepository(IDbContextProvider<ICmsKitDbContext> dbContextProvide
{
Check.NotNullOrEmpty(entityType, nameof(entityType));
Check.NotNullOrEmpty(name, nameof(name));

return FindAsync(x =>
x.EntityType == entityType &&
x.Name == name,
Expand All @@ -67,7 +67,7 @@ public EfCoreTagRepository(IDbContextProvider<ICmsKitDbContext> dbContextProvide
{
Check.NotNullOrEmpty(entityType, nameof(entityType));
Check.NotNullOrEmpty(entityId, nameof(entityId));

var entityTagIds = await (await GetDbContextAsync()).Set<EntityTag>()
.Where(q => q.EntityId == entityId)
.Select(q => q.TagId)
Expand All @@ -79,5 +79,25 @@ public EfCoreTagRepository(IDbContextProvider<ICmsKitDbContext> dbContextProvide

return await query.ToListAsync(cancellationToken: GetCancellationToken(cancellationToken));
}

public async Task<List<Tag>> GetListAsync(string filter)
{
return await (await GetQueryableByFilterAsync(filter)).ToListAsync();
}

public async Task<int> GetCountAsync(string filter)
{
return await (await GetQueryableByFilterAsync(filter)).CountAsync();
}

private async Task<IQueryable<Tag>> GetQueryableByFilterAsync(string filter)
{
return (await GetQueryableAsync())
.WhereIf(
!filter.IsNullOrEmpty(),
x =>
x.Name.ToLower().Contains(filter.ToLower()) ||
x.EntityType.ToLower().Contains(filter.ToLower()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public MongoTagRepository(IMongoDbContextProvider<ICmsKitMongoDbContext> dbConte
{
Check.NotNullOrEmpty(entityType, nameof(entityType));
Check.NotNullOrEmpty(name, nameof(name));

return await (await GetMongoQueryableAsync(cancellationToken))
.AnyAsync(x =>
x.EntityType == entityType &&
Expand All @@ -52,7 +52,7 @@ public MongoTagRepository(IMongoDbContextProvider<ICmsKitMongoDbContext> dbConte
{
Check.NotNullOrEmpty(entityType, nameof(entityType));
Check.NotNullOrEmpty(name, nameof(name));

return FindAsync(x =>
x.EntityType == entityType &&
x.Name == name,
Expand All @@ -66,7 +66,7 @@ public MongoTagRepository(IMongoDbContextProvider<ICmsKitMongoDbContext> dbConte
{
Check.NotNullOrEmpty(entityType, nameof(entityType));
Check.NotNullOrEmpty(entityId, nameof(entityId));

var entityTagIds = await (await GetDbContextAsync(cancellationToken)).EntityTags.AsQueryable()
.Where(q => q.EntityId == entityId)
.Select(q => q.TagId)
Expand All @@ -80,5 +80,30 @@ public MongoTagRepository(IMongoDbContextProvider<ICmsKitMongoDbContext> dbConte
var result = await query.ToListAsync(cancellationToken: GetCancellationToken(cancellationToken));
return result;
}


public async Task<List<Tag>> GetListAsync(string filter)
{
return await (await GetQueryableByFilterAsync(filter)).ToListAsync();
}

public async Task<int> GetCountAsync(string filter)
{
return await (await GetQueryableByFilterAsync(filter)).CountAsync();
}

private async Task<IMongoQueryable<Tag>> GetQueryableByFilterAsync(string filter)
{
var mongoQueryable = await GetMongoQueryableAsync();

if (!filter.IsNullOrWhiteSpace())
{
mongoQueryable = mongoQueryable.Where(x =>
x.Name.ToLower().Contains(filter) ||
x.EntityType.ToLower().Contains(filter));
}

return mongoQueryable;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,22 @@ public async Task Should_Not_Get_Related_Tags()

tags.Count.ShouldBe(0);
}

[Fact]
public async Task Should_GetList_With_Filter()
{
var tags = await _tagRepository.GetListAsync(_cmsKitTestData.TagName_1);

tags.ShouldNotBeNull();
tags.Count.ShouldBe(1);
}

[Fact]
public async Task Should_GetCount_With_Filter()
{
var count = await _tagRepository.GetCountAsync(_cmsKitTestData.TagName_1);

count.ShouldBe(1);
}
}
}