-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathUserService.cs
More file actions
47 lines (39 loc) · 1.61 KB
/
Copy pathUserService.cs
File metadata and controls
47 lines (39 loc) · 1.61 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
using Microsoft.AspNetCore.Http;
using PrismaApi.Application.Interfaces.Repositories;
using PrismaApi.Application.Interfaces.Services;
using PrismaApi.Application.Mapping;
using PrismaApi.Domain.Dtos;
namespace PrismaApi.Application.Services;
public class UserService : IUserService
{
private readonly IUserRepository _userRepository;
private readonly IUserProvider _userProvider;
public UserService(IUserRepository userRepository, IUserProvider userProvider)
{
_userRepository = userRepository;
_userProvider = userProvider;
}
public async Task<List<UserOutgoingDto>> GetAllAsync()
{
var users = await _userRepository.GetAllAsync(withTracking: false);
return users.ToOutgoingDtos();
}
public async Task<List<UserOutgoingDto>> GetByIdsAsync(IEnumerable<string> ids)
{
var users = await _userRepository.GetByIdsAsync(ids, withTracking: false);
return users.ToOutgoingDtos();
}
public Task<UserOutgoingDto> GetOrCreateUserFromContextAsync(HttpContext context)
=> _userProvider.ResolveUserFromContextAsync(context);
public Task<List<UserOutgoingDto>> SearchUsersAsync(string query)
=> _userProvider.SearchUsersAsync(query);
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);
}
}