Fixes: U4-6834 Add TagService.GetTagsForEntity(Guid key) method
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Umbraco.Core.Models;
|
||||
|
||||
@@ -25,6 +26,14 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
/// <returns></returns>
|
||||
IEnumerable<ITag> GetTagsForEntity(int contentId, string group = null);
|
||||
|
||||
/// <summary>
|
||||
/// Returns all tags that exist on the content item - Content/Media/Member
|
||||
/// </summary>
|
||||
/// <param name="contentId">The content item id to get tags for</param>
|
||||
/// <param name="group">Optional group</param>
|
||||
/// <returns></returns>
|
||||
IEnumerable<ITag> GetTagsForEntity(Guid contentId, string group = null);
|
||||
|
||||
/// <summary>
|
||||
/// Returns all tags that exist on the content item for the property specified - Content/Media/Member
|
||||
/// </summary>
|
||||
@@ -34,6 +43,15 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
/// <returns></returns>
|
||||
IEnumerable<ITag> GetTagsForProperty(int contentId, string propertyTypeAlias, string group = null);
|
||||
|
||||
/// <summary>
|
||||
/// Returns all tags that exist on the content item for the property specified - Content/Media/Member
|
||||
/// </summary>
|
||||
/// <param name="contentId">The content item id to get tags for</param>
|
||||
/// <param name="propertyTypeAlias">The property alias to get tags for</param>
|
||||
/// <param name="group">Optional group</param>
|
||||
/// <returns></returns>
|
||||
IEnumerable<ITag> GetTagsForProperty(Guid contentId, string propertyTypeAlias, string group = null);
|
||||
|
||||
/// <summary>
|
||||
/// Assigns the given tags to a content item's property
|
||||
/// </summary>
|
||||
|
||||
@@ -96,7 +96,7 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
var sql = new Sql();
|
||||
if (isCount)
|
||||
{
|
||||
sql.Select("COUNT(*)").From<TagDto>();
|
||||
sql.Select("COUNT(*)").From<TagDto>(SqlSyntax);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -105,10 +105,10 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
return sql;
|
||||
}
|
||||
|
||||
private static Sql GetBaseQuery()
|
||||
private Sql GetBaseQuery()
|
||||
{
|
||||
var sql = new Sql();
|
||||
sql.Select("*").From<TagDto>();
|
||||
sql.Select("*").From<TagDto>(SqlSyntax);
|
||||
return sql;
|
||||
}
|
||||
|
||||
@@ -166,15 +166,15 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
{
|
||||
var sql = new Sql()
|
||||
.Select("cmsTagRelationship.nodeId, cmsPropertyType.Alias, cmsPropertyType.id as propertyTypeId, cmsTags.tag, cmsTags.id as tagId, cmsTags." + SqlSyntax.GetQuotedColumnName("group"))
|
||||
.From<TagDto>()
|
||||
.InnerJoin<TagRelationshipDto>()
|
||||
.On<TagRelationshipDto, TagDto>(left => left.TagId, right => right.Id)
|
||||
.InnerJoin<ContentDto>()
|
||||
.On<ContentDto, TagRelationshipDto>(left => left.NodeId, right => right.NodeId)
|
||||
.InnerJoin<PropertyTypeDto>()
|
||||
.On<PropertyTypeDto, TagRelationshipDto>(left => left.Id, right => right.PropertyTypeId)
|
||||
.InnerJoin<NodeDto>()
|
||||
.On<NodeDto, ContentDto>(left => left.NodeId, right => right.NodeId)
|
||||
.From<TagDto>(SqlSyntax)
|
||||
.InnerJoin<TagRelationshipDto>(SqlSyntax)
|
||||
.On<TagRelationshipDto, TagDto>(SqlSyntax, left => left.TagId, right => right.Id)
|
||||
.InnerJoin<ContentDto>(SqlSyntax)
|
||||
.On<ContentDto, TagRelationshipDto>(SqlSyntax, left => left.NodeId, right => right.NodeId)
|
||||
.InnerJoin<PropertyTypeDto>(SqlSyntax)
|
||||
.On<PropertyTypeDto, TagRelationshipDto>(SqlSyntax, left => left.Id, right => right.PropertyTypeId)
|
||||
.InnerJoin<NodeDto>(SqlSyntax)
|
||||
.On<NodeDto, ContentDto>(SqlSyntax, left => left.NodeId, right => right.NodeId)
|
||||
.Where<TagDto>(dto => dto.Group == tagGroup);
|
||||
|
||||
if (objectType != TaggableObjectTypes.All)
|
||||
@@ -192,15 +192,15 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
{
|
||||
var sql = new Sql()
|
||||
.Select("cmsTagRelationship.nodeId, cmsPropertyType.Alias, cmsPropertyType.id as propertyTypeId, cmsTags.tag, cmsTags.id as tagId, cmsTags." + SqlSyntax.GetQuotedColumnName("group"))
|
||||
.From<TagDto>()
|
||||
.InnerJoin<TagRelationshipDto>()
|
||||
.On<TagRelationshipDto, TagDto>(left => left.TagId, right => right.Id)
|
||||
.InnerJoin<ContentDto>()
|
||||
.On<ContentDto, TagRelationshipDto>(left => left.NodeId, right => right.NodeId)
|
||||
.InnerJoin<PropertyTypeDto>()
|
||||
.On<PropertyTypeDto, TagRelationshipDto>(left => left.Id, right => right.PropertyTypeId)
|
||||
.InnerJoin<NodeDto>()
|
||||
.On<NodeDto, ContentDto>(left => left.NodeId, right => right.NodeId)
|
||||
.From<TagDto>(SqlSyntax)
|
||||
.InnerJoin<TagRelationshipDto>(SqlSyntax)
|
||||
.On<TagRelationshipDto, TagDto>(SqlSyntax, left => left.TagId, right => right.Id)
|
||||
.InnerJoin<ContentDto>(SqlSyntax)
|
||||
.On<ContentDto, TagRelationshipDto>(SqlSyntax, left => left.NodeId, right => right.NodeId)
|
||||
.InnerJoin<PropertyTypeDto>(SqlSyntax)
|
||||
.On<PropertyTypeDto, TagRelationshipDto>(SqlSyntax, left => left.Id, right => right.PropertyTypeId)
|
||||
.InnerJoin<NodeDto>(SqlSyntax)
|
||||
.On<NodeDto, ContentDto>(SqlSyntax, left => left.NodeId, right => right.NodeId)
|
||||
.Where<TagDto>(dto => dto.Tag == tag);
|
||||
|
||||
if (objectType != TaggableObjectTypes.All)
|
||||
@@ -240,13 +240,7 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
var sql = GetTagsQuerySelect(true);
|
||||
|
||||
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);
|
||||
|
||||
|
||||
if (objectType != TaggableObjectTypes.All)
|
||||
{
|
||||
var nodeObjectType = GetNodeObjectType(objectType);
|
||||
@@ -268,7 +262,21 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
sql = ApplyRelationshipJoinToTagsQuery(sql);
|
||||
|
||||
sql = sql
|
||||
.Where<TagRelationshipDto>(dto => dto.NodeId == contentId);
|
||||
.Where<NodeDto>(dto => dto.NodeId == contentId);
|
||||
|
||||
sql = ApplyGroupFilterToTagsQuery(sql, group);
|
||||
|
||||
return ExecuteTagsQuery(sql);
|
||||
}
|
||||
|
||||
public IEnumerable<ITag> GetTagsForEntity(Guid contentId, string group = null)
|
||||
{
|
||||
var sql = GetTagsQuerySelect();
|
||||
|
||||
sql = ApplyRelationshipJoinToTagsQuery(sql);
|
||||
|
||||
sql = sql
|
||||
.Where<NodeDto>(dto => dto.UniqueId == contentId);
|
||||
|
||||
sql = ApplyGroupFilterToTagsQuery(sql, group);
|
||||
|
||||
@@ -282,9 +290,26 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
sql = ApplyRelationshipJoinToTagsQuery(sql);
|
||||
|
||||
sql = sql
|
||||
.InnerJoin<PropertyTypeDto>()
|
||||
.On<PropertyTypeDto, TagRelationshipDto>(left => left.Id, right => right.PropertyTypeId)
|
||||
.Where<TagRelationshipDto>(dto => dto.NodeId == contentId)
|
||||
.InnerJoin<PropertyTypeDto>(SqlSyntax)
|
||||
.On<PropertyTypeDto, TagRelationshipDto>(SqlSyntax, left => left.Id, right => right.PropertyTypeId)
|
||||
.Where<NodeDto>(dto => dto.NodeId == contentId)
|
||||
.Where<PropertyTypeDto>(dto => dto.Alias == propertyTypeAlias);
|
||||
|
||||
sql = ApplyGroupFilterToTagsQuery(sql, group);
|
||||
|
||||
return ExecuteTagsQuery(sql);
|
||||
}
|
||||
|
||||
public IEnumerable<ITag> GetTagsForProperty(Guid contentId, string propertyTypeAlias, string group = null)
|
||||
{
|
||||
var sql = GetTagsQuerySelect();
|
||||
|
||||
sql = ApplyRelationshipJoinToTagsQuery(sql);
|
||||
|
||||
sql = sql
|
||||
.InnerJoin<PropertyTypeDto>(SqlSyntax)
|
||||
.On<PropertyTypeDto, TagRelationshipDto>(SqlSyntax, left => left.Id, right => right.PropertyTypeId)
|
||||
.Where<NodeDto>(dto => dto.UniqueId == contentId)
|
||||
.Where<PropertyTypeDto>(dto => dto.Alias == propertyTypeAlias);
|
||||
|
||||
sql = ApplyGroupFilterToTagsQuery(sql, group);
|
||||
@@ -298,7 +323,7 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
|
||||
if (withGrouping)
|
||||
{
|
||||
sql = sql.Select("cmsTags.Id, cmsTags.Tag, cmsTags." + SqlSyntax.GetQuotedColumnName("Group") + @", Count(*) NodeCount");
|
||||
sql = sql.Select("cmsTags.id, cmsTags.tag, cmsTags." + SqlSyntax.GetQuotedColumnName("group") + @", Count(*) NodeCount");
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -311,14 +336,18 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
private Sql ApplyRelationshipJoinToTagsQuery(Sql sql)
|
||||
{
|
||||
return sql
|
||||
.From<TagDto>()
|
||||
.InnerJoin<TagRelationshipDto>()
|
||||
.On<TagRelationshipDto, TagDto>(left => left.TagId, right => right.Id);
|
||||
.From<TagDto>(SqlSyntax)
|
||||
.InnerJoin<TagRelationshipDto>(SqlSyntax)
|
||||
.On<TagRelationshipDto, TagDto>(SqlSyntax, left => left.TagId, right => right.Id)
|
||||
.InnerJoin<ContentDto>(SqlSyntax)
|
||||
.On<ContentDto, TagRelationshipDto>(SqlSyntax, left => left.NodeId, right => right.NodeId)
|
||||
.InnerJoin<NodeDto>(SqlSyntax)
|
||||
.On<NodeDto, ContentDto>(SqlSyntax, left => left.NodeId, right => right.NodeId);
|
||||
}
|
||||
|
||||
private Sql ApplyGroupFilterToTagsQuery(Sql sql, string group)
|
||||
{
|
||||
if (!group.IsNullOrWhiteSpace())
|
||||
if (@group.IsNullOrWhiteSpace() == false)
|
||||
{
|
||||
sql = sql.Where<TagDto>(dto => dto.Group == group);
|
||||
}
|
||||
@@ -328,7 +357,7 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
|
||||
private Sql ApplyGroupByToTagsQuery(Sql sql)
|
||||
{
|
||||
return sql.GroupBy(new string[] { "cmsTags.Id", "cmsTags.Tag", "cmsTags." + SqlSyntax.GetQuotedColumnName("Group") + @"" });
|
||||
return sql.GroupBy(new string[] { "cmsTags.id", "cmsTags.tag", "cmsTags." + SqlSyntax.GetQuotedColumnName("group") + @"" });
|
||||
}
|
||||
|
||||
private IEnumerable<ITag> ExecuteTagsQuery(Sql sql)
|
||||
@@ -385,16 +414,16 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
|
||||
//adds any tags found in the collection that aren't in cmsTag
|
||||
var insertTagsSql = string.Concat("insert into cmsTags (Tag,",
|
||||
SqlSyntax.GetQuotedColumnName("Group"),
|
||||
SqlSyntax.GetQuotedColumnName("group"),
|
||||
") ",
|
||||
" select TagSet.Tag, TagSet.",
|
||||
SqlSyntax.GetQuotedColumnName("Group"),
|
||||
SqlSyntax.GetQuotedColumnName("group"),
|
||||
" from ",
|
||||
tagSetSql,
|
||||
" left outer join cmsTags on (TagSet.Tag = cmsTags.Tag and TagSet.",
|
||||
SqlSyntax.GetQuotedColumnName("Group"),
|
||||
SqlSyntax.GetQuotedColumnName("group"),
|
||||
" = cmsTags.",
|
||||
SqlSyntax.GetQuotedColumnName("Group"),
|
||||
SqlSyntax.GetQuotedColumnName("group"),
|
||||
")",
|
||||
" where cmsTags.Id is null ");
|
||||
//insert the tags that don't exist
|
||||
@@ -413,9 +442,9 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
"select NewTags.Id from ",
|
||||
tagSetSql,
|
||||
" inner join cmsTags as NewTags on (TagSet.Tag = NewTags.Tag and TagSet.",
|
||||
SqlSyntax.GetQuotedColumnName("Group"),
|
||||
SqlSyntax.GetQuotedColumnName("group"),
|
||||
" = TagSet.",
|
||||
SqlSyntax.GetQuotedColumnName("Group"),
|
||||
SqlSyntax.GetQuotedColumnName("group"),
|
||||
") ",
|
||||
") as NewTagsSet ",
|
||||
"left outer join cmsTagRelationship ",
|
||||
@@ -451,7 +480,7 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
" AND tagId IN ",
|
||||
"(SELECT id FROM cmsTags INNER JOIN ",
|
||||
tagSetSql,
|
||||
" ON (TagSet.Tag = cmsTags.Tag and TagSet." + SqlSyntax.GetQuotedColumnName("Group") + @" = cmsTags." + SqlSyntax.GetQuotedColumnName("Group") + @"))");
|
||||
" ON (TagSet.Tag = cmsTags.Tag and TagSet." + SqlSyntax.GetQuotedColumnName("group") + @" = cmsTags." + SqlSyntax.GetQuotedColumnName("group") + @"))");
|
||||
|
||||
Database.Execute(deleteSql);
|
||||
}
|
||||
@@ -503,7 +532,7 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
|
||||
var array = tagsToInsert
|
||||
.Select(tag =>
|
||||
string.Format("select '{0}' as Tag, '{1}' as " + SqlSyntax.GetQuotedColumnName("Group") + @"",
|
||||
string.Format("select '{0}' as Tag, '{1}' as " + SqlSyntax.GetQuotedColumnName("group") + @"",
|
||||
PetaPocoExtensions.EscapeAtSymbols(tag.Text.Replace("'", "''")), tag.Group))
|
||||
.ToArray();
|
||||
return "(" + string.Join(" union ", array).Replace(" ", " ") + ") as TagSet";
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Umbraco.Core.Models;
|
||||
|
||||
@@ -119,5 +120,26 @@ namespace Umbraco.Core.Services
|
||||
/// <param name="tagGroup">Optional name of the 'Tag Group'</param>
|
||||
/// <returns>An enumerable list of <see cref="ITag"/></returns>
|
||||
IEnumerable<ITag> GetTagsForEntity(int contentId, string tagGroup = null);
|
||||
|
||||
/// <summary>
|
||||
/// Gets all tags attached to a property by entity id
|
||||
/// </summary>
|
||||
/// <remarks>Use the optional tagGroup parameter to limit the
|
||||
/// result to a specific 'Tag Group'.</remarks>
|
||||
/// <param name="contentId">The content item id to get tags for</param>
|
||||
/// <param name="propertyTypeAlias">Property type alias</param>
|
||||
/// <param name="tagGroup">Optional name of the 'Tag Group'</param>
|
||||
/// <returns>An enumerable list of <see cref="ITag"/></returns>
|
||||
IEnumerable<ITag> GetTagsForProperty(Guid contentId, string propertyTypeAlias, string tagGroup = null);
|
||||
|
||||
/// <summary>
|
||||
/// Gets all tags attached to an entity (content, media or member) by entity id
|
||||
/// </summary>
|
||||
/// <remarks>Use the optional tagGroup parameter to limit the
|
||||
/// result to a specific 'Tag Group'.</remarks>
|
||||
/// <param name="contentId">The content item id to get tags for</param>
|
||||
/// <param name="tagGroup">Optional name of the 'Tag Group'</param>
|
||||
/// <returns>An enumerable list of <see cref="ITag"/></returns>
|
||||
IEnumerable<ITag> GetTagsForEntity(Guid contentId, string tagGroup = null);
|
||||
}
|
||||
}
|
||||
@@ -222,5 +222,38 @@ namespace Umbraco.Core.Services
|
||||
return repository.GetTagsForEntity(contentId, tagGroup);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets all tags attached to a property by entity id
|
||||
/// </summary>
|
||||
/// <remarks>Use the optional tagGroup parameter to limit the
|
||||
/// result to a specific 'Tag Group'.</remarks>
|
||||
/// <param name="contentId">The content item id to get tags for</param>
|
||||
/// <param name="propertyTypeAlias">Property type alias</param>
|
||||
/// <param name="tagGroup">Optional name of the 'Tag Group'</param>
|
||||
/// <returns>An enumerable list of <see cref="ITag"/></returns>
|
||||
public IEnumerable<ITag> GetTagsForProperty(Guid contentId, string propertyTypeAlias, string tagGroup = null)
|
||||
{
|
||||
using (var repository = RepositoryFactory.CreateTagRepository(UowProvider.GetUnitOfWork()))
|
||||
{
|
||||
return repository.GetTagsForProperty(contentId, propertyTypeAlias, tagGroup);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets all tags attached to an entity (content, media or member) by entity id
|
||||
/// </summary>
|
||||
/// <remarks>Use the optional tagGroup parameter to limit the
|
||||
/// result to a specific 'Tag Group'.</remarks>
|
||||
/// <param name="contentId">The content item id to get tags for</param>
|
||||
/// <param name="tagGroup">Optional name of the 'Tag Group'</param>
|
||||
/// <returns>An enumerable list of <see cref="ITag"/></returns>
|
||||
public IEnumerable<ITag> GetTagsForEntity(Guid contentId, string tagGroup = null)
|
||||
{
|
||||
using (var repository = RepositoryFactory.CreateTagRepository(UowProvider.GetUnitOfWork()))
|
||||
{
|
||||
return repository.GetTagsForEntity(contentId, tagGroup);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user