aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Models/Http/UserInfo.cs
diff options
context:
space:
mode:
author杨宇千 <crupest@outlook.com>2020-02-01 00:26:35 +0800
committerGitHub <noreply@github.com>2020-02-01 00:26:35 +0800
commit7b962cd876719fb871569ba3c97fb5545721a3f8 (patch)
treef02f8d57440c777d4732bc4439f82e8b25c6732c /Timeline/Models/Http/UserInfo.cs
parent289c7e1fada1f4dae6ce5e421e997ebddd55c2df (diff)
parentbcb0a2361467614531a337282da1fd23996924f1 (diff)
downloadtimeline-7b962cd876719fb871569ba3c97fb5545721a3f8.tar.gz
timeline-7b962cd876719fb871569ba3c97fb5545721a3f8.tar.bz2
timeline-7b962cd876719fb871569ba3c97fb5545721a3f8.zip
Merge pull request #56 from crupest/dev
Refactor API to be RESTful.
Diffstat (limited to 'Timeline/Models/Http/UserInfo.cs')
-rw-r--r--Timeline/Models/Http/UserInfo.cs59
1 files changed, 59 insertions, 0 deletions
diff --git a/Timeline/Models/Http/UserInfo.cs b/Timeline/Models/Http/UserInfo.cs
new file mode 100644
index 00000000..0d1d702b
--- /dev/null
+++ b/Timeline/Models/Http/UserInfo.cs
@@ -0,0 +1,59 @@
+using AutoMapper;
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.AspNetCore.Mvc.Infrastructure;
+using Microsoft.AspNetCore.Mvc.Routing;
+using Timeline.Controllers;
+using Timeline.Services;
+
+namespace Timeline.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
+ }
+
+ public class UserInfoLinks
+ {
+ public string Avatar { get; set; } = default!;
+ public string Timeline { get; set; } = default!;
+ }
+
+ public class UserInfoLinksValueResolver : IValueResolver<User, UserInfo, UserInfoLinks?>
+ {
+ private readonly IActionContextAccessor _actionContextAccessor;
+ private readonly IUrlHelperFactory _urlHelperFactory;
+
+ public UserInfoLinksValueResolver(IActionContextAccessor actionContextAccessor, IUrlHelperFactory urlHelperFactory)
+ {
+ _actionContextAccessor = actionContextAccessor;
+ _urlHelperFactory = urlHelperFactory;
+ }
+
+ public UserInfoLinks? Resolve(User source, UserInfo destination, UserInfoLinks? destMember, ResolutionContext context)
+ {
+ if (_actionContextAccessor.ActionContext == null)
+ return null;
+
+ var urlHelper = _urlHelperFactory.GetUrlHelper(_actionContextAccessor.ActionContext);
+ var result = new UserInfoLinks
+ {
+ 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 })
+ };
+ return result;
+ }
+ }
+
+ public class UserInfoAutoMapperProfile : Profile
+ {
+ public UserInfoAutoMapperProfile()
+ {
+ CreateMap<User, UserInfo>().ForMember(u => u._links, opt => opt.MapFrom<UserInfoLinksValueResolver>());
+ }
+ }
+}