From 65e481b0158039a768ceac4e754c91fb4042c83d Mon Sep 17 00:00:00 2001 From: enisn Date: Thu, 24 Dec 2020 17:07:07 +0300 Subject: [PATCH] CmsKit Tags AppServices & ViewComponent --- .../Volo/CmsKit/Tags/GetRelatedTagsInput.cs | 16 +++++ .../Volo/CmsKit/Tags/ITagAppService.cs | 11 ++++ .../Volo/CmsKit/Tags/TagDto.cs | 14 +++++ ...msKitCommonApplicationAutoMapperProfile.cs | 3 + .../Volo/CmsKit/Tags/TagAppService.cs | 58 +++++++++++++++++++ .../CmsKit/Controllers/Tags/TagController.cs | 22 +++++++ .../Shared/Components/Tags/Default.cshtml | 13 +++++ .../Components/Tags/TagViewComponent.cs | 51 ++++++++++++++++ .../CmsKit/Shared/Components/Tags/default.css | 4 ++ .../Volo.CmsKit.Common.Web.csproj | 4 ++ 10 files changed, 196 insertions(+) create mode 100644 modules/cms-kit/src/Volo.CmsKit.Common.Application.Contracts/Volo/CmsKit/Tags/GetRelatedTagsInput.cs create mode 100644 modules/cms-kit/src/Volo.CmsKit.Common.Application.Contracts/Volo/CmsKit/Tags/ITagAppService.cs create mode 100644 modules/cms-kit/src/Volo.CmsKit.Common.Application.Contracts/Volo/CmsKit/Tags/TagDto.cs create mode 100644 modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/Tags/TagAppService.cs create mode 100644 modules/cms-kit/src/Volo.CmsKit.Common.HttpApi/Volo/CmsKit/Controllers/Tags/TagController.cs create mode 100644 modules/cms-kit/src/Volo.CmsKit.Common.Web/Pages/CmsKit/Shared/Components/Tags/Default.cshtml create mode 100644 modules/cms-kit/src/Volo.CmsKit.Common.Web/Pages/CmsKit/Shared/Components/Tags/TagViewComponent.cs create mode 100644 modules/cms-kit/src/Volo.CmsKit.Common.Web/Pages/CmsKit/Shared/Components/Tags/default.css diff --git a/modules/cms-kit/src/Volo.CmsKit.Common.Application.Contracts/Volo/CmsKit/Tags/GetRelatedTagsInput.cs b/modules/cms-kit/src/Volo.CmsKit.Common.Application.Contracts/Volo/CmsKit/Tags/GetRelatedTagsInput.cs new file mode 100644 index 0000000000..0019bbd661 --- /dev/null +++ b/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 Tags { get; set; } + } +} diff --git a/modules/cms-kit/src/Volo.CmsKit.Common.Application.Contracts/Volo/CmsKit/Tags/ITagAppService.cs b/modules/cms-kit/src/Volo.CmsKit.Common.Application.Contracts/Volo/CmsKit/Tags/ITagAppService.cs new file mode 100644 index 0000000000..f831d0f735 --- /dev/null +++ b/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> GetAllRelatedTagsAsync(GetRelatedTagsInput input); + } +} diff --git a/modules/cms-kit/src/Volo.CmsKit.Common.Application.Contracts/Volo/CmsKit/Tags/TagDto.cs b/modules/cms-kit/src/Volo.CmsKit.Common.Application.Contracts/Volo/CmsKit/Tags/TagDto.cs new file mode 100644 index 0000000000..4b9e4a5897 --- /dev/null +++ b/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 + { + public string EntityType { get; set; } + + public string Name { get; protected set; } + + public string HexColor { get; protected set; } + } +} diff --git a/modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/CmsKitCommonApplicationAutoMapperProfile.cs b/modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/CmsKitCommonApplicationAutoMapperProfile.cs index b7cb5e2210..c7be0fcc69 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/CmsKitCommonApplicationAutoMapperProfile.cs +++ b/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(); + + CreateMap(); } } } diff --git a/modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/Tags/TagAppService.cs b/modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/Tags/TagAppService.cs new file mode 100644 index 0000000000..c9ddef9629 --- /dev/null +++ b/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> 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>(entities); + } + } +} diff --git a/modules/cms-kit/src/Volo.CmsKit.Common.HttpApi/Volo/CmsKit/Controllers/Tags/TagController.cs b/modules/cms-kit/src/Volo.CmsKit.Common.HttpApi/Volo/CmsKit/Controllers/Tags/TagController.cs new file mode 100644 index 0000000000..b45d7fc6d4 --- /dev/null +++ b/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> GetAllRelatedTagsAsync(GetRelatedTagsInput input) + { + return TagAppService.GetAllRelatedTagsAsync(input); + } + } +} diff --git a/modules/cms-kit/src/Volo.CmsKit.Common.Web/Pages/CmsKit/Shared/Components/Tags/Default.cshtml b/modules/cms-kit/src/Volo.CmsKit.Common.Web/Pages/CmsKit/Shared/Components/Tags/Default.cshtml new file mode 100644 index 0000000000..e0e9a034a3 --- /dev/null +++ b/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) + { + + @tag.Name + + } +} \ No newline at end of file diff --git a/modules/cms-kit/src/Volo.CmsKit.Common.Web/Pages/CmsKit/Shared/Components/Tags/TagViewComponent.cs b/modules/cms-kit/src/Volo.CmsKit.Common.Web/Pages/CmsKit/Shared/Components/Tags/TagViewComponent.cs new file mode 100644 index 0000000000..83ad0e7e3a --- /dev/null +++ b/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 InvokeAsync( + string entityType, + string entityId, + IEnumerable 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 Tags { get; set; } + public string EntityId { get; set; } + public string EntityType { get; set; } + } + } +} diff --git a/modules/cms-kit/src/Volo.CmsKit.Common.Web/Pages/CmsKit/Shared/Components/Tags/default.css b/modules/cms-kit/src/Volo.CmsKit.Common.Web/Pages/CmsKit/Shared/Components/Tags/default.css new file mode 100644 index 0000000000..e3f6d69107 --- /dev/null +++ b/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; +} diff --git a/modules/cms-kit/src/Volo.CmsKit.Common.Web/Volo.CmsKit.Common.Web.csproj b/modules/cms-kit/src/Volo.CmsKit.Common.Web/Volo.CmsKit.Common.Web.csproj index 620274f4f3..132e584fc0 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Common.Web/Volo.CmsKit.Common.Web.csproj +++ b/modules/cms-kit/src/Volo.CmsKit.Common.Web/Volo.CmsKit.Common.Web.csproj @@ -35,4 +35,8 @@ + + + +