aboutsummaryrefslogtreecommitdiff
path: root/BackEnd/Timeline/Services/Mapper/UserMapper.cs
blob: 8a41cd4b65866d2201ee7c6665b6b59b02d3e697 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using Microsoft.AspNetCore.Mvc;
using System;
using System.Security.Claims;
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 : IMapper<UserEntity, HttpUser>
    {
        private readonly DatabaseContext _database;
        private readonly IUserPermissionService _userPermissionService;

        public UserMapper(DatabaseContext database, IUserPermissionService userPermissionService)
        {
            _database = database;
            _userPermissionService = userPermissionService;
        }

        public async Task<HttpUser> MapAsync(UserEntity entity, IUrlHelper urlHelper, ClaimsPrincipal? user)
        {
            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("Get", "UserV2", new { username = entity.Username }) ?? throw new Exception("Failed to generate link for user self."),
                    avatar: urlHelper.ActionLink("Get", "UserAvatarV2", new { username = entity.Username }) ?? throw new Exception("Failed to generate link for user avatar.")
                )
            );
        }
    }
}