diff options
author | crupest <crupest@outlook.com> | 2021-04-25 21:20:04 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-04-25 21:20:04 +0800 |
commit | 657fb589137099794e58fbd35beb7d942b376965 (patch) | |
tree | 7ab03d970f4c556b0a005f94da2e0752e9d7ce99 /BackEnd/Timeline/Services/Mapper/UserMapper.cs | |
parent | b64806226723df9a9deb64e80defc93860896f50 (diff) | |
download | timeline-657fb589137099794e58fbd35beb7d942b376965.tar.gz timeline-657fb589137099794e58fbd35beb7d942b376965.tar.bz2 timeline-657fb589137099794e58fbd35beb7d942b376965.zip |
...
Diffstat (limited to 'BackEnd/Timeline/Services/Mapper/UserMapper.cs')
-rw-r--r-- | BackEnd/Timeline/Services/Mapper/UserMapper.cs | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/BackEnd/Timeline/Services/Mapper/UserMapper.cs b/BackEnd/Timeline/Services/Mapper/UserMapper.cs new file mode 100644 index 00000000..42f88d8a --- /dev/null +++ b/BackEnd/Timeline/Services/Mapper/UserMapper.cs @@ -0,0 +1,47 @@ +using Microsoft.AspNetCore.Mvc;
+using System.Collections.Generic;
+using System.Threading.Tasks;
+using Timeline.Controllers;
+using Timeline.Entities;
+using Timeline.Models.Http;
+using Timeline.Services.User;
+
+namespace Timeline.Services.Mapper
+{
+ public class UserMapper
+ {
+ private readonly DatabaseContext _database;
+ private readonly IUserPermissionService _userPermissionService;
+
+ public UserMapper(DatabaseContext database, IUserPermissionService userPermissionService)
+ {
+ _database = database;
+ _userPermissionService = userPermissionService;
+ }
+
+ public async Task<HttpUser> MapToHttp(UserEntity entity, IUrlHelper urlHelper)
+ {
+ return new HttpUser(
+ uniqueId: entity.UniqueId,
+ username: entity.Username,
+ nickname: string.IsNullOrEmpty(entity.Nickname) ? entity.Username : entity.Nickname,
+ permissions: (await _userPermissionService.GetPermissionsOfUserAsync(entity.Id, false)).ToStringList(),
+ 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 async Task<List<HttpUser>> MapToHttp(List<UserEntity> entities, IUrlHelper urlHelper)
+ {
+ var result = new List<HttpUser>();
+ foreach (var entity in entities)
+ {
+ result.Add(await MapToHttp(entity, urlHelper));
+ }
+ return result;
+ }
+ }
+}
|