Added NodeCount property to ITag/Tag and an optional flag to tag retrieval methods on TagsService, in order to retrieve the count for the tag as well as it's text

This commit is contained in:
AndyButland
2014-08-03 23:09:56 +02:00
parent 2325c8a64f
commit 6ee078108d
8 changed files with 120 additions and 62 deletions

View File

@@ -11,6 +11,8 @@ namespace Umbraco.Core.Models
[DataMember]
string Group { get; set; }
int NodeCount { get; }
//TODO: enable this at some stage
//int ParentId { get; set; }
}

View File

@@ -27,5 +27,8 @@ namespace Umbraco.Core.Models.Rdbms
[NullSetting(NullSetting = NullSettings.Null)]
[Length(100)]
public string Group { get; set; }//NOTE Is set to [varchar] (100) in Sql Server script
[Column("NodeCount")]
public int NodeCount { get; set; }
}
}

View File

@@ -16,9 +16,15 @@ namespace Umbraco.Core.Models
public Tag(int id, string text, string @group)
{
Id = id;
Text = text;
Group = @group;
Id = id;
}
public Tag(int id, string text, string @group, int nodeCount)
: this(id, text, @group)
{
NodeCount = nodeCount;
}
private static readonly PropertyInfo TextSelector = ExpressionHelper.GetPropertyInfo<Tag, string>(x => x.Text);
@@ -52,6 +58,8 @@ namespace Umbraco.Core.Models
}
}
public int NodeCount { get; internal set; }
//TODO: enable this at some stage
//public int ParentId { get; set; }
}

View File

@@ -7,7 +7,7 @@ namespace Umbraco.Core.Persistence.Factories
{
public ITag BuildEntity(TagDto dto)
{
var model = new Tag(dto.Id, dto.Tag, dto.Group);
var model = new Tag(dto.Id, dto.Tag, dto.Group, dto.NodeCount);
//on initial construction we don't want to have dirty properties tracked
// http://issues.umbraco.org/issue/U4-1946
model.ResetDirtyProperties(false);
@@ -20,7 +20,8 @@ namespace Umbraco.Core.Persistence.Factories
{
Id = entity.Id,
Group = entity.Group,
Tag = entity.Text
Tag = entity.Text,
NodeCount = entity.NodeCount,
};
}
}

View File

@@ -12,10 +12,11 @@ namespace Umbraco.Core.Persistence.Repositories
/// <summary>
/// Returns all tags for an entity type (content/media/member)
/// </summary>
/// <param name="objectType"></param>
/// <param name="objectType">Entity type</param>
/// <param name="group">Optional group</param>
/// <param name="withCount">Optional flag to return the number of content items tagged with the tag</param>
/// <returns></returns>
IEnumerable<ITag> GetTagsForEntityType(TaggableObjectTypes objectType, string group = null);
IEnumerable<ITag> GetTagsForEntityType(TaggableObjectTypes objectType, string group = null, bool withCount = false);
/// <summary>
/// Returns all tags that exist on the content item - Content/Media/Member

View File

@@ -226,67 +226,105 @@ namespace Umbraco.Core.Persistence.Repositories
return list;
}
public IEnumerable<ITag> GetTagsForEntityType(TaggableObjectTypes objectType, string group = null)
public IEnumerable<ITag> GetTagsForEntityType(TaggableObjectTypes objectType, string group = null, bool withCount = false)
{
var nodeObjectType = GetNodeObjectType(objectType);
var sql = new Sql()
.Select("DISTINCT cmsTags.*")
.From<TagDto>()
.InnerJoin<TagRelationshipDto>()
.On<TagRelationshipDto, TagDto>(left => left.TagId, right => right.Id)
var sql = GetTagsQuerySelect(withCount);
sql = ApplyRelationshipJoinToTagsQuery(sql);
sql = sql
.InnerJoin<ContentDto>()
.On<ContentDto, TagRelationshipDto>(left => left.NodeId, right => right.NodeId)
.InnerJoin<NodeDto>()
.On<NodeDto, ContentDto>(left => left.NodeId, right => right.NodeId)
.Where<NodeDto>(dto => dto.NodeObjectType == nodeObjectType);
if (group.IsNullOrWhiteSpace() == false)
{
sql = sql.Where<TagDto>(dto => dto.Group == group);
}
sql = ApplyGroupFilterToTagsQuery(sql, group);
var factory = new TagFactory();
sql = ApplyGroupByToTagsQuery(sql, withCount);
return Database.Fetch<TagDto>(sql).Select(factory.BuildEntity);
return ExecuteTagsQuery(sql);
}
public IEnumerable<ITag> GetTagsForEntity(int contentId, string group = null)
{
var sql = new Sql()
.Select("DISTINCT cmsTags.*")
.From<TagDto>()
.InnerJoin<TagRelationshipDto>()
.On<TagRelationshipDto, TagDto>(left => left.TagId, right => right.Id)
var sql = GetTagsQuerySelect();
sql = ApplyRelationshipJoinToTagsQuery(sql);
sql = sql
.Where<TagRelationshipDto>(dto => dto.NodeId == contentId);
if (group.IsNullOrWhiteSpace() == false)
{
sql = sql.Where<TagDto>(dto => dto.Group == group);
}
sql = ApplyGroupFilterToTagsQuery(sql, group);
var factory = new TagFactory();
return Database.Fetch<TagDto>(sql).Select(factory.BuildEntity);
return ExecuteTagsQuery(sql);
}
public IEnumerable<ITag> GetTagsForProperty(int contentId, string propertyTypeAlias, string group = null)
{
var sql = new Sql()
.Select("DISTINCT cmsTags.*")
.From<TagDto>()
.InnerJoin<TagRelationshipDto>()
.On<TagRelationshipDto, TagDto>(left => left.TagId, right => right.Id)
var sql = GetTagsQuerySelect();
sql = ApplyRelationshipJoinToTagsQuery(sql);
sql = sql
.InnerJoin<PropertyTypeDto>()
.On<PropertyTypeDto, TagRelationshipDto>(left => left.Id, right => right.PropertyTypeId)
.Where<TagRelationshipDto>(dto => dto.NodeId == contentId)
.Where<PropertyTypeDto>(dto => dto.Alias == propertyTypeAlias);
if (group.IsNullOrWhiteSpace() == false)
sql = ApplyGroupFilterToTagsQuery(sql, group);
return ExecuteTagsQuery(sql);
}
private Sql GetTagsQuerySelect(bool withCount = false)
{
var sql = new Sql();
if (withCount)
{
sql = sql.Select("cmsTags.Id, cmsTags.Tag, cmsTags.[Group], Count(*) NodeCount");
}
else
{
sql = sql.Select("DISTINCT cmsTags.*");
}
return sql;
}
private Sql ApplyRelationshipJoinToTagsQuery(Sql sql)
{
return sql
.From<TagDto>()
.InnerJoin<TagRelationshipDto>()
.On<TagRelationshipDto, TagDto>(left => left.TagId, right => right.Id);
}
private Sql ApplyGroupFilterToTagsQuery(Sql sql, string group)
{
if (!group.IsNullOrWhiteSpace())
{
sql = sql.Where<TagDto>(dto => dto.Group == group);
}
return sql;
}
private Sql ApplyGroupByToTagsQuery(Sql sql, bool withCount)
{
if (withCount)
{
sql = sql.GroupBy(new string[] { "cmsTags.Id", "cmsTags.Tag", "cmsTags.[Group]" });
}
return sql;
}
private IEnumerable<ITag> ExecuteTagsQuery(Sql sql)
{
var factory = new TagFactory();
return Database.Fetch<TagDto>(sql).Select(factory.BuildEntity);

View File

@@ -32,40 +32,42 @@ namespace Umbraco.Core.Services
/// <summary>
/// Get all tags for content items (with optional group)
/// </summary>
/// <param name="group"></param>
/// <param name="group">Optional group</param>
/// <param name="withCount">Optional flag to return the number of content items tagged with the tag</param>
/// <returns></returns>
IEnumerable<ITag> GetAllContentTags(string group = null);
IEnumerable<ITag> GetAllContentTags(string group = null, bool withCount = false);
/// <summary>
/// Get all tags for media items (with optional group)
/// </summary>
/// <param name="group"></param>
/// <param name="group">Optional group</param>
/// <param name="withCount">Optional flag to return the number of content items tagged with the tag</param>
/// <returns></returns>
IEnumerable<ITag> GetAllMediaTags(string group = null);
IEnumerable<ITag> GetAllMediaTags(string group = null, bool withCount = false);
/// <summary>
/// Get all tags for member items (with optional group)
/// </summary>
/// <param name="group"></param>
/// <param name="group">Optional group</param>
/// <param name="withCount">Optional flag to return the number of content items tagged with the tag</param>
/// <returns></returns>
IEnumerable<ITag> GetAllMemberTags(string group = null);
IEnumerable<ITag> GetAllMemberTags(string group = null, bool withCount = false);
/// <summary>
/// Returns all tags attached to a property by entity id
/// </summary>
/// <param name="contentId"></param>
/// <param name="propertyTypeAlias"></param>
/// <param name="tagGroup"></param>
/// <param name="contentId">The content item id to get tags for</param>
/// <param name="propertyTypeAlias">Property type alias</param>
/// <param name="tagGroup">Optional tag group</param>
/// <returns></returns>
IEnumerable<ITag> GetTagsForProperty(int contentId, string propertyTypeAlias, string tagGroup = null);
/// <summary>
/// Returns all tags attached to an entity (content, media or member) by entity id
/// </summary>
/// <param name="contentId"></param>
/// <param name="tagGroup"></param>
/// <param name="contentId">The content item id to get tags for</param>
/// <param name="tagGroup">Optional tag group</param>
/// <returns></returns>
IEnumerable<ITag> GetTagsForEntity(int contentId, string tagGroup = null);
}
}

View File

@@ -10,7 +10,7 @@ using Umbraco.Core.Persistence.UnitOfWork;
namespace Umbraco.Core.Services
{
/// <summary>
/// Tag service to query for tags in the tags db table. The tags returned are only relavent for published content & saved media or members
/// Tag service to query for tags in the tags db table. The tags returned are only relevant for published content & saved media or members
/// </summary>
/// <remarks>
/// If there is unpublished content with tags, those tags will not be contained
@@ -110,48 +110,51 @@ namespace Umbraco.Core.Services
/// <summary>
/// Get all tags for content items (with optional group)
/// </summary>
/// <param name="group"></param>
/// <param name="group">Optional group</param>
/// <param name="withCount">Optional flag to return the number of content items tagged with the tag</param>
/// <returns></returns>
public IEnumerable<ITag> GetAllContentTags(string group = null)
public IEnumerable<ITag> GetAllContentTags(string group = null, bool withCount = false)
{
using (var repository = _repositoryFactory.CreateTagsRepository(_uowProvider.GetUnitOfWork()))
{
return repository.GetTagsForEntityType(TaggableObjectTypes.Content, group);
return repository.GetTagsForEntityType(TaggableObjectTypes.Content, group, withCount);
}
}
/// <summary>
/// Get all tags for media items (with optional group)
/// </summary>
/// <param name="group"></param>
/// <param name="group">Optional group</param>
/// <param name="withCount">Optional flag to return the number of content items tagged with the tag</param>
/// <returns></returns>
public IEnumerable<ITag> GetAllMediaTags(string group = null)
public IEnumerable<ITag> GetAllMediaTags(string group = null, bool withCount = false)
{
using (var repository = _repositoryFactory.CreateTagsRepository(_uowProvider.GetUnitOfWork()))
{
return repository.GetTagsForEntityType(TaggableObjectTypes.Media, group);
return repository.GetTagsForEntityType(TaggableObjectTypes.Media, group, withCount);
}
}
/// <summary>
/// Get all tags for member items (with optional group)
/// </summary>
/// <param name="group"></param>
/// <param name="group">Optional group</param>
/// <param name="withCount">Optional flag to return the number of content items tagged with the tag</param>
/// <returns></returns>
public IEnumerable<ITag> GetAllMemberTags(string group = null)
public IEnumerable<ITag> GetAllMemberTags(string group = null, bool withCount = false)
{
using (var repository = _repositoryFactory.CreateTagsRepository(_uowProvider.GetUnitOfWork()))
{
return repository.GetTagsForEntityType(TaggableObjectTypes.Member, group);
return repository.GetTagsForEntityType(TaggableObjectTypes.Member, group, withCount);
}
}
/// <summary>
/// Returns all tags attached to a property by entity id
/// </summary>
/// <param name="contentId"></param>
/// <param name="propertyTypeAlias"></param>
/// <param name="tagGroup"></param>
/// <param name="contentId">The content item id to get tags for</param>
/// <param name="propertyTypeAlias">Property type alias</param>
/// <param name="tagGroup">Optional tag group</param>
/// <returns></returns>
public IEnumerable<ITag> GetTagsForProperty(int contentId, string propertyTypeAlias, string tagGroup = null)
{
@@ -164,8 +167,8 @@ namespace Umbraco.Core.Services
/// <summary>
/// Returns all tags attached to an entity (content, media or member) by entity id
/// </summary>
/// <param name="contentId"></param>
/// <param name="tagGroup"></param>
/// <param name="contentId">The content item id to get tags for</param>
/// <param name="tagGroup">Optional tag group</param>
/// <returns></returns>
public IEnumerable<ITag> GetTagsForEntity(int contentId, string tagGroup = null)
{