-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathSendOwnerQuery.cs
More file actions
54 lines (46 loc) · 1.66 KB
/
SendOwnerQuery.cs
File metadata and controls
54 lines (46 loc) · 1.66 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
using System.Security.Claims;
using Bit.Core.Exceptions;
using Bit.Core.Services;
using Bit.Core.Tools.Entities;
using Bit.Core.Tools.Repositories;
using Bit.Core.Tools.SendFeatures.Queries.Interfaces;
namespace Bit.Core.Tools.SendFeatures.Queries;
/// <inheritdoc cref="ISendOwnerQuery"/>
public class SendOwnerQuery : ISendOwnerQuery
{
private readonly ISendRepository _repository;
private readonly IUserService _users;
/// <summary>
/// Instantiates the command
/// </summary>
/// <param name="sendRepository">
/// Retrieves send records
/// </param>
/// <exception cref="ArgumentNullException">
/// Thrown when <paramref name="sendRepository"/> is <see langword="null"/>.
/// </exception>
public SendOwnerQuery(ISendRepository sendRepository, IUserService users)
{
_repository = sendRepository;
_users = users ?? throw new ArgumentNullException(nameof(users));
}
/// <inheritdoc cref="ISendOwnerQuery.Get"/>
public async Task<Send> Get(Guid id, ClaimsPrincipal user)
{
var userId = _users.GetProperUserId(user) ?? throw new BadRequestException("invalid user.");
var send = await _repository.GetByIdAsync(id);
if (send == null || send.UserId != userId)
{
throw new NotFoundException();
}
return send;
}
/// <inheritdoc cref="ISendOwnerQuery.GetOwned"/>
public async Task<ICollection<Send>> GetOwned(ClaimsPrincipal user)
{
var userId = _users.GetProperUserId(user) ?? throw new BadRequestException("invalid user.");
var sends = await _repository.GetManyByUserIdAsync(userId);
return sends;
}
}