From 78e836025b7cbf39a2f17aea7284dcc5fbc13716 Mon Sep 17 00:00:00 2001 From: Shannon Date: Mon, 19 Aug 2013 16:49:07 +1000 Subject: [PATCH] Fixes EntityController to have a correct model mapper and removes invalidly named 'map' method. Removes the key from entity since we don't need it. Added some notes to the controller since we have to implement security for the results. --- src/Umbraco.Web/Editors/EntityController.cs | 39 ++++++------------- .../{EntityDisplay.cs => EntityBasic.cs} | 7 +--- .../Models/Mapping/EntityModelMapper.cs | 17 ++++++++ src/Umbraco.Web/Umbraco.Web.csproj | 3 +- 4 files changed, 32 insertions(+), 34 deletions(-) rename src/Umbraco.Web/Models/ContentEditing/{EntityDisplay.cs => EntityBasic.cs} (82%) create mode 100644 src/Umbraco.Web/Models/Mapping/EntityModelMapper.cs diff --git a/src/Umbraco.Web/Editors/EntityController.cs b/src/Umbraco.Web/Editors/EntityController.cs index bb2eff362e..3750f440e0 100644 --- a/src/Umbraco.Web/Editors/EntityController.cs +++ b/src/Umbraco.Web/Editors/EntityController.cs @@ -19,41 +19,24 @@ namespace Umbraco.Web.Editors [PluginController("UmbracoApi")] public class EntityController : UmbracoAuthorizedJsonController { - public EntityDisplay GetById(int id) + public EntityBasic GetById(int id) { - - return map((UmbracoEntity)Services.EntityService.Get(id, UmbracoObjectTypes.Document)); + return Mapper.Map(Services.EntityService.Get(id, UmbracoObjectTypes.Document)); } - public IEnumerable GetByIds([FromUri]int[] ids) + + //TODO: This should probably be change to GetContentByIds since it will be different for media, etc...! + + //TODO: Because this is a publicly accessible API, we need to filter the results for what the currently logged in user + // is actually allowed to access. We'll need to enhance the FilterAllowedOutgoingContent to acheive that. + + public IEnumerable GetByIds([FromUri]int[] ids) { if (ids == null) throw new ArgumentNullException("ids"); - return ids.Select(id => map(((UmbracoEntity)Services.EntityService.Get(id, UmbracoObjectTypes.Document)))).Where(entity => entity != null).ToList(); + return ids.Select(id => + Mapper.Map(Services.EntityService.Get(id, UmbracoObjectTypes.Document))); } - private EntityDisplay map(UmbracoEntity input) - { - EntityDisplay output = new EntityDisplay(); - output.Name = input.Name; - output.Id = input.Id; - output.Key = input.Key; - output.Icon = input.ContentTypeIcon; - return output; - } - - //public IEnumerable GetContentByIds(int[] ids) - //{ - // var list = new List(); - // foreach (var id in ids) - // list.Add((UmbracoEntity)Services.EntityService.Get(id)); - - // return list; - //} - - //public UmbracoEntity GetContentById(int id) - //{ - // return (UmbracoEntity)Services.EntityService.Get(id); - //} } } diff --git a/src/Umbraco.Web/Models/ContentEditing/EntityDisplay.cs b/src/Umbraco.Web/Models/ContentEditing/EntityBasic.cs similarity index 82% rename from src/Umbraco.Web/Models/ContentEditing/EntityDisplay.cs rename to src/Umbraco.Web/Models/ContentEditing/EntityBasic.cs index 328173c13e..5cc1adbf4e 100644 --- a/src/Umbraco.Web/Models/ContentEditing/EntityDisplay.cs +++ b/src/Umbraco.Web/Models/ContentEditing/EntityBasic.cs @@ -8,17 +8,14 @@ using System.Threading.Tasks; namespace Umbraco.Web.Models.ContentEditing { [DataContract(Name = "entity", Namespace = "")] - public class EntityDisplay + public class EntityBasic { [DataMember(Name = "name")] public string Name { get; set; } [DataMember(Name = "id")] public int Id { get; set; } - - [DataMember(Name = "key")] - public Guid Key { get; set; } - + [DataMember(Name = "icon")] public string Icon { get; set; } } diff --git a/src/Umbraco.Web/Models/Mapping/EntityModelMapper.cs b/src/Umbraco.Web/Models/Mapping/EntityModelMapper.cs new file mode 100644 index 0000000000..3f2061bced --- /dev/null +++ b/src/Umbraco.Web/Models/Mapping/EntityModelMapper.cs @@ -0,0 +1,17 @@ +using AutoMapper; +using Umbraco.Core; +using Umbraco.Core.Models; +using Umbraco.Core.Models.Mapping; +using Umbraco.Web.Models.ContentEditing; + +namespace Umbraco.Web.Models.Mapping +{ + internal class EntityModelMapper : MapperConfiguration + { + public override void ConfigureMappings(IConfiguration config, ApplicationContext applicationContext) + { + config.CreateMap() + .ForMember(basic => basic.Icon, expression => expression.MapFrom(entity => entity.ContentTypeIcon)); + } + } +} \ No newline at end of file diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj index 26fb87687e..764018f4a9 100644 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -299,8 +299,9 @@ - + +