Browse Source

CmsKit Tags AppServices & ViewComponent

pull/6821/head
enisn 5 years ago
parent
commit
65e481b015
  1. 16
      modules/cms-kit/src/Volo.CmsKit.Common.Application.Contracts/Volo/CmsKit/Tags/GetRelatedTagsInput.cs
  2. 11
      modules/cms-kit/src/Volo.CmsKit.Common.Application.Contracts/Volo/CmsKit/Tags/ITagAppService.cs
  3. 14
      modules/cms-kit/src/Volo.CmsKit.Common.Application.Contracts/Volo/CmsKit/Tags/TagDto.cs
  4. 3
      modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/CmsKitCommonApplicationAutoMapperProfile.cs
  5. 58
      modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/Tags/TagAppService.cs
  6. 22
      modules/cms-kit/src/Volo.CmsKit.Common.HttpApi/Volo/CmsKit/Controllers/Tags/TagController.cs
  7. 13
      modules/cms-kit/src/Volo.CmsKit.Common.Web/Pages/CmsKit/Shared/Components/Tags/Default.cshtml
  8. 51
      modules/cms-kit/src/Volo.CmsKit.Common.Web/Pages/CmsKit/Shared/Components/Tags/TagViewComponent.cs
  9. 4
      modules/cms-kit/src/Volo.CmsKit.Common.Web/Pages/CmsKit/Shared/Components/Tags/default.css
  10. 4
      modules/cms-kit/src/Volo.CmsKit.Common.Web/Volo.CmsKit.Common.Web.csproj

16
modules/cms-kit/src/Volo.CmsKit.Common.Application.Contracts/Volo/CmsKit/Tags/GetRelatedTagsInput.cs

@ -0,0 +1,16 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace Volo.CmsKit.Tags
{
public class GetRelatedTagsInput
{
[Required]
public string EntityType { get; set; }
[Required]
public string EntityId { get; set; }
public List<string> Tags { get; set; }
}
}

11
modules/cms-kit/src/Volo.CmsKit.Common.Application.Contracts/Volo/CmsKit/Tags/ITagAppService.cs

@ -0,0 +1,11 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
namespace Volo.CmsKit.Tags
{
public interface ITagAppService : IApplicationService
{
Task<List<TagDto>> GetAllRelatedTagsAsync(GetRelatedTagsInput input);
}
}

14
modules/cms-kit/src/Volo.CmsKit.Common.Application.Contracts/Volo/CmsKit/Tags/TagDto.cs

@ -0,0 +1,14 @@
using System;
using Volo.Abp.Application.Dtos;
namespace Volo.CmsKit.Tags
{
public class TagDto : EntityDto<Guid>
{
public string EntityType { get; set; }
public string Name { get; protected set; }
public string HexColor { get; protected set; }
}
}

3
modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/CmsKitCommonApplicationAutoMapperProfile.cs

@ -1,5 +1,6 @@
using AutoMapper;
using Volo.CmsKit.Contents;
using Volo.CmsKit.Tags;
namespace Volo.CmsKit.Common.Application.Volo.CmsKit
{
@ -8,6 +9,8 @@ namespace Volo.CmsKit.Common.Application.Volo.CmsKit
public CmsKitCommonApplicationAutoMapperProfile()
{
CreateMap<Content, ContentDto>();
CreateMap<Tag, TagDto>();
}
}
}

58
modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/Tags/TagAppService.cs

@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Volo.CmsKit.Tags
{
public class TagAppService : CmsKitAppServiceBase, ITagAppService
{
protected readonly ITagManager TagManager;
protected readonly ITagRepository TagRepository;
protected readonly IEntityTagRepository EntityTagRepository;
public TagAppService(
ITagManager tagManager,
ITagRepository tagRepository, IEntityTagRepository entityTagRepository)
{
TagManager = tagManager;
TagRepository = tagRepository;
EntityTagRepository = entityTagRepository;
}
public virtual async Task<List<TagDto>> GetAllRelatedTagsAsync(GetRelatedTagsInput input)
{
var entities = await TagRepository.GetAllRelatedTagsAsync(
input.EntityType,
input.EntityId,
CurrentTenant.Id);
if (input.Tags?.Count > 0)
{
var nonExisting = input.Tags
.Where(x =>
!entities.Any(a =>
a.Name.Equals(x.Trim(),
StringComparison.InvariantCultureIgnoreCase)));
foreach (var tag in nonExisting)
{
var insertedTag = await TagManager.GetOrAddAsync(
input.EntityType,
tag,
tenantId: CurrentTenant?.Id);
await EntityTagRepository.InsertAsync(
new EntityTag(
insertedTag.Id,
input.EntityId,
CurrentTenant?.Id));
entities.Add(insertedTag);
}
}
return ObjectMapper.Map<List<Tag>, List<TagDto>>(entities);
}
}
}

22
modules/cms-kit/src/Volo.CmsKit.Common.HttpApi/Volo/CmsKit/Controllers/Tags/TagController.cs

@ -0,0 +1,22 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.CmsKit.Controllers;
using Volo.CmsKit.Tags;
namespace Volo.CmsKit.Common.HttpApi.Volo.CmsKit.Controllers.Tags
{
public class TagController : CmsKitControllerBase, ITagAppService
{
protected readonly ITagAppService TagAppService;
public TagController(ITagAppService tagAppService)
{
TagAppService = tagAppService;
}
public Task<List<TagDto>> GetAllRelatedTagsAsync(GetRelatedTagsInput input)
{
return TagAppService.GetAllRelatedTagsAsync(input);
}
}
}

13
modules/cms-kit/src/Volo.CmsKit.Common.Web/Pages/CmsKit/Shared/Components/Tags/Default.cshtml

@ -0,0 +1,13 @@
@using Volo.CmsKit.Web.Pages.CmsKit.Shared.Components.Tags
@model TagViewComponent.TagViewModel
@if (Model.Tags != null)
{
foreach (var tag in Model.Tags)
{
<span class="cmskit-tag" data-color="#@tag.HexColor">
@tag.Name
</span>
}
}

51
modules/cms-kit/src/Volo.CmsKit.Common.Web/Pages/CmsKit/Shared/Components/Tags/TagViewComponent.cs

@ -0,0 +1,51 @@
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.AspNetCore.Mvc;
using Volo.CmsKit.Tags;
namespace Volo.CmsKit.Web.Pages.CmsKit.Shared.Components.Tags
{
[ViewComponent(Name = "CmsTags")]
public class TagViewComponent : AbpViewComponent
{
protected readonly ITagAppService TagAppService;
public TagViewComponent(ITagAppService tagAppService)
{
TagAppService = tagAppService;
}
public virtual async Task<IViewComponentResult> InvokeAsync(
string entityType,
string entityId,
IEnumerable<string> tags = null)
{
var tagDtos = await TagAppService.GetAllRelatedTagsAsync(new GetRelatedTagsInput
{
EntityId = entityId,
EntityType = entityType,
Tags = tags?.ToList()
});
var viewModel = new TagViewModel
{
EntityId = entityId,
EntityType = entityType,
Tags = tagDtos
};
return View("~/Pages/CmsKit/Shared/Components/Tags/Default.cshtml", viewModel);
}
public class TagViewModel
{
public List<TagDto> Tags { get; set; }
public string EntityId { get; set; }
public string EntityType { get; set; }
}
}
}

4
modules/cms-kit/src/Volo.CmsKit.Common.Web/Pages/CmsKit/Shared/Components/Tags/default.css

@ -0,0 +1,4 @@
.cmskit-tag {
border-radius: 2px;
border: 1px solid;
}

4
modules/cms-kit/src/Volo.CmsKit.Common.Web/Volo.CmsKit.Common.Web.csproj

@ -35,4 +35,8 @@
<Content Remove="Components\**\*.css" />
</ItemGroup>
<ItemGroup>
<None Remove="Pages\CmsKit\Shared\Components\Tags\default.css" />
</ItemGroup>
</Project>

Loading…
Cancel
Save