Got this working: U4-4420 TagQuery / TagService missing method to get content with tag - just need to add some unit tests.

This commit is contained in:
Shannon
2014-05-06 19:04:49 +10:00
parent d264792e18
commit cfd21bc924
5 changed files with 178 additions and 1 deletions

View File

@@ -5,6 +5,10 @@ namespace Umbraco.Core.Persistence.Repositories
{
public interface ITagsRepository : IRepositoryQueryable<int, ITag>
{
IEnumerable<int> GetIdsForEntityTypeByTagGroup(TaggableObjectTypes objectType, string tagGroup);
IEnumerable<int> GetIdsForEntityTypeByTag(TaggableObjectTypes objectType, string tag, string tagGroup = null);
/// <summary>
/// Returns all tags for an entity type (content/media/member)
/// </summary>

View File

@@ -160,6 +160,50 @@ namespace Umbraco.Core.Persistence.Repositories
//TODO: Consider caching implications.
public IEnumerable<int> GetIdsForEntityTypeByTagGroup(TaggableObjectTypes objectType, string tagGroup)
{
var nodeObjectType = GetNodeObjectType(objectType);
var sql = new Sql()
.Select("DISTINCT cmsTagRelationship.nodeId")
.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<NodeDto>()
.On<NodeDto, ContentDto>(left => left.NodeId, right => right.NodeId)
.Where<NodeDto>(dto => dto.NodeObjectType == nodeObjectType)
.Where<TagDto>(dto => dto.Group == tagGroup);
return ApplicationContext.Current.DatabaseContext.Database.Fetch<int>(sql);
}
public IEnumerable<int> GetIdsForEntityTypeByTag(TaggableObjectTypes objectType, string tag, string tagGroup = null)
{
var nodeObjectType = GetNodeObjectType(objectType);
var sql = new Sql()
.Select("DISTINCT cmsTagRelationship.nodeId")
.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<NodeDto>()
.On<NodeDto, ContentDto>(left => left.NodeId, right => right.NodeId)
.Where<NodeDto>(dto => dto.NodeObjectType == nodeObjectType)
.Where<TagDto>(dto => dto.Tag == tag);
if (tagGroup.IsNullOrWhiteSpace() == false)
{
sql = sql.Where<TagDto>(dto => dto.Group == tagGroup);
}
return ApplicationContext.Current.DatabaseContext.Database.Fetch<int>(sql);
}
public IEnumerable<ITag> GetTagsForEntityType(TaggableObjectTypes objectType, string group = null)
{
var nodeObjectType = GetNodeObjectType(objectType);

View File

@@ -15,7 +15,14 @@ namespace Umbraco.Core.Services
/// to the content, media and member services respectively.
/// </remarks>
public interface ITagService : IService
{
{
IEnumerable<int> GetContentIdsByTagGroup(string tagGroup);
IEnumerable<int> GetContentIdsByTag(string tag, string tagGroup = null);
IEnumerable<int> GetMediaIdsByTagGroup(string tagGroup);
IEnumerable<int> GetMediaIdsByTag(string tag, string tagGroup = null);
IEnumerable<int> GetMemberIdsByTagGroup(string tagGroup);
IEnumerable<int> GetMemberIdsByTag(string tag, string tagGroup = null);
/// <summary>
/// Get every tag stored in the database (with optional group)

View File

@@ -1,7 +1,10 @@
using System.Collections.Generic;
using System.Linq;
using Umbraco.Core.Models;
using Umbraco.Core.Models.Rdbms;
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.Querying;
using Umbraco.Core.Persistence.SqlSyntax;
using Umbraco.Core.Persistence.UnitOfWork;
namespace Umbraco.Core.Services
@@ -38,6 +41,54 @@ namespace Umbraco.Core.Services
_uowProvider = provider;
}
public IEnumerable<int> GetContentIdsByTagGroup(string tagGroup)
{
using (var repository = _repositoryFactory.CreateTagsRepository(_uowProvider.GetUnitOfWork()))
{
return repository.GetIdsForEntityTypeByTagGroup(TaggableObjectTypes.Content, tagGroup);
}
}
public IEnumerable<int> GetContentIdsByTag(string tag, string tagGroup = null)
{
using (var repository = _repositoryFactory.CreateTagsRepository(_uowProvider.GetUnitOfWork()))
{
return repository.GetIdsForEntityTypeByTag(TaggableObjectTypes.Content, tag, tagGroup);
}
}
public IEnumerable<int> GetMediaIdsByTagGroup(string tagGroup)
{
using (var repository = _repositoryFactory.CreateTagsRepository(_uowProvider.GetUnitOfWork()))
{
return repository.GetIdsForEntityTypeByTagGroup(TaggableObjectTypes.Media, tagGroup);
}
}
public IEnumerable<int> GetMediaIdsByTag(string tag, string tagGroup = null)
{
using (var repository = _repositoryFactory.CreateTagsRepository(_uowProvider.GetUnitOfWork()))
{
return repository.GetIdsForEntityTypeByTag(TaggableObjectTypes.Media, tag, tagGroup);
}
}
public IEnumerable<int> GetMemberIdsByTagGroup(string tagGroup)
{
using (var repository = _repositoryFactory.CreateTagsRepository(_uowProvider.GetUnitOfWork()))
{
return repository.GetIdsForEntityTypeByTagGroup(TaggableObjectTypes.Member, tagGroup);
}
}
public IEnumerable<int> GetMemberIdsByTag(string tag, string tagGroup = null)
{
using (var repository = _repositoryFactory.CreateTagsRepository(_uowProvider.GetUnitOfWork()))
{
return repository.GetIdsForEntityTypeByTag(TaggableObjectTypes.Member, tag, tagGroup);
}
}
/// <summary>
/// Get every tag stored in the database (with optional group)
/// </summary>

View File

@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using AutoMapper;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
using Umbraco.Web.Models;
@@ -12,12 +14,81 @@ namespace Umbraco.Web
public class TagQuery
{
private readonly ITagService _tagService;
private readonly PublishedContentQuery _contentQuery;
[Obsolete("Use the alternate constructor specifying the contentQuery instead")]
public TagQuery(ITagService tagService)
: this(tagService, new PublishedContentQuery(UmbracoContext.Current.ContentCache, UmbracoContext.Current.MediaCache))
{
}
public TagQuery(ITagService tagService, PublishedContentQuery contentQuery)
{
if (tagService == null) throw new ArgumentNullException("tagService");
if (contentQuery == null) throw new ArgumentNullException("contentQuery");
_tagService = tagService;
_contentQuery = contentQuery;
}
/// <summary>
/// Returns all content that is tagged with the specified tag value and optional tag group
/// </summary>
/// <param name="tag"></param>
/// <param name="tagGroup"></param>
/// <returns></returns>
public IEnumerable<IPublishedContent> GetContentByTag(string tag, string tagGroup = null)
{
var ids = _tagService.GetContentIdsByTag(tag, tagGroup);
return _contentQuery.TypedContent(ids)
.Where(x => x != null);
}
/// <summary>
/// Returns all content that has been tagged with any tag in the specified group
/// </summary>
/// <param name="tagGroup"></param>
/// <returns></returns>
public IEnumerable<IPublishedContent> GetContentByTagGroup(string tagGroup)
{
var ids = _tagService.GetContentIdsByTagGroup(tagGroup);
return _contentQuery.TypedContent(ids)
.Where(x => x != null);
}
/// <summary>
/// Returns all Media that is tagged with the specified tag value and optional tag group
/// </summary>
/// <param name="tag"></param>
/// <param name="tagGroup"></param>
/// <returns></returns>
public IEnumerable<IPublishedContent> GetMediaByTag(string tag, string tagGroup = null)
{
var ids = _tagService.GetMediaIdsByTag(tag, tagGroup);
return _contentQuery.TypedMedia(ids)
.Where(x => x != null);
}
/// <summary>
/// Returns all Media that has been tagged with any tag in the specified group
/// </summary>
/// <param name="tagGroup"></param>
/// <returns></returns>
public IEnumerable<IPublishedContent> GetMediaByTagGroup(string tagGroup)
{
var ids = _tagService.GetMediaIdsByTagGroup(tagGroup);
return _contentQuery.TypedMedia(ids)
.Where(x => x != null);
}
//TODO: Should prob implement these, requires a bit of work on the member service to do this,
// also not sure if its necessary ?
//public IEnumerable<IPublishedContent> GetMembersByTag(string tag, string tagGroup = null)
//{
//}
//public IEnumerable<IPublishedContent> GetMembersByTagGroup(string tagGroup)
//{
//}
/// <summary>
/// Get every tag stored in the database (with optional group)