From e7ca87f25dae2f806469043ee556d4790d9ebcae Mon Sep 17 00:00:00 2001 From: crupest Date: Tue, 10 Mar 2020 03:04:23 +0800 Subject: ... --- Timeline/Models/Http/Common.cs | 4 +- Timeline/Models/Http/ErrorResponse.cs | 4 +- Timeline/Models/Http/Timeline.cs | 119 +++++++++++++++++++++++++++++ Timeline/Models/Http/TimelineCommon.cs | 116 ---------------------------- Timeline/Models/Http/TimelineController.cs | 4 +- Timeline/Models/Http/TokenController.cs | 2 +- Timeline/Models/Http/UserController.cs | 6 +- Timeline/Models/Http/UserInfo.cs | 18 ++--- 8 files changed, 138 insertions(+), 135 deletions(-) create mode 100644 Timeline/Models/Http/Timeline.cs delete mode 100644 Timeline/Models/Http/TimelineCommon.cs (limited to 'Timeline/Models/Http') diff --git a/Timeline/Models/Http/Common.cs b/Timeline/Models/Http/Common.cs index a9fc8a79..f30fdb0d 100644 --- a/Timeline/Models/Http/Common.cs +++ b/Timeline/Models/Http/Common.cs @@ -1,6 +1,6 @@ -using static Timeline.Resources.Models.Http.Common; +using static TimelineApp.Resources.Models.Http.Common; -namespace Timeline.Models.Http +namespace TimelineApp.Models.Http { public class CommonResponse { diff --git a/Timeline/Models/Http/ErrorResponse.cs b/Timeline/Models/Http/ErrorResponse.cs index 9f7e70e1..d6cb5ca0 100644 --- a/Timeline/Models/Http/ErrorResponse.cs +++ b/Timeline/Models/Http/ErrorResponse.cs @@ -1,7 +1,7 @@  -using static Timeline.Resources.Messages; +using static TimelineApp.Resources.Messages; -namespace Timeline.Models.Http +namespace TimelineApp.Models.Http { public static class ErrorResponse diff --git a/Timeline/Models/Http/Timeline.cs b/Timeline/Models/Http/Timeline.cs new file mode 100644 index 00000000..144cca7c --- /dev/null +++ b/Timeline/Models/Http/Timeline.cs @@ -0,0 +1,119 @@ +using AutoMapper; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Infrastructure; +using Microsoft.AspNetCore.Mvc.Routing; +using System; +using System.Collections.Generic; +using TimelineApp.Controllers; + +namespace TimelineApp.Models.Http +{ + public class TimelinePostContentInfo + { + public string Type { get; set; } = default!; + public string? Text { get; set; } + public string? Url { get; set; } + } + + public class TimelinePostInfo + { + public long Id { get; set; } + public TimelinePostContentInfo Content { get; set; } = default!; + public DateTime Time { get; set; } + public UserInfo Author { get; set; } = default!; + public DateTime LastUpdated { get; set; } = default!; + } + + public class TimelineInfo + { + public string? Name { get; set; } + public string Description { get; set; } = default!; + public UserInfo Owner { get; set; } = default!; + public TimelineVisibility Visibility { get; set; } +#pragma warning disable CA2227 // Collection properties should be read only + public List Members { get; set; } = default!; +#pragma warning restore CA2227 // Collection properties should be read only + +#pragma warning disable CA1707 // Identifiers should not contain underscores + public TimelineInfoLinks _links { get; set; } = default!; +#pragma warning restore CA1707 // Identifiers should not contain underscores + } + + public class TimelineInfoLinks + { + public string Self { get; set; } = default!; + public string Posts { get; set; } = default!; + } + + public class TimelineInfoLinksValueResolver : IValueResolver + { + private readonly IActionContextAccessor _actionContextAccessor; + private readonly IUrlHelperFactory _urlHelperFactory; + + public TimelineInfoLinksValueResolver(IActionContextAccessor actionContextAccessor, IUrlHelperFactory urlHelperFactory) + { + _actionContextAccessor = actionContextAccessor; + _urlHelperFactory = urlHelperFactory; + } + + public TimelineInfoLinks Resolve(Timeline source, TimelineInfo destination, TimelineInfoLinks destMember, ResolutionContext context) + { + if (_actionContextAccessor.ActionContext == null) + throw new InvalidOperationException("No action context, can't fill urls."); + + var urlHelper = _urlHelperFactory.GetUrlHelper(_actionContextAccessor.ActionContext); + + + return new TimelineInfoLinks + { + 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 TimelinePostConverter : ITypeConverter + { + private readonly IActionContextAccessor _actionContextAccessor; + private readonly IUrlHelperFactory _urlHelperFactory; + + public TimelinePostConverter(IActionContextAccessor actionContextAccessor, IUrlHelperFactory urlHelperFactory) + { + _actionContextAccessor = actionContextAccessor; + _urlHelperFactory = urlHelperFactory; + } + + public TimelinePostContentInfo Convert(ITimelinePostContent source, TimelinePostContentInfo destination, 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) + { + return new TimelinePostContentInfo + { + Type = TimelinePostContentTypes.Text, + Text = textContent.Text + }; + } + else if (source is ImageTimelinePostContent imageContent) + { + return new TimelinePostContentInfo + { + Type = TimelinePostContentTypes.Image, + Url = urlHelper.ActionLink(action: "PostDataGet", nameof(TimelineController)[0..^nameof(Controller).Length], new { source.Name }) + }; + } + } + } + + public class TimelineInfoAutoMapperProfile : Profile + { + public TimelineInfoAutoMapperProfile() + { + CreateMap().ForMember(u => u._links, opt => opt.MapFrom()); + } + } +} diff --git a/Timeline/Models/Http/TimelineCommon.cs b/Timeline/Models/Http/TimelineCommon.cs deleted file mode 100644 index 3be4f895..00000000 --- a/Timeline/Models/Http/TimelineCommon.cs +++ /dev/null @@ -1,116 +0,0 @@ -using Microsoft.AspNetCore.Mvc; -using System; -using System.Collections.Generic; -using System.Text.Json.Serialization; -using Timeline.Controllers; - -namespace Timeline.Models.Http -{ - public enum TimelineVisibility - { - /// - /// All people including those without accounts. - /// - Public, - /// - /// Only people signed in. - /// - Register, - /// - /// Only member. - /// - Private - } - - public static class TimelinePostContentTypes - { - public const string Text = "text"; - public const string Image = "image"; - } - - public interface ITimelinePostContent - { - public string Type { get; } - } - - public class TextTimelinePostContent : ITimelinePostContent - { - public TextTimelinePostContent() { } - public TextTimelinePostContent(string text) { Text = text; } - - public string Type { get; } = TimelinePostContentTypes.Text; - public string Text { get; set; } = ""; - } - - public class ImageTimelinePostContent : ITimelinePostContent - { - public ImageTimelinePostContent() { } - public ImageTimelinePostContent(string dataTag) { DataTag = dataTag; } - - public string Type { get; } = TimelinePostContentTypes.Image; - [JsonIgnore] - public string DataTag { get; set; } = ""; - public string? Url { get; set; } = null; - } - - public class TimelinePostInfo - { - public long Id { get; set; } - // use object to make json serializer use the runtime type - public object Content { get; set; } = default!; - public DateTime Time { get; set; } - public UserInfo Author { get; set; } = default!; - public DateTime LastUpdated { get; set; } = default!; - } - - public class TimelineInfo - { - public string? Name { get; set; } - public string Description { get; set; } = default!; - public UserInfo Owner { get; set; } = default!; - public TimelineVisibility Visibility { get; set; } -#pragma warning disable CA2227 // Collection properties should be read only - public List Members { get; set; } = default!; -#pragma warning restore CA2227 // Collection properties should be read only - -#pragma warning disable CA1707 // Identifiers should not contain underscores - public TimelineInfoLinks? _links { get; set; } -#pragma warning restore CA1707 // Identifiers should not contain underscores - } - - public class TimelineInfoLinks - { - public string Self { get; set; } = default!; - public string Posts { get; set; } = default!; - } - - public static class TimelineInfoExtensions - { - public static TimelineInfo FillLinks(this TimelineInfo info, IUrlHelper urlHelper) - { - if (info == null) - throw new ArgumentNullException(nameof(info)); - if (urlHelper == null) - throw new ArgumentNullException(nameof(urlHelper)); - - if (string.IsNullOrEmpty(info.Name)) - { - info._links = new TimelineInfoLinks - { - Self = urlHelper.ActionLink(nameof(PersonalTimelineController.TimelineGet), nameof(PersonalTimelineController)[0..^nameof(Controller).Length], new { info.Owner.Username }), - Posts = urlHelper.ActionLink(nameof(PersonalTimelineController.PostListGet), nameof(PersonalTimelineController)[0..^nameof(Controller).Length], new { info.Owner.Username }) - }; - } - else - { - info._links = new TimelineInfoLinks - { - Self = urlHelper.ActionLink(nameof(TimelineController.TimelineGet), nameof(TimelineController)[0..^nameof(Controller).Length], new { info.Name }), - Posts = urlHelper.ActionLink(nameof(TimelineController.PostListGet), nameof(TimelineController)[0..^nameof(Controller).Length], new { info.Name }) - }; - } - - return info; - } - } -} diff --git a/Timeline/Models/Http/TimelineController.cs b/Timeline/Models/Http/TimelineController.cs index ce5f3b98..b389c549 100644 --- a/Timeline/Models/Http/TimelineController.cs +++ b/Timeline/Models/Http/TimelineController.cs @@ -1,8 +1,8 @@ using System; using System.ComponentModel.DataAnnotations; -using Timeline.Models.Validation; +using TimelineApp.Models.Validation; -namespace Timeline.Models.Http +namespace TimelineApp.Models.Http { public class TimelinePostCreateRequestContent { diff --git a/Timeline/Models/Http/TokenController.cs b/Timeline/Models/Http/TokenController.cs index ea8b59ed..3376fb61 100644 --- a/Timeline/Models/Http/TokenController.cs +++ b/Timeline/Models/Http/TokenController.cs @@ -1,6 +1,6 @@ using System.ComponentModel.DataAnnotations; -namespace Timeline.Models.Http +namespace TimelineApp.Models.Http { public class CreateTokenRequest { diff --git a/Timeline/Models/Http/UserController.cs b/Timeline/Models/Http/UserController.cs index e4c95cbd..c024c757 100644 --- a/Timeline/Models/Http/UserController.cs +++ b/Timeline/Models/Http/UserController.cs @@ -1,9 +1,9 @@ using AutoMapper; using System.ComponentModel.DataAnnotations; -using Timeline.Models.Validation; -using Timeline.Services; +using TimelineApp.Models.Validation; +using TimelineApp.Services; -namespace Timeline.Models.Http +namespace TimelineApp.Models.Http { public class UserPatchRequest { diff --git a/Timeline/Models/Http/UserInfo.cs b/Timeline/Models/Http/UserInfo.cs index 68c6d8bd..fcbbe7c9 100644 --- a/Timeline/Models/Http/UserInfo.cs +++ b/Timeline/Models/Http/UserInfo.cs @@ -2,19 +2,19 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Infrastructure; using Microsoft.AspNetCore.Mvc.Routing; -using Timeline.Controllers; -using Timeline.Services; +using System; +using TimelineApp.Controllers; -namespace Timeline.Models.Http +namespace TimelineApp.Models.Http { public class UserInfo { public string Username { get; set; } = default!; public string Nickname { get; set; } = default!; public bool? Administrator { get; set; } = default!; -#pragma warning disable CA1707 - public UserInfoLinks? _links { get; set; } -#pragma warning restore CA1707 +#pragma warning disable CA1707 // Identifiers should not contain underscores + public UserInfoLinks _links { get; set; } = default!; +#pragma warning restore CA1707 // Identifiers should not contain underscores } public class UserInfoLinks @@ -24,7 +24,7 @@ namespace Timeline.Models.Http public string Timeline { get; set; } = default!; } - public class UserInfoLinksValueResolver : IValueResolver + public class UserInfoLinksValueResolver : IValueResolver { private readonly IActionContextAccessor _actionContextAccessor; private readonly IUrlHelperFactory _urlHelperFactory; @@ -35,10 +35,10 @@ namespace Timeline.Models.Http _urlHelperFactory = urlHelperFactory; } - public UserInfoLinks? Resolve(User source, UserInfo destination, UserInfoLinks? destMember, ResolutionContext context) + public UserInfoLinks Resolve(User source, UserInfo destination, UserInfoLinks destMember, ResolutionContext context) { if (_actionContextAccessor.ActionContext == null) - return null; + throw new InvalidOperationException("No action context, can't fill urls."); var urlHelper = _urlHelperFactory.GetUrlHelper(_actionContextAccessor.ActionContext); var result = new UserInfoLinks -- cgit v1.2.3