From 8d724ff2f7bc27a28341d745a43c39c6fb10b054 Mon Sep 17 00:00:00 2001 From: Shannon Date: Thu, 24 Oct 2013 13:31:10 +1100 Subject: [PATCH] Completes: U4-3040 Create UmbracoHelper methods for querying real tags on the front-end and creates new TagQueryContext --- .../Models/Mapping/TabModelMapper.cs | 15 ++++ src/Umbraco.Web/Models/TagModel.cs | 22 +++++ src/Umbraco.Web/TagQueryContext.cs | 83 +++++++++++++++++++ src/Umbraco.Web/Umbraco.Web.csproj | 3 + src/Umbraco.Web/UmbracoHelper.cs | 9 ++ 5 files changed, 132 insertions(+) create mode 100644 src/Umbraco.Web/Models/Mapping/TabModelMapper.cs create mode 100644 src/Umbraco.Web/Models/TagModel.cs create mode 100644 src/Umbraco.Web/TagQueryContext.cs diff --git a/src/Umbraco.Web/Models/Mapping/TabModelMapper.cs b/src/Umbraco.Web/Models/Mapping/TabModelMapper.cs new file mode 100644 index 0000000000..1df0ae3f33 --- /dev/null +++ b/src/Umbraco.Web/Models/Mapping/TabModelMapper.cs @@ -0,0 +1,15 @@ +using AutoMapper; +using Umbraco.Core; +using Umbraco.Core.Models; +using Umbraco.Core.Models.Mapping; + +namespace Umbraco.Web.Models.Mapping +{ + internal class TabModelMapper : MapperConfiguration + { + public override void ConfigureMappings(IConfiguration config, ApplicationContext applicationContext) + { + config.CreateMap(); + } + } +} \ No newline at end of file diff --git a/src/Umbraco.Web/Models/TagModel.cs b/src/Umbraco.Web/Models/TagModel.cs new file mode 100644 index 0000000000..50981859a2 --- /dev/null +++ b/src/Umbraco.Web/Models/TagModel.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Runtime.Serialization; + +namespace Umbraco.Web.Models +{ + [DataContract(Name = "tag", Namespace = "")] + public class TagModel + { + [DataMember(Name = "id", IsRequired = true)] + public int Id { get; set; } + + [DataMember(Name = "text", IsRequired = true)] + public string Text { get; set; } + + [DataMember(Name = "group")] + public string Group { get; set; } + } +} diff --git a/src/Umbraco.Web/TagQueryContext.cs b/src/Umbraco.Web/TagQueryContext.cs new file mode 100644 index 0000000000..fac291a77b --- /dev/null +++ b/src/Umbraco.Web/TagQueryContext.cs @@ -0,0 +1,83 @@ +using System; +using System.Collections.Generic; +using AutoMapper; +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 TagQueryContext + { + private readonly ITagService _tagService; + + public TagQueryContext(ITagService tagService) + { + if (tagService == null) throw new ArgumentNullException("tagService"); + _tagService = tagService; + } + + /// + /// 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)); + } + } +} \ No newline at end of file diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj index 14769ef9de..68cef10fba 100644 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -332,6 +332,8 @@ + + @@ -389,6 +391,7 @@ + diff --git a/src/Umbraco.Web/UmbracoHelper.cs b/src/Umbraco.Web/UmbracoHelper.cs index 7bd4115fa0..a9ccba55f4 100644 --- a/src/Umbraco.Web/UmbracoHelper.cs +++ b/src/Umbraco.Web/UmbracoHelper.cs @@ -33,6 +33,15 @@ namespace Umbraco.Web private readonly UmbracoContext _umbracoContext; private readonly IPublishedContent _currentPage; private PublishedQueryContext _queryContext; + private TagQueryContext _tagContext; + + /// + /// Lazy instantiates the tag context + /// + public TagQueryContext Tags + { + get { return _tagContext ?? (_tagContext = new TagQueryContext(UmbracoContext.Application.Services.TagService)); } + } /// /// Lazy instantiates the query context