-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTestUserService.cs
More file actions
68 lines (56 loc) · 2.27 KB
/
Copy pathTestUserService.cs
File metadata and controls
68 lines (56 loc) · 2.27 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
using System.Security.Claims;
using Microsoft.AspNetCore.Http;
using Microsoft.Identity.Web;
using PrismaApi.Application.Interfaces.Repositories;
using PrismaApi.Application.Interfaces.Services;
using PrismaApi.Application.Mapping;
using PrismaApi.Domain.Dtos;
namespace PrismaApi.Test.Mocks;
public class TestUserService : IUserService
{
private readonly IUserRepository _userRepository;
public TestUserService(IUserRepository userRepository)
{
_userRepository = userRepository;
}
public async Task<List<UserOutgoingDto>> GetAllAsync()
{
var users = await _userRepository.GetAllAsync(withTracking: false);
return users.ToOutgoingDtos();
}
public async Task<List<UserOutgoingDto>> SearchUsersAsync(string query)
{
var users = await _userRepository.GetAllAsync(withTracking: false);
return users.Where(u => u.Name.Contains(query, StringComparison.OrdinalIgnoreCase)).ToOutgoingDtos();
}
public async Task<UserOutgoingDto> GetOrCreateUserFromContextAsync(HttpContext context)
{
var oid = context.User.Claims.FirstOrDefault(c => c.Type == ClaimConstants.Oid)?.Value
?? context.User.Claims.FirstOrDefault(c => c.Type == ClaimConstants.ObjectId)?.Value;
if (string.IsNullOrEmpty(oid))
{
throw new InvalidOperationException("No Id found in Claims");
}
var name = context.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name)?.Value ?? "Test User";
var user = await _userRepository.GetOrAddByIdAsync(new UserIncomingDto
{
Id = oid,
Name = name
});
return user.ToOutgoingDto();
}
public async Task<List<UserOutgoingDto>> GetByIdsAsync(IEnumerable<string> ids)
{
var users = await _userRepository.GetByIdsAsync(ids, withTracking: false);
return users.ToOutgoingDtos();
}
public async Task DeleteUserAsync(string userId, UserOutgoingDto user, CancellationToken ct = default)
{
// user is the user making the request, and only allows to delete itself.
if (userId != user.Id)
{
throw new InvalidOperationException("Users can only delete themselves.");
}
await _userRepository.DeleteByIdsAsync([userId], ct: ct);
}
}