using System; using System.Collections.Generic; using System.Linq; using AutoMapper; using Umbraco.Core.Models; using Umbraco.Core.Services; using Umbraco.Web.Models; namespace Umbraco.Web { /// /// A class that exposes methods used to query tag data in views /// public class TagQuery : ITagQuery { private readonly ITagService _tagService; private readonly ITypedPublishedContentQuery _typedContentQuery; [Obsolete("Use the alternate constructor specifying the contentQuery instead")] public TagQuery(ITagService tagService) : this(tagService, new PublishedContentQuery(UmbracoContext.Current.ContentCache, UmbracoContext.Current.MediaCache)) { } [Obsolete("Use the alternate constructor specifying the ITypedPublishedContentQuery instead")] public TagQuery(ITagService tagService, PublishedContentQuery contentQuery) { if (tagService == null) throw new ArgumentNullException("tagService"); if (contentQuery == null) throw new ArgumentNullException("contentQuery"); _tagService = tagService; _typedContentQuery = contentQuery; } /// /// Constructor /// /// /// public TagQuery(ITagService tagService, ITypedPublishedContentQuery typedContentQuery) { if (tagService == null) throw new ArgumentNullException("tagService"); if (typedContentQuery == null) throw new ArgumentNullException("typedContentQuery"); _tagService = tagService; _typedContentQuery = typedContentQuery; } /// /// 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.GetTaggedContentByTag(tag, tagGroup) .Select(x => x.EntityId); return _typedContentQuery.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.GetTaggedContentByTagGroup(tagGroup) .Select(x => x.EntityId); return _typedContentQuery.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.GetTaggedMediaByTag(tag, tagGroup) .Select(x => x.EntityId); return _typedContentQuery.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.GetTaggedMediaByTagGroup(tagGroup) .Select(x => x.EntityId); return _typedContentQuery.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) /// public IEnumerable GetAllTags(string group = null) { return Mapper.Map>(_tagService.GetAllTags(group)); } /// /// Get all tags for content items (with optional group) /// /// /// public IEnumerable GetAllContentTags(string group = null) { return Mapper.Map>(_tagService.GetAllContentTags(group)); } /// /// Get all tags for media items (with optional group) /// /// /// public IEnumerable GetAllMediaTags(string group = null) { return Mapper.Map>(_tagService.GetAllMediaTags(group)); } /// /// Get all tags for member items (with optional group) /// /// /// public IEnumerable GetAllMemberTags(string group = null) { return Mapper.Map>(_tagService.GetAllMemberTags(group)); } /// /// Returns all tags attached to a property by entity id /// /// /// /// /// public IEnumerable GetTagsForProperty(int contentId, string propertyTypeAlias, string tagGroup = null) { return Mapper.Map>(_tagService.GetTagsForProperty(contentId, propertyTypeAlias, tagGroup)); } /// /// Returns all tags attached to an entity (content, media or member) by entity id /// /// /// /// public IEnumerable GetTagsForEntity(int contentId, string tagGroup = null) { return Mapper.Map>(_tagService.GetTagsForEntity(contentId, tagGroup)); } } }