Browse Source

CmsKit - Refactor CancellationToken usage

pull/7848/head
enisn 6 years ago
parent
commit
0ae018311a
  1. 4
      modules/cms-kit/src/Volo.CmsKit.Admin.Application/Volo/CmsKit/Admin/Tags/EntityTagAdminAppService.cs
  2. 3
      modules/cms-kit/src/Volo.CmsKit.Admin.Application/Volo/CmsKit/Admin/Tags/TagAdminAppService.cs
  3. 34
      modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/TagManager.cs

4
modules/cms-kit/src/Volo.CmsKit.Admin.Application/Volo/CmsKit/Admin/Tags/EntityTagAdminAppService.cs

@ -33,7 +33,7 @@ namespace Volo.CmsKit.Admin.Tags
await CheckPolicyAsync(definition.CreatePolicy); await CheckPolicyAsync(definition.CreatePolicy);
var tag = await TagManager.GetOrAddAsync(input.EntityType, input.TagName, CurrentTenant?.Id); var tag = await TagManager.GetOrAddAsync(input.EntityType, input.TagName);
await EntityTagManager.AddTagToEntityAsync( await EntityTagManager.AddTagToEntityAsync(
tag.Id, tag.Id,
@ -71,7 +71,7 @@ namespace Volo.CmsKit.Admin.Tags
foreach (var addedTag in addedTags) foreach (var addedTag in addedTags)
{ {
var tag = await TagManager.GetOrAddAsync(input.EntityType, addedTag, CurrentTenant?.Id); var tag = await TagManager.GetOrAddAsync(input.EntityType, addedTag);
await EntityTagManager.AddTagToEntityAsync(tag.Id, input.EntityType, input.EntityId, CurrentTenant?.Id); await EntityTagManager.AddTagToEntityAsync(tag.Id, input.EntityType, input.EntityId, CurrentTenant?.Id);
} }

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

@ -47,8 +47,7 @@ namespace Volo.CmsKit.Admin.Tags
var tag = await TagManager.InsertAsync( var tag = await TagManager.InsertAsync(
GuidGenerator.Create(), GuidGenerator.Create(),
input.EntityType, input.EntityType,
input.Name, input.Name);
CurrentTenant?.Id);
return await MapToGetOutputDtoAsync(tag); return await MapToGetOutputDtoAsync(tag);
} }

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

@ -3,6 +3,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Domain.Services; using Volo.Abp.Domain.Services;
namespace Volo.CmsKit.Tags namespace Volo.CmsKit.Tags
@ -18,54 +19,55 @@ namespace Volo.CmsKit.Tags
TagDefinitionStore = tagDefinitionStore; TagDefinitionStore = tagDefinitionStore;
} }
public virtual async Task<Tag> GetOrAddAsync([NotNull] string entityType, [NotNull] string name, public virtual async Task<Tag> GetOrAddAsync([NotNull] string entityType, [NotNull] string name)
Guid? tenantId = null, CancellationToken cancellationToken = default)
{ {
var entity = await TagRepository.FindAsync(entityType, name, tenantId, cancellationToken); var entity = await TagRepository.FindAsync(entityType, name, CurrentTenant.Id);
if (entity == null) if (entity == null)
{ {
entity = await InsertAsync(GuidGenerator.Create(), entityType, name, tenantId, cancellationToken); entity = await InsertAsync(GuidGenerator.Create(), entityType, name);
} }
return entity; return entity;
} }
public virtual async Task<Tag> InsertAsync(Guid id, [NotNull] string entityType, [NotNull] string name, public virtual async Task<Tag> InsertAsync(Guid id,
Guid? tenantId = null, CancellationToken cancellationToken = default) [NotNull] string entityType,
[NotNull] string name)
{ {
if (!await TagDefinitionStore.IsDefinedAsync(entityType)) if (!await TagDefinitionStore.IsDefinedAsync(entityType))
{ {
throw new EntityNotTaggableException(entityType); throw new EntityNotTaggableException(entityType);
} }
if (await TagRepository.AnyAsync(entityType, name, tenantId, cancellationToken)) if (await TagRepository.AnyAsync(entityType, name, CurrentTenant.Id))
{ {
throw new TagAlreadyExistException(entityType, name); throw new TagAlreadyExistException(entityType, name);
} }
return await TagRepository.InsertAsync(new Tag(id, entityType, name, tenantId), return await TagRepository.InsertAsync(
cancellationToken: cancellationToken); new Tag(id, entityType, name, CurrentTenant.Id));
} }
public virtual async Task<Tag> UpdateAsync(Guid id, [NotNull] string name, public virtual async Task<Tag> UpdateAsync(Guid id,
CancellationToken cancellationToken = default) [NotNull] string name)
{ {
var entity = await TagRepository.GetAsync(id, cancellationToken: cancellationToken); Check.NotNullOrEmpty(name, nameof(name));
var entity = await TagRepository.GetAsync(id);
if (name != entity.Name && if (name != entity.Name &&
await TagRepository.AnyAsync(entity.EntityType, name, entity.TenantId, cancellationToken)) await TagRepository.AnyAsync(entity.EntityType, name, entity.TenantId))
{ {
throw new TagAlreadyExistException(entity.EntityType, name); throw new TagAlreadyExistException(entity.EntityType, name);
} }
entity.SetName(name); entity.SetName(name);
return await TagRepository.UpdateAsync(entity, cancellationToken: cancellationToken); return await TagRepository.UpdateAsync(entity);
} }
public virtual Task<List<TagEntityTypeDefiniton>> GetTagDefinitionsAsync( public virtual Task<List<TagEntityTypeDefiniton>> GetTagDefinitionsAsync()
CancellationToken cancellationToken = default)
{ {
return TagDefinitionStore.GetTagEntityTypeDefinitionListAsync(); return TagDefinitionStore.GetTagEntityTypeDefinitionListAsync();
} }

Loading…
Cancel
Save