Browse Source

CmsKit - Remove Db write operation from TagManager.Update

pull/7848/head
enisn 5 years ago
parent
commit
2374b91c33
  1. 13
      modules/cms-kit/src/Volo.CmsKit.Admin.Application/Volo/CmsKit/Admin/Tags/TagAdminAppService.cs
  2. 29
      modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/TagManager.cs
  3. 18
      modules/cms-kit/test/Volo.CmsKit.Domain.Tests/Tags/TagManager_Tests.cs
  4. 27
      modules/cms-kit/test/Volo.CmsKit.TestBase/CmsKitDataSeedContributor.cs

13
modules/cms-kit/src/Volo.CmsKit.Admin.Application/Volo/CmsKit/Admin/Tags/TagAdminAppService.cs

@ -46,11 +46,13 @@ namespace Volo.CmsKit.Admin.Tags
[Authorize(CmsKitAdminPermissions.Tags.Create)] [Authorize(CmsKitAdminPermissions.Tags.Create)]
public override async Task<TagDto> CreateAsync(TagCreateDto input) public override async Task<TagDto> CreateAsync(TagCreateDto input)
{ {
var tag = await TagManager.InsertAsync( var tag = await TagManager.CreateAsync(
GuidGenerator.Create(), GuidGenerator.Create(),
input.EntityType, input.EntityType,
input.Name); input.Name);
await Repository.InsertAsync(tag);
return await MapToGetOutputDtoAsync(tag); return await MapToGetOutputDtoAsync(tag);
} }
@ -61,11 +63,14 @@ namespace Volo.CmsKit.Admin.Tags
id, id,
input.Name); input.Name);
await Repository.UpdateAsync(tag);
return await MapToGetOutputDtoAsync(tag); return await MapToGetOutputDtoAsync(tag);
} }
protected override IQueryable<Tag> CreateFilteredQuery(TagGetListInput input)
protected override async Task<IQueryable<Tag>> CreateFilteredQueryAsync(TagGetListInput input)
{ {
return base.CreateFilteredQuery(input) return (await base.CreateFilteredQueryAsync(input))
.WhereIf( .WhereIf(
!input.Filter.IsNullOrEmpty(), !input.Filter.IsNullOrEmpty(),
x => x =>

29
modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/TagManager.cs

@ -21,11 +21,18 @@ namespace Volo.CmsKit.Tags
public virtual async Task<Tag> GetOrAddAsync([NotNull] string entityType, [NotNull] string name) public virtual async Task<Tag> GetOrAddAsync([NotNull] string entityType, [NotNull] string name)
{ {
return await TagRepository.FindAsync(entityType, name) var tag = await TagRepository.FindAsync(entityType, name);
?? await InsertAsync(GuidGenerator.Create(), entityType, name);
if (tag == null)
{
tag = await CreateAsync(GuidGenerator.Create(), entityType, name);
await TagRepository.InsertAsync(tag);
}
return tag;
} }
public virtual async Task<Tag> InsertAsync(Guid id, public virtual async Task<Tag> CreateAsync(Guid id,
[NotNull] string entityType, [NotNull] string entityType,
[NotNull] string name) [NotNull] string name)
{ {
@ -39,8 +46,8 @@ namespace Volo.CmsKit.Tags
throw new TagAlreadyExistException(entityType, name); throw new TagAlreadyExistException(entityType, name);
} }
return await TagRepository.InsertAsync( return
new Tag(id, entityType, name, CurrentTenant.Id)); new Tag(id, entityType, name, CurrentTenant.Id);
} }
public virtual async Task<Tag> UpdateAsync(Guid id, public virtual async Task<Tag> UpdateAsync(Guid id,
@ -48,17 +55,17 @@ namespace Volo.CmsKit.Tags
{ {
Check.NotNullOrEmpty(name, nameof(name)); Check.NotNullOrEmpty(name, nameof(name));
var entity = await TagRepository.GetAsync(id); var tag = await TagRepository.GetAsync(id);
if (name != entity.Name && if (name != tag.Name &&
await TagRepository.AnyAsync(entity.EntityType, name)) await TagRepository.AnyAsync(tag.EntityType, name))
{ {
throw new TagAlreadyExistException(entity.EntityType, name); throw new TagAlreadyExistException(tag.EntityType, name);
} }
entity.SetName(name); tag.SetName(name);
return await TagRepository.UpdateAsync(entity); return tag;
} }
} }
} }

18
modules/cms-kit/test/Volo.CmsKit.Domain.Tests/Tags/TagManager_Tests.cs

@ -53,24 +53,23 @@ namespace Volo.CmsKit.Tags
} }
[Fact] [Fact]
public async Task ShouldInsertAsync() public async Task ShouldCreateAsync()
{ {
var tagName = "Freshly Created New Tag"; var tagName = "Freshly Created New Tag";
var tag = await _tagManager.InsertAsync(Guid.NewGuid(), _cmsKitTestData.EntityType1, tagName); var tag = await _tagManager.CreateAsync(Guid.NewGuid(), _cmsKitTestData.EntityType1, tagName);
tag.ShouldNotBeNull(); tag.ShouldNotBeNull();
var doesExist = await _tagRepository.AnyAsync(_cmsKitTestData.EntityType1, tagName); tag.Id.ShouldNotBe(Guid.Empty);
doesExist.ShouldBeTrue();
} }
[Fact] [Fact]
public async Task ShouldntInsertWithUnconfiguredEntityTypeAsync() public async Task ShouldntInsertWithUnconfiguredEntityTypeAsync()
{ {
var notConfiguredEntityType = "My.Namespace.SomeEntity"; var notConfiguredEntityType = "My.Namespace.SomeEntity";
var exception = await Should.ThrowAsync<EntityNotTaggableException>(async () => var exception = await Should.ThrowAsync<EntityNotTaggableException>(async () =>
await _tagManager.InsertAsync(Guid.NewGuid(), notConfiguredEntityType, "test")); await _tagManager.CreateAsync(Guid.NewGuid(), notConfiguredEntityType, "test"));
exception.ShouldNotBeNull(); exception.ShouldNotBeNull();
exception.Data[nameof(Tag.EntityType)].ShouldBe(notConfiguredEntityType); exception.Data[nameof(Tag.EntityType)].ShouldBe(notConfiguredEntityType);
@ -82,7 +81,7 @@ namespace Volo.CmsKit.Tags
var type = _cmsKitTestData.Content_1_EntityType; var type = _cmsKitTestData.Content_1_EntityType;
var name = _cmsKitTestData.Content_1_Tags[0]; var name = _cmsKitTestData.Content_1_Tags[0];
Should.Throw<Exception>(async () => await _tagManager.InsertAsync(Guid.NewGuid(), type, name)); Should.Throw<Exception>(async () => await _tagManager.CreateAsync(Guid.NewGuid(), type, name));
} }
[Fact] [Fact]
@ -94,11 +93,10 @@ namespace Volo.CmsKit.Tags
var tag = await _tagRepository.GetAsync(type, name); var tag = await _tagRepository.GetAsync(type, name);
await _tagManager.UpdateAsync(tag.Id, newName); var updatedTag = await _tagManager.UpdateAsync(tag.Id, newName);
var updatedTag = await _tagRepository.GetAsync(type, newName);
updatedTag.Id.ShouldBe(tag.Id); updatedTag.Id.ShouldBe(tag.Id);
updatedTag.Name.ShouldBe(newName);
} }
[Fact] [Fact]

27
modules/cms-kit/test/Volo.CmsKit.TestBase/CmsKitDataSeedContributor.cs

@ -36,6 +36,7 @@ namespace Volo.CmsKit
private readonly IContentRepository _contentRepository; private readonly IContentRepository _contentRepository;
private readonly EntityTagManager _entityTagManager; private readonly EntityTagManager _entityTagManager;
private readonly TagManager _tagManager; private readonly TagManager _tagManager;
private readonly ITagRepository _tagRepository;
private readonly IEntityTagRepository _entityTagRepository; private readonly IEntityTagRepository _entityTagRepository;
private readonly IPageRepository _pageRepository; private readonly IPageRepository _pageRepository;
private readonly IBlogRepository _blogRepository; private readonly IBlogRepository _blogRepository;
@ -56,6 +57,7 @@ namespace Volo.CmsKit
ICurrentTenant currentTenant, ICurrentTenant currentTenant,
IContentRepository contentRepository, IContentRepository contentRepository,
TagManager tagManager, TagManager tagManager,
ITagRepository tagRepository,
IEntityTagRepository entityTagRepository, IEntityTagRepository entityTagRepository,
IPageRepository pageRepository, IPageRepository pageRepository,
IBlogRepository blogRepository, IBlogRepository blogRepository,
@ -76,6 +78,7 @@ namespace Volo.CmsKit
_currentTenant = currentTenant; _currentTenant = currentTenant;
_contentRepository = contentRepository; _contentRepository = contentRepository;
_tagManager = tagManager; _tagManager = tagManager;
_tagRepository = tagRepository;
_entityTagManager = entityTagManager; _entityTagManager = entityTagManager;
_entityTagRepository = entityTagRepository; _entityTagRepository = entityTagRepository;
_pageRepository = pageRepository; _pageRepository = pageRepository;
@ -285,24 +288,40 @@ namespace Volo.CmsKit
private async Task SeedTagsAsync() private async Task SeedTagsAsync()
{ {
var created1 = await _tagManager.InsertAsync(_cmsKitTestData.TagId_1, _cmsKitTestData.EntityType1, _cmsKitTestData.TagName_1); var created1 = await _tagRepository.InsertAsync(
await _tagManager.CreateAsync(
_cmsKitTestData.TagId_1,
_cmsKitTestData.EntityType1,
_cmsKitTestData.TagName_1));
await _entityTagManager.AddTagToEntityAsync(created1.Id, created1.EntityType, _cmsKitTestData.EntityId1); await _entityTagManager.AddTagToEntityAsync(created1.Id, created1.EntityType, _cmsKitTestData.EntityId1);
var created2 = await _tagManager.InsertAsync(_cmsKitTestData.TagId_2, _cmsKitTestData.EntityType2, _cmsKitTestData.TagName_2); var created2 = await _tagRepository.InsertAsync(
await _tagManager.CreateAsync(
_cmsKitTestData.TagId_2,
_cmsKitTestData.EntityType2,
_cmsKitTestData.TagName_2));
await _entityTagManager.AddTagToEntityAsync(created2.Id, created2.EntityType, _cmsKitTestData.EntityId2); await _entityTagManager.AddTagToEntityAsync(created2.Id, created2.EntityType, _cmsKitTestData.EntityId2);
foreach (var tag in _cmsKitTestData.Content_1_Tags) foreach (var tag in _cmsKitTestData.Content_1_Tags)
{ {
var tagEntity = await _tagManager.InsertAsync(_guidGenerator.Create(), _cmsKitTestData.Content_1_EntityType, tag); var tagEntity = await _tagRepository.InsertAsync(
await _tagManager.CreateAsync(
_guidGenerator.Create(),
_cmsKitTestData.Content_1_EntityType,
tag));
await _entityTagManager.AddTagToEntityAsync(tagEntity.Id, _cmsKitTestData.Content_1_EntityType, _cmsKitTestData.Content_1_EntityId); await _entityTagManager.AddTagToEntityAsync(tagEntity.Id, _cmsKitTestData.Content_1_EntityType, _cmsKitTestData.Content_1_EntityId);
} }
foreach (var tag in _cmsKitTestData.Content_2_Tags) foreach (var tag in _cmsKitTestData.Content_2_Tags)
{ {
var tagEntity = await _tagManager.InsertAsync(_guidGenerator.Create(), _cmsKitTestData.Content_2_EntityType, tag); var tagEntity = await _tagRepository.InsertAsync(
await _tagManager.CreateAsync(
_guidGenerator.Create(),
_cmsKitTestData.Content_2_EntityType,
tag));
await _entityTagManager.AddTagToEntityAsync(tagEntity.Id, _cmsKitTestData.Content_2_EntityType, _cmsKitTestData.Content_2_EntityId); await _entityTagManager.AddTagToEntityAsync(tagEntity.Id, _cmsKitTestData.Content_2_EntityType, _cmsKitTestData.Content_2_EntityId);
} }

Loading…
Cancel
Save