diff options
author | crupest <crupest@outlook.com> | 2021-01-07 16:23:20 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-01-07 16:23:20 +0800 |
commit | 7594a16e38304739487b053405153379faee6e58 (patch) | |
tree | bb99d1a24fffc9c4142219b9c25dc66e3d2b60d2 /BackEnd/Timeline/Models/Mapper/UserMapper.cs | |
parent | 97e094c97dc9ed79cf7daa0a93568e1933015bdd (diff) | |
download | timeline-7594a16e38304739487b053405153379faee6e58.tar.gz timeline-7594a16e38304739487b053405153379faee6e58.tar.bz2 timeline-7594a16e38304739487b053405153379faee6e58.zip |
史诗级重构!
Diffstat (limited to 'BackEnd/Timeline/Models/Mapper/UserMapper.cs')
-rw-r--r-- | BackEnd/Timeline/Models/Mapper/UserMapper.cs | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/BackEnd/Timeline/Models/Mapper/UserMapper.cs b/BackEnd/Timeline/Models/Mapper/UserMapper.cs new file mode 100644 index 00000000..3255dca9 --- /dev/null +++ b/BackEnd/Timeline/Models/Mapper/UserMapper.cs @@ -0,0 +1,38 @@ +using Microsoft.AspNetCore.Mvc;
+using System.Collections.Generic;
+using System.Linq;
+using Timeline.Controllers;
+using Timeline.Entities;
+using Timeline.Models.Http;
+using Timeline.Services;
+
+namespace Timeline.Models.Mapper
+{
+ public static class UserMapper
+ {
+ public static HttpUser MapToHttp(this UserEntity entity, IUrlHelper urlHelper)
+ {
+ return new HttpUser(
+ uniqueId: entity.UniqueId,
+ username: entity.Username,
+ nickname: string.IsNullOrEmpty(entity.Nickname) ? entity.Username : entity.Nickname,
+ permissions: MapPermission(entity),
+ links: new HttpUserLinks(
+ self: urlHelper.ActionLink(nameof(UserController.Get), nameof(UserController)[0..^nameof(Controller).Length], new { entity.Username }),
+ avatar: urlHelper.ActionLink(nameof(UserAvatarController.Get), nameof(UserAvatarController)[0..^nameof(Controller).Length], new { entity.Username }),
+ timeline: urlHelper.ActionLink(nameof(TimelineController.TimelineGet), nameof(TimelineController)[0..^nameof(Controller).Length], new { timeline = "@" + entity.Username })
+ )
+ );
+ }
+
+ public static List<HttpUser> MapToHttp(this List<UserEntity> entities, IUrlHelper urlHelper)
+ {
+ return entities.Select(e => e.MapToHttp(urlHelper)).ToList();
+ }
+
+ private static List<string> MapPermission(UserEntity entity)
+ {
+ return entity.Permissions.Select(p => p.Permission).ToList();
+ }
+ }
+}
|