aboutsummaryrefslogtreecommitdiff
path: root/BackEnd/Timeline/Controllers/V2/V2ControllerBase.cs
blob: d6fa0c84ec5fcb14e69e8433be266c32174409a4 (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Timeline.Auth;
using Timeline.Services.Mapper;
using Timeline.Services.User;

namespace Timeline.Controllers.V2
{
    public class V2ControllerBase : ControllerBase
    {
        #region auth
        protected bool UserHasPermission(UserPermission permission)
        {
            return User.HasPermission(permission);
        }

        protected long? GetOptionalAuthUserId()
        {
            return User.GetOptionalUserId();
        }

        protected long GetAuthUserId()
        {
            return GetOptionalAuthUserId() ?? throw new InvalidOperationException(Resource.ExceptionNoUserId);
        }
        #endregion

        #region mapper
        protected IGenericMapper GetMapper()
        {
            return HttpContext.RequestServices.GetRequiredService<IGenericMapper>();
        }

        protected async Task<T> MapAsync<T>(object o)
        {
            return await GetMapper().MapAsync<T>(o, Url, User);
        }

        protected async Task<List<T>> MapListAsync<T>(IEnumerable<object> o)
        {
            return await GetMapper().MapListAsync<T>(o, Url, User);
        }

        protected T AutoMapperMap<T>(object o)
        {
            return GetMapper().AutoMapperMap<T>(o);
        }
        #endregion
    }
}