Add Popular Tags View Component

pull/17110/head
Salih 2 years ago
parent 3ab1db1e38
commit 92561484cc

@ -1,11 +1,11 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.CmsKit.Tags;
namespace Volo.CmsKit.Tags;
public interface ITagAppService : IApplicationService
{
Task<List<TagDto>> GetAllRelatedTagsAsync(string entityType, string entityId);
}
Task<List<PopularTagDto>> GetPopularTagsAsync(string entityType, int maxCount);
}

@ -0,0 +1,10 @@
using System;
namespace Volo.CmsKit.Tags;
public class PopularTagDto
{
public Guid Id { get; set; }
public string Name { get; set; }
public int Count { get; set; }
}

@ -11,6 +11,8 @@ public class CmsKitCommonApplicationAutoMapperProfile : Profile
{
CreateMap<Tag, TagDto>().MapExtraProperties();
CreateMap<PopularTag, PopularTagDto>();
CreateMap<CmsUser, CmsUserDto>().MapExtraProperties();
CreateMap<BlogFeature, BlogFeatureCacheItem>().MapExtraProperties();

@ -24,4 +24,14 @@ public class TagAppService : CmsKitAppServiceBase, ITagAppService
return ObjectMapper.Map<List<Tag>, List<TagDto>>(entities);
}
}
public async Task<List<PopularTagDto>> GetPopularTagsAsync(string entityType, int maxCount)
{
return ObjectMapper.Map<List<PopularTag>, List<PopularTagDto>>(
await TagRepository.GetPopularTagsAsync(
entityType,
maxCount
)
);
}
}

@ -36,4 +36,9 @@ public interface ITagRepository : IBasicRepository<Tag, Guid>
[NotNull] string entityType,
[NotNull] string entityId,
CancellationToken cancellationToken = default);
}
Task<List<PopularTag>> GetPopularTagsAsync(
[NotNull] string entityType,
int maxCount,
CancellationToken cancellationToken = default);
}

@ -0,0 +1,17 @@
using System;
namespace Volo.CmsKit.Tags;
public class PopularTag
{
public Guid Id { get; set; }
public string Name { get; set; }
public int Count { get; set; }
public PopularTag(Guid id, string name, int count)
{
Id = id;
Name = name;
Count = count;
}
}

@ -80,6 +80,23 @@ public class EfCoreTagRepository : EfCoreRepository<ICmsKitDbContext, Tag, Guid>
return await query.ToListAsync(cancellationToken: GetCancellationToken(cancellationToken));
}
public async Task<List<PopularTag>> GetPopularTagsAsync(
[NotNull] string entityType,
int maxCount,
CancellationToken cancellationToken = default)
{
return await (from tag in await GetDbSetAsync()
join entityTag in (await GetDbContextAsync()).Set<EntityTag>() on tag.Id equals entityTag.TagId
where tag.EntityType == entityType
select new { tag, entityTag } into tagEntityTag
group tagEntityTag by tagEntityTag.entityTag.TagId
into g
orderby g.Count() descending
select new PopularTag(g.Key, g.First().tag.Name, g.Count()))
.Take(maxCount)
.ToListAsync(cancellationToken: GetCancellationToken(cancellationToken));
}
public async Task<List<Tag>> GetListAsync(string filter, CancellationToken cancellationToken = default)
{
return await (await GetQueryableByFilterAsync(filter)).ToListAsync(GetCancellationToken(cancellationToken));

@ -81,6 +81,13 @@ public class MongoTagRepository : MongoDbRepository<ICmsKitMongoDbContext, Volo.
return result;
}
public async Task<List<PopularTag>> GetPopularTagsAsync(string entityType, int maxCount, CancellationToken cancellationToken = default)
{
//TODO: Implement this method
throw new NotImplementedException();
}
public async Task<List<Tag>> GetListAsync(string filter, CancellationToken cancellationToken = default)
{

@ -30,4 +30,11 @@ public class TagPublicController : CmsKitPublicControllerBase, ITagAppService
{
return TagAppService.GetAllRelatedTagsAsync(entityType, entityId);
}
[HttpGet]
[Route("popular/{entityType}/{maxCount:int}")]
public Task<List<PopularTagDto>> GetPopularTagsAsync(string entityType, int maxCount)
{
return TagAppService.GetPopularTagsAsync(entityType, maxCount);
}
}

@ -0,0 +1,29 @@
@inject IHtmlLocalizer<CmsKitResource> L
@using Microsoft.AspNetCore.Mvc.Localization
@using Volo.CmsKit.Localization
@model Volo.CmsKit.Public.Web.Pages.CmsKit.Shared.Components.PopularTags.PopularTagsViewComponent.PopularTagsViewModel
@if (Model.Tags != null && Model.Tags.Any())
{
<h4 class="mb-3 fw--6">@L["PopularTags"]</h4>
<div class="cms-tags-area my-3">
@foreach (var tag in Model.Tags)
{
if (Model.UrlFactory == null)
{
<span class="br-8 me-2 mb-2 p-2 d-inline-block cmskit-tag fw--5" style="background: #eeedf7; color: #766bc8">
@tag.Name
</span>
}
else
{
<a href="@Model.UrlFactory(tag)">
<span class="br-8 me-2 mb-2 p-2 d-inline-block cmskit-tag fw--5" style="background: #eeedf7; color: #766bc8">
@tag.Name
</span>
</a>
}
}
</div>
}

@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Uow;
using Volo.CmsKit.Tags;
namespace Volo.CmsKit.Public.Web.Pages.CmsKit.Shared.Components.PopularTags;
public class PopularTagsViewComponent : AbpViewComponent
{
private readonly ITagAppService _tagAppService;
public PopularTagsViewComponent(ITagAppService tagAppService)
{
_tagAppService = tagAppService;
}
public async Task<IViewComponentResult> InvokeAsync(string entityType, int maxCount, Func<PopularTagDto, string> urlFactory = null)
{
var model = new PopularTagsViewModel
{
Tags = await _tagAppService.GetPopularTagsAsync(entityType, maxCount),
UrlFactory = urlFactory
};
return View("~/Pages/CmsKit/Shared/Components/PopularTags/Default.cshtml", model);
}
public class PopularTagsViewModel
{
public List<PopularTagDto> Tags { get; set; }
public Func<PopularTagDto, string> UrlFactory { get; set; }
}
}

@ -129,4 +129,14 @@ public abstract class TagRepository_Test<TStartupModule> : CmsKitTestBase<TStart
count.ShouldBe(1);
}
[Fact]
public async Task Should_Get_Popular_Tags()
{
var popularTags = await _tagRepository.GetPopularTagsAsync(_cmsKitTestData.Content_1_EntityType, 2);
popularTags.Count.ShouldBe(2);
_cmsKitTestData.Content_1_Tags.Contains(popularTags[0].Name).ShouldBeTrue();
_cmsKitTestData.Content_1_Tags.Contains(popularTags[1].Name).ShouldBeTrue();
}
}

Loading…
Cancel
Save