From 7594a16e38304739487b053405153379faee6e58 Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 7 Jan 2021 16:23:20 +0800 Subject: 史诗级重构! MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BackEnd/Timeline/Models/Http/Timeline.cs | 137 +++++++++++-------------------- 1 file changed, 48 insertions(+), 89 deletions(-) (limited to 'BackEnd/Timeline/Models/Http/Timeline.cs') diff --git a/BackEnd/Timeline/Models/Http/Timeline.cs b/BackEnd/Timeline/Models/Http/Timeline.cs index 8e3831e1..06fa4e5a 100644 --- a/BackEnd/Timeline/Models/Http/Timeline.cs +++ b/BackEnd/Timeline/Models/Http/Timeline.cs @@ -1,10 +1,5 @@ -using AutoMapper; -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.Infrastructure; -using Microsoft.AspNetCore.Mvc.Routing; -using System; +using System; using System.Collections.Generic; -using Timeline.Controllers; namespace Timeline.Models.Http { @@ -13,6 +8,16 @@ namespace Timeline.Models.Http /// public class HttpTimelinePostContent { + public HttpTimelinePostContent() { } + + public HttpTimelinePostContent(string type, string? text, string? url, string? eTag) + { + Type = type; + Text = text; + Url = url; + ETag = eTag; + } + /// /// Type of the post content. /// @@ -36,6 +41,18 @@ namespace Timeline.Models.Http /// public class HttpTimelinePost { + public HttpTimelinePost() { } + + public HttpTimelinePost(long id, HttpTimelinePostContent? content, bool deleted, DateTime time, HttpUser? author, DateTime lastUpdated) + { + Id = id; + Content = content; + Deleted = deleted; + Time = time; + Author = author; + LastUpdated = lastUpdated; + } + /// /// Post id. /// @@ -67,6 +84,23 @@ namespace Timeline.Models.Http /// public class HttpTimeline { + public HttpTimeline() { } + + public HttpTimeline(string uniqueId, string title, string name, DateTime nameLastModifed, string description, HttpUser owner, TimelineVisibility visibility, List members, DateTime createTime, DateTime lastModified, HttpTimelineLinks links) + { + UniqueId = uniqueId; + Title = title; + Name = name; + NameLastModifed = nameLastModifed; + Description = description; + Owner = owner; + Visibility = visibility; + Members = members; + CreateTime = createTime; + LastModified = lastModified; + _links = links; + } + /// /// Unique id. /// @@ -123,6 +157,14 @@ namespace Timeline.Models.Http /// public class HttpTimelineLinks { + public HttpTimelineLinks() { } + + public HttpTimelineLinks(string self, string posts) + { + Self = self; + Posts = posts; + } + /// /// Self. /// @@ -132,87 +174,4 @@ namespace Timeline.Models.Http /// public string Posts { get; set; } = default!; } - - public class HttpTimelineLinksValueResolver : IValueResolver - { - private readonly IActionContextAccessor _actionContextAccessor; - private readonly IUrlHelperFactory _urlHelperFactory; - - public HttpTimelineLinksValueResolver(IActionContextAccessor actionContextAccessor, IUrlHelperFactory urlHelperFactory) - { - _actionContextAccessor = actionContextAccessor; - _urlHelperFactory = urlHelperFactory; - } - - public HttpTimelineLinks Resolve(TimelineInfo source, HttpTimeline destination, HttpTimelineLinks destMember, ResolutionContext context) - { - var actionContext = _actionContextAccessor.AssertActionContextForUrlFill(); - var urlHelper = _urlHelperFactory.GetUrlHelper(actionContext); - - return new HttpTimelineLinks - { - Self = urlHelper.ActionLink(nameof(TimelineController.TimelineGet), nameof(TimelineController)[0..^nameof(Controller).Length], new { source.Name }), - Posts = urlHelper.ActionLink(nameof(TimelineController.PostListGet), nameof(TimelineController)[0..^nameof(Controller).Length], new { source.Name }) - }; - } - } - - public class HttpTimelinePostContentResolver : IValueResolver - { - private readonly IActionContextAccessor _actionContextAccessor; - private readonly IUrlHelperFactory _urlHelperFactory; - - public HttpTimelinePostContentResolver(IActionContextAccessor actionContextAccessor, IUrlHelperFactory urlHelperFactory) - { - _actionContextAccessor = actionContextAccessor; - _urlHelperFactory = urlHelperFactory; - } - - public HttpTimelinePostContent? Resolve(TimelinePostInfo source, HttpTimelinePost destination, HttpTimelinePostContent? destMember, ResolutionContext context) - { - var actionContext = _actionContextAccessor.AssertActionContextForUrlFill(); - var urlHelper = _urlHelperFactory.GetUrlHelper(actionContext); - - var sourceContent = source.Content; - - if (sourceContent == null) - { - return null; - } - - if (sourceContent is TextTimelinePostContent textContent) - { - return new HttpTimelinePostContent - { - Type = TimelinePostContentTypes.Text, - Text = textContent.Text - }; - } - else if (sourceContent is ImageTimelinePostContent imageContent) - { - return new HttpTimelinePostContent - { - Type = TimelinePostContentTypes.Image, - Url = urlHelper.ActionLink( - action: nameof(TimelineController.PostDataGet), - controller: nameof(TimelineController)[0..^nameof(Controller).Length], - values: new { Name = source.TimelineName, Id = source.Id }), - ETag = $"\"{imageContent.DataTag}\"" - }; - } - else - { - throw new InvalidOperationException(Resources.Models.Http.Exception.UnknownPostContentType); - } - } - } - - public class HttpTimelineAutoMapperProfile : Profile - { - public HttpTimelineAutoMapperProfile() - { - CreateMap().ForMember(u => u._links, opt => opt.MapFrom()); - CreateMap().ForMember(p => p.Content, opt => opt.MapFrom()); - } - } } -- cgit v1.2.3