-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserService.cs
More file actions
98 lines (87 loc) · 3.69 KB
/
UserService.cs
File metadata and controls
98 lines (87 loc) · 3.69 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Graph;
using PrismaApi.Application.Interfaces.Repositories;
using PrismaApi.Application.Interfaces.Services;
using PrismaApi.Application.Mapping;
using PrismaApi.Domain.Dtos;
using PrismaApi.Domain.Constants;
using PrismaApi.Infrastructure.Caching;
using Scampi.Domain.Extensions;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace PrismaApi.Application.Services;
public class UserService : IUserService
{
private readonly IUserRepository _userRepository;
private readonly GraphServiceClient _graphServiceClient;
private readonly IMemoryCache _memoryCache;
public UserService(IUserRepository userRepository, GraphServiceClient graphServiceClient, IMemoryCache memoryCache)
{
_userRepository = userRepository;
_graphServiceClient = graphServiceClient;
_memoryCache = memoryCache;
}
public async Task<List<UserOutgoingDto>> GetAllAsync()
{
var users = await _userRepository.GetAllAsync(withTracking: false);
return users.ToOutgoingDtos();
}
public async Task<UserOutgoingDto> GetOrCreateUserByIdAsync(UserIncomingDto dto)
{
var user = (await _userRepository.GetOrAddByIdAsync(dto)).ToOutgoingDto();
return user;
}
public async Task<UserOutgoingDto> GetOrCreateUserFromGraphMeAsync(string? cacheKey)
{
if (cacheKey != null && _memoryCache.TryGetValue(cacheKey, out UserOutgoingDto? cachedUser) && cachedUser != null)
{
return cachedUser;
}
var graphUser = await _graphServiceClient.Me.GetAsync();
if (graphUser == null || graphUser.Id == null) throw new Exception("User not found");
var userDto = new UserIncomingDto
{
Id = graphUser.Id.ToString(),
Name = graphUser.DisplayName ?? "",
};
var user = (await _userRepository.GetOrAddByIdAsync(userDto)).ToOutgoingDto();
if (cacheKey != null)
_memoryCache.AddCacheItem(new CacheItem { CacheKey = cacheKey }, TimeSpan.FromMinutes(30), user);
return user;
}
public async Task<UserOutgoingDto> GetOrCreateUserFromContextAsync(HttpContext context)
{
var oid = context.User.Claims
.FirstOrDefault(c => c.Type == "http://schemas.microsoft.com/identity/claims/objectidentifier")?.Value;
if (string.IsNullOrEmpty(oid))
{
throw new InvalidOperationException("No Id found in Claims");
}
var user = await GetOrCreateUserFromGraphMeAsync(oid);
return user;
}
public async Task<List<UserOutgoingDto>> GetByIdsAsync(IEnumerable<string> ids)
{
var users = await _userRepository.GetByIdsAsync(ids, withTracking: false);
return users.ToOutgoingDtos();
}
public async Task<List<UserOutgoingDto>> SearchUsersFromGraphAsync(string query)
{
string sanitizedQuery = query.SanitizeQuery();
var users = await _graphServiceClient.Users
.GetAsync(config =>
{
config.QueryParameters.Search = $"\"displayName:{sanitizedQuery}\" OR \"mail:{sanitizedQuery}\"";
config.QueryParameters.Select = GraphApiConstants.UserSearchSelectFields;
config.QueryParameters.Top = GraphApiConstants.DefaultSearchTop;
config.Headers.Add(GraphApiConstants.ConsistencyLevelHeader, GraphApiConstants.ConsistencyLevelEventual);
});
return users?.Value?.Select(u => new UserOutgoingDto
{
Id = u.Id ?? "",
Name = u.DisplayName ?? u.UserPrincipalName ?? "",
}).ToList() ?? new List<UserOutgoingDto>();
}
}