From cfd21bc924e67339b6ffbb7489448fc4601d8db1 Mon Sep 17 00:00:00 2001 From: Shannon Date: Tue, 6 May 2014 19:04:49 +1000 Subject: [PATCH] Got this working: U4-4420 TagQuery / TagService missing method to get content with tag - just need to add some unit tests. --- .../Interfaces/ITagsRepository.cs | 4 ++ .../Repositories/TagsRepository.cs | 44 ++++++++++++ src/Umbraco.Core/Services/ITagService.cs | 9 ++- src/Umbraco.Core/Services/TagService.cs | 51 +++++++++++++ src/Umbraco.Web/TagQuery.cs | 71 +++++++++++++++++++ 5 files changed, 178 insertions(+), 1 deletion(-) diff --git a/src/Umbraco.Core/Persistence/Repositories/Interfaces/ITagsRepository.cs b/src/Umbraco.Core/Persistence/Repositories/Interfaces/ITagsRepository.cs index 64e9add722..459ef81882 100644 --- a/src/Umbraco.Core/Persistence/Repositories/Interfaces/ITagsRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/Interfaces/ITagsRepository.cs @@ -5,6 +5,10 @@ namespace Umbraco.Core.Persistence.Repositories { public interface ITagsRepository : IRepositoryQueryable { + IEnumerable GetIdsForEntityTypeByTagGroup(TaggableObjectTypes objectType, string tagGroup); + + IEnumerable GetIdsForEntityTypeByTag(TaggableObjectTypes objectType, string tag, string tagGroup = null); + /// /// Returns all tags for an entity type (content/media/member) /// diff --git a/src/Umbraco.Core/Persistence/Repositories/TagsRepository.cs b/src/Umbraco.Core/Persistence/Repositories/TagsRepository.cs index 41b7bd6f1a..f278a19952 100644 --- a/src/Umbraco.Core/Persistence/Repositories/TagsRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/TagsRepository.cs @@ -160,6 +160,50 @@ namespace Umbraco.Core.Persistence.Repositories //TODO: Consider caching implications. + + public IEnumerable GetIdsForEntityTypeByTagGroup(TaggableObjectTypes objectType, string tagGroup) + { + var nodeObjectType = GetNodeObjectType(objectType); + + var sql = new Sql() + .Select("DISTINCT cmsTagRelationship.nodeId") + .From() + .InnerJoin() + .On(left => left.TagId, right => right.Id) + .InnerJoin() + .On(left => left.NodeId, right => right.NodeId) + .InnerJoin() + .On(left => left.NodeId, right => right.NodeId) + .Where(dto => dto.NodeObjectType == nodeObjectType) + .Where(dto => dto.Group == tagGroup); + + return ApplicationContext.Current.DatabaseContext.Database.Fetch(sql); + } + + public IEnumerable GetIdsForEntityTypeByTag(TaggableObjectTypes objectType, string tag, string tagGroup = null) + { + var nodeObjectType = GetNodeObjectType(objectType); + + var sql = new Sql() + .Select("DISTINCT cmsTagRelationship.nodeId") + .From() + .InnerJoin() + .On(left => left.TagId, right => right.Id) + .InnerJoin() + .On(left => left.NodeId, right => right.NodeId) + .InnerJoin() + .On(left => left.NodeId, right => right.NodeId) + .Where(dto => dto.NodeObjectType == nodeObjectType) + .Where(dto => dto.Tag == tag); + + if (tagGroup.IsNullOrWhiteSpace() == false) + { + sql = sql.Where(dto => dto.Group == tagGroup); + } + + return ApplicationContext.Current.DatabaseContext.Database.Fetch(sql); + } + public IEnumerable GetTagsForEntityType(TaggableObjectTypes objectType, string group = null) { var nodeObjectType = GetNodeObjectType(objectType); diff --git a/src/Umbraco.Core/Services/ITagService.cs b/src/Umbraco.Core/Services/ITagService.cs index efba465c21..839df8a05d 100644 --- a/src/Umbraco.Core/Services/ITagService.cs +++ b/src/Umbraco.Core/Services/ITagService.cs @@ -15,7 +15,14 @@ namespace Umbraco.Core.Services /// to the content, media and member services respectively. /// public interface ITagService : IService - { + { + + IEnumerable GetContentIdsByTagGroup(string tagGroup); + IEnumerable GetContentIdsByTag(string tag, string tagGroup = null); + IEnumerable GetMediaIdsByTagGroup(string tagGroup); + IEnumerable GetMediaIdsByTag(string tag, string tagGroup = null); + IEnumerable GetMemberIdsByTagGroup(string tagGroup); + IEnumerable GetMemberIdsByTag(string tag, string tagGroup = null); /// /// Get every tag stored in the database (with optional group) diff --git a/src/Umbraco.Core/Services/TagService.cs b/src/Umbraco.Core/Services/TagService.cs index 179b2a6df7..8e5d6c0ab0 100644 --- a/src/Umbraco.Core/Services/TagService.cs +++ b/src/Umbraco.Core/Services/TagService.cs @@ -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 GetContentIdsByTagGroup(string tagGroup) + { + using (var repository = _repositoryFactory.CreateTagsRepository(_uowProvider.GetUnitOfWork())) + { + return repository.GetIdsForEntityTypeByTagGroup(TaggableObjectTypes.Content, tagGroup); + } + } + + public IEnumerable GetContentIdsByTag(string tag, string tagGroup = null) + { + using (var repository = _repositoryFactory.CreateTagsRepository(_uowProvider.GetUnitOfWork())) + { + return repository.GetIdsForEntityTypeByTag(TaggableObjectTypes.Content, tag, tagGroup); + } + } + + public IEnumerable GetMediaIdsByTagGroup(string tagGroup) + { + using (var repository = _repositoryFactory.CreateTagsRepository(_uowProvider.GetUnitOfWork())) + { + return repository.GetIdsForEntityTypeByTagGroup(TaggableObjectTypes.Media, tagGroup); + } + } + + public IEnumerable GetMediaIdsByTag(string tag, string tagGroup = null) + { + using (var repository = _repositoryFactory.CreateTagsRepository(_uowProvider.GetUnitOfWork())) + { + return repository.GetIdsForEntityTypeByTag(TaggableObjectTypes.Media, tag, tagGroup); + } + } + + public IEnumerable GetMemberIdsByTagGroup(string tagGroup) + { + using (var repository = _repositoryFactory.CreateTagsRepository(_uowProvider.GetUnitOfWork())) + { + return repository.GetIdsForEntityTypeByTagGroup(TaggableObjectTypes.Member, tagGroup); + } + } + + public IEnumerable GetMemberIdsByTag(string tag, string tagGroup = null) + { + using (var repository = _repositoryFactory.CreateTagsRepository(_uowProvider.GetUnitOfWork())) + { + return repository.GetIdsForEntityTypeByTag(TaggableObjectTypes.Member, tag, tagGroup); + } + } + /// /// Get every tag stored in the database (with optional group) /// diff --git a/src/Umbraco.Web/TagQuery.cs b/src/Umbraco.Web/TagQuery.cs index ae82cce988..ebdf67b77a 100644 --- a/src/Umbraco.Web/TagQuery.cs +++ b/src/Umbraco.Web/TagQuery.cs @@ -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; } + + /// + /// Returns all content that is tagged with the specified tag value and optional tag group + /// + /// + /// + /// + public IEnumerable GetContentByTag(string tag, string tagGroup = null) + { + var ids = _tagService.GetContentIdsByTag(tag, tagGroup); + return _contentQuery.TypedContent(ids) + .Where(x => x != null); + } + + /// + /// Returns all content that has been tagged with any tag in the specified group + /// + /// + /// + public IEnumerable GetContentByTagGroup(string tagGroup) + { + var ids = _tagService.GetContentIdsByTagGroup(tagGroup); + return _contentQuery.TypedContent(ids) + .Where(x => x != null); + } + + /// + /// Returns all Media that is tagged with the specified tag value and optional tag group + /// + /// + /// + /// + public IEnumerable GetMediaByTag(string tag, string tagGroup = null) + { + var ids = _tagService.GetMediaIdsByTag(tag, tagGroup); + return _contentQuery.TypedMedia(ids) + .Where(x => x != null); + } + + /// + /// Returns all Media that has been tagged with any tag in the specified group + /// + /// + /// + public IEnumerable 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 GetMembersByTag(string tag, string tagGroup = null) + //{ + //} + + //public IEnumerable GetMembersByTagGroup(string tagGroup) + //{ + //} /// /// Get every tag stored in the database (with optional group)