Changed IContentType on IContent to a more simple class with lesser properties

This commit is contained in:
Bjarke Berg
2019-01-03 09:27:52 +01:00
parent 17d818b604
commit eda46a16d0
28 changed files with 278 additions and 150 deletions

View File

@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using AutoMapper;
using Umbraco.Core;
@@ -29,7 +30,8 @@ namespace Umbraco.Web.Models.Mapping
var creatorResolver = new CreatorResolver(userService);
var actionButtonsResolver = new ActionButtonsResolver(userService, contentService);
var childOfListViewResolver = new ContentChildOfListViewResolver(contentService, contentTypeService);
var contentTypeBasicResolver = new ContentTypeBasicResolver<IContent, ContentItemDisplay>();
var contentTypeBasicResolver = new ContentTypeBasicResolver<IContent, ContentItemDisplay>(contentTypeService);
var allowedTemplatesResolver = new AllowedTemplatesResolver(contentTypeService);
var defaultTemplateResolver = new DefaultTemplateResolver();
var variantResolver = new ContentVariantResolver(localizationService);
var schedPublishReleaseDateResolver = new ScheduledPublishDateResolver(ContentScheduleAction.Release);
@@ -56,10 +58,7 @@ namespace Umbraco.Web.Models.Mapping
.ForMember(dest => dest.Notifications, opt => opt.Ignore())
.ForMember(dest => dest.Errors, opt => opt.Ignore())
.ForMember(dest => dest.DocumentType, opt => opt.ResolveUsing(contentTypeBasicResolver))
.ForMember(dest => dest.AllowedTemplates, opt =>
opt.MapFrom(content => content.ContentType.AllowedTemplates
.Where(t => t.Alias.IsNullOrWhiteSpace() == false && t.Name.IsNullOrWhiteSpace() == false)
.ToDictionary(t => t.Alias, t => t.Name)))
.ForMember(dest => dest.AllowedTemplates, opt => opt.ResolveUsing(allowedTemplatesResolver))
.ForMember(dest => dest.AllowedActions, opt => opt.ResolveUsing(src => actionButtonsResolver.Resolve(src)))
.ForMember(dest => dest.AdditionalData, opt => opt.Ignore());
@@ -140,5 +139,27 @@ namespace Umbraco.Web.Models.Mapping
return source.CultureInfos.TryGetValue(culture, out var name) && !name.Name.IsNullOrWhiteSpace() ? name.Name : $"(({source.Name}))";
}
}
}
private class AllowedTemplatesResolver : IValueResolver<IContent, ContentItemDisplay, IDictionary<string, string>>
{
private readonly IContentTypeService _contentTypeService;
public AllowedTemplatesResolver(IContentTypeService contentTypeService)
{
_contentTypeService = contentTypeService;
}
public IDictionary<string, string> Resolve(IContent source, ContentItemDisplay destination, IDictionary<string, string> destMember, ResolutionContext context)
{
var contentType = _contentTypeService.Get(source.ContentTypeId);
return contentType.AllowedTemplates
.Where(t => t.Alias.IsNullOrWhiteSpace() == false && t.Name.IsNullOrWhiteSpace() == false)
.ToDictionary(t => t.Alias, t => t.Name);
}
}
}
}

View File

@@ -4,6 +4,7 @@ using System.Web;
using AutoMapper;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
using Umbraco.Web.Models.ContentEditing;
namespace Umbraco.Web.Models.Mapping
@@ -15,6 +16,13 @@ namespace Umbraco.Web.Models.Mapping
internal class ContentTypeBasicResolver<TSource, TDestination> : IValueResolver<TSource, TDestination, ContentTypeBasic>
where TSource : IContentBase
{
private readonly IContentTypeService _contentTypeService;
public ContentTypeBasicResolver(IContentTypeService contentTypeService)
{
_contentTypeService = contentTypeService;
}
public ContentTypeBasic Resolve(TSource source, TDestination destination, ContentTypeBasic destMember, ResolutionContext context)
{
//TODO: We can resolve the UmbracoContext from the IValueResolver options!
@@ -22,13 +30,8 @@ namespace Umbraco.Web.Models.Mapping
if (HttpContext.Current != null && UmbracoContext.Current != null && UmbracoContext.Current.Security.CurrentUser != null
&& UmbracoContext.Current.Security.CurrentUser.AllowedSections.Any(x => x.Equals(Constants.Applications.Settings)))
{
ContentTypeBasic contentTypeBasic;
if (source is IContent content)
contentTypeBasic = Mapper.Map<IContentType, ContentTypeBasic>(content.ContentType);
else if (source is IMedia media)
contentTypeBasic = Mapper.Map<IMediaType, ContentTypeBasic>(media.ContentType);
else
throw new NotSupportedException($"Expected TSource to be IContent or IMedia, got {typeof(TSource).Name}.");
var contentType = _contentTypeService.Get(source.ContentTypeId);
var contentTypeBasic = Mapper.Map<IContentType, ContentTypeBasic>(contentType);
return contentTypeBasic;
}

View File

@@ -15,8 +15,7 @@ namespace Umbraco.Web.Models.Mapping
/// </summary>
internal class MediaMapperProfile : Profile
{
public MediaMapperProfile(
TabsAndPropertiesResolver<IMedia, MediaItemDisplay> tabsAndPropertiesResolver,
public MediaMapperProfile(TabsAndPropertiesResolver<IMedia, MediaItemDisplay> tabsAndPropertiesResolver,
ContentTreeNodeUrlResolver<IMedia, MediaTreeController> contentTreeNodeUrlResolver,
MediaAppResolver mediaAppResolver,
IUserService userService,
@@ -24,12 +23,13 @@ namespace Umbraco.Web.Models.Mapping
IDataTypeService dataTypeService,
IMediaService mediaService,
IMediaTypeService mediaTypeService,
ILogger logger)
ILogger logger,
IContentTypeService contentTypeService)
{
// create, capture, cache
var mediaOwnerResolver = new OwnerResolver<IMedia>(userService);
var childOfListViewResolver = new MediaChildOfListViewResolver(mediaService, mediaTypeService);
var mediaTypeBasicResolver = new ContentTypeBasicResolver<IMedia, MediaItemDisplay>();
var mediaTypeBasicResolver = new ContentTypeBasicResolver<IMedia, MediaItemDisplay>(contentTypeService);
//FROM IMedia TO MediaItemDisplay
CreateMap<IMedia, MediaItemDisplay>()