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 ++++++++++++++++-------- Timeline/Models/Http/UserInfo.cs | 2 +- Timeline/Models/Timeline.cs | 17 ++++++++++++++--- 3 files changed, 31 insertions(+), 12 deletions(-) (limited to 'Timeline/Models') 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(); } } diff --git a/Timeline/Models/Http/UserInfo.cs b/Timeline/Models/Http/UserInfo.cs index 4f887549..b4bf14c1 100644 --- a/Timeline/Models/Http/UserInfo.cs +++ b/Timeline/Models/Http/UserInfo.cs @@ -45,7 +45,7 @@ namespace Timeline.Models.Http { Self = urlHelper.ActionLink(nameof(UserController.Get), nameof(UserController)[0..^nameof(Controller).Length], new { destination.Username }), Avatar = urlHelper.ActionLink(nameof(UserAvatarController.Get), nameof(UserAvatarController)[0..^nameof(Controller).Length], new { destination.Username }), - Timeline = urlHelper.ActionLink(nameof(PersonalTimelineController.TimelineGet), nameof(PersonalTimelineController)[0..^nameof(Controller).Length], new { destination.Username }) + Timeline = urlHelper.ActionLink(nameof(TimelineController.TimelineGet), nameof(TimelineController)[0..^nameof(Controller).Length], new { Name = "@" + destination.Username }) }; return result; } diff --git a/Timeline/Models/Timeline.cs b/Timeline/Models/Timeline.cs index 6d4c924d..803a5c5c 100644 --- a/Timeline/Models/Timeline.cs +++ b/Timeline/Models/Timeline.cs @@ -48,11 +48,22 @@ namespace Timeline.Models public class TimelinePost { + public TimelinePost(long id, ITimelinePostContent content, DateTime time, User author, DateTime lastUpdated, string timelineName) + { + Id = id; + Content = content; + Time = time; + Author = author; + LastUpdated = lastUpdated; + TimelineName = timelineName; + } + public long Id { get; set; } - public ITimelinePostContent Content { get; set; } = default!; + public ITimelinePostContent Content { get; set; } public DateTime Time { get; set; } - public User Author { get; set; } = default!; - public DateTime LastUpdated { get; set; } = default!; + public User Author { get; set; } + public DateTime LastUpdated { get; set; } + public string TimelineName { get; set; } } #pragma warning disable CA1724 // Type names should not match namespaces -- cgit v1.2.3