From 95be9f58b7cf76f1c585791447e8393d61862e1c Mon Sep 17 00:00:00 2001 From: crupest Date: Tue, 10 Mar 2020 19:37:58 +0800 Subject: ... --- Timeline/Models/Http/Timeline.cs | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'Timeline/Models/Http/Timeline.cs') diff --git a/Timeline/Models/Http/Timeline.cs b/Timeline/Models/Http/Timeline.cs index 55c3a3bf..9e2aefd0 100644 --- a/Timeline/Models/Http/Timeline.cs +++ b/Timeline/Models/Http/Timeline.cs @@ -72,25 +72,27 @@ namespace Timeline.Models.Http } } - public class TimelinePostConverter : ITypeConverter + public class TimelinePostContentResolver : IValueResolver { private readonly IActionContextAccessor _actionContextAccessor; private readonly IUrlHelperFactory _urlHelperFactory; - public TimelinePostConverter(IActionContextAccessor actionContextAccessor, IUrlHelperFactory urlHelperFactory) + public TimelinePostContentResolver(IActionContextAccessor actionContextAccessor, IUrlHelperFactory urlHelperFactory) { _actionContextAccessor = actionContextAccessor; _urlHelperFactory = urlHelperFactory; } - public TimelinePostContentInfo Convert(ITimelinePostContent source, TimelinePostContentInfo destination, ResolutionContext context) + public TimelinePostContentInfo Resolve(TimelinePost source, TimelinePostInfo destination, TimelinePostContentInfo destMember, ResolutionContext context) { if (_actionContextAccessor.ActionContext == null) throw new InvalidOperationException("No action context, can't fill urls."); var urlHelper = _urlHelperFactory.GetUrlHelper(_actionContextAccessor.ActionContext); - if (source is TextTimelinePostContent textContent) + var sourceContent = source.Content; + + if (sourceContent is TextTimelinePostContent textContent) { return new TimelinePostContentInfo { @@ -98,14 +100,21 @@ namespace Timeline.Models.Http Text = textContent.Text }; } - else if (source is ImageTimelinePostContent imageContent) + else if (sourceContent is ImageTimelinePostContent imageContent) { return new TimelinePostContentInfo { Type = TimelinePostContentTypes.Image, - Url = urlHelper.ActionLink(action: "PostDataGet", nameof(TimelineController)[0..^nameof(Controller).Length], new { source.Name }) + Url = urlHelper.ActionLink( + action: nameof(TimelineController.PostDataGet), + controller: nameof(TimelineController)[0..^nameof(Controller).Length], + values: new { Name = source.TimelineName, Id = source.Id }) }; } + else + { + throw new InvalidOperationException("Unknown content type."); + } } } @@ -114,8 +123,7 @@ namespace Timeline.Models.Http public TimelineInfoAutoMapperProfile() { CreateMap().ForMember(u => u._links, opt => opt.MapFrom()); - CreateMap(); - CreateMap().ConvertUsing(); + CreateMap().ForMember(p => p.Content, opt => opt.MapFrom()); CreateMap(); } } -- cgit v1.2.3