diff options
author | crupest <crupest@outlook.com> | 2020-12-17 23:49:16 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-17 23:49:16 +0800 |
commit | ebc2892d1546b8b59bd1c9adabe8a96a2e2a0754 (patch) | |
tree | f27b279237a6a7ab3b6a09206f3fec6a5524674b /BackEnd/Timeline/Models/Http/User.cs | |
parent | b0ee9afddd4f7ecf8a183ab8d8e9e575324a2b68 (diff) | |
parent | d452781c81e6d19076cf9d356877e154b2e34e91 (diff) | |
download | timeline-ebc2892d1546b8b59bd1c9adabe8a96a2e2a0754.tar.gz timeline-ebc2892d1546b8b59bd1c9adabe8a96a2e2a0754.tar.bz2 timeline-ebc2892d1546b8b59bd1c9adabe8a96a2e2a0754.zip |
Merge pull request #192 from crupest/highlight-timeline
Highlight timeline.
Diffstat (limited to 'BackEnd/Timeline/Models/Http/User.cs')
-rw-r--r-- | BackEnd/Timeline/Models/Http/User.cs | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/BackEnd/Timeline/Models/Http/User.cs b/BackEnd/Timeline/Models/Http/User.cs new file mode 100644 index 00000000..bdb40b9f --- /dev/null +++ b/BackEnd/Timeline/Models/Http/User.cs @@ -0,0 +1,105 @@ +using AutoMapper;
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.AspNetCore.Mvc.Infrastructure;
+using Microsoft.AspNetCore.Mvc.Routing;
+using System.Collections.Generic;
+using Timeline.Controllers;
+using Timeline.Services;
+
+namespace Timeline.Models.Http
+{
+ /// <summary>
+ /// Info of a user.
+ /// </summary>
+ public class HttpUser
+ {
+ /// <summary>
+ /// Unique id.
+ /// </summary>
+ public string UniqueId { get; set; } = default!;
+ /// <summary>
+ /// Username.
+ /// </summary>
+ public string Username { get; set; } = default!;
+ /// <summary>
+ /// Nickname.
+ /// </summary>
+ public string Nickname { get; set; } = default!;
+#pragma warning disable CA2227 // Collection properties should be read only
+ /// <summary>
+ /// The permissions of the user.
+ /// </summary>
+ public List<string> Permissions { get; set; } = default!;
+#pragma warning restore CA2227 // Collection properties should be read only
+#pragma warning disable CA1707 // Identifiers should not contain underscores
+ /// <summary>
+ /// Related links.
+ /// </summary>
+ public HttpUserLinks _links { get; set; } = default!;
+#pragma warning restore CA1707 // Identifiers should not contain underscores
+ }
+
+ /// <summary>
+ /// Related links for user.
+ /// </summary>
+ public class HttpUserLinks
+ {
+ /// <summary>
+ /// Self.
+ /// </summary>
+ public string Self { get; set; } = default!;
+ /// <summary>
+ /// Avatar url.
+ /// </summary>
+ public string Avatar { get; set; } = default!;
+ /// <summary>
+ /// Personal timeline url.
+ /// </summary>
+ public string Timeline { get; set; } = default!;
+ }
+
+ public class HttpUserPermissionsValueConverter : ITypeConverter<UserPermissions, List<string>>
+ {
+ public List<string> Convert(UserPermissions source, List<string> destination, ResolutionContext context)
+ {
+ return source.ToStringList();
+ }
+ }
+
+ public class HttpUserLinksValueResolver : IValueResolver<UserInfo, HttpUser, HttpUserLinks>
+ {
+ private readonly IActionContextAccessor _actionContextAccessor;
+ private readonly IUrlHelperFactory _urlHelperFactory;
+
+ public HttpUserLinksValueResolver(IActionContextAccessor actionContextAccessor, IUrlHelperFactory urlHelperFactory)
+ {
+ _actionContextAccessor = actionContextAccessor;
+ _urlHelperFactory = urlHelperFactory;
+ }
+
+ public HttpUserLinks Resolve(UserInfo source, HttpUser destination, HttpUserLinks destMember, ResolutionContext context)
+ {
+ var actionContext = _actionContextAccessor.AssertActionContextForUrlFill();
+ var urlHelper = _urlHelperFactory.GetUrlHelper(actionContext);
+
+ var result = new HttpUserLinks
+ {
+ 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(TimelineController.TimelineGet), nameof(TimelineController)[0..^nameof(Controller).Length], new { Name = "@" + destination.Username })
+ };
+ return result;
+ }
+ }
+
+ public class HttpUserAutoMapperProfile : Profile
+ {
+ public HttpUserAutoMapperProfile()
+ {
+ CreateMap<UserPermissions, List<string>>()
+ .ConvertUsing<HttpUserPermissionsValueConverter>();
+ CreateMap<UserInfo, HttpUser>()
+ .ForMember(u => u._links, opt => opt.MapFrom<HttpUserLinksValueResolver>());
+ }
+ }
+}
|