-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathSendRepository.cs
More file actions
108 lines (97 loc) · 3.9 KB
/
SendRepository.cs
File metadata and controls
108 lines (97 loc) · 3.9 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
99
100
101
102
103
104
105
106
107
108
#nullable enable
using AutoMapper;
using Bit.Core.KeyManagement.UserKey;
using Bit.Core.Tools.Repositories;
using Bit.Infrastructure.EntityFramework.Models;
using Bit.Infrastructure.EntityFramework.Repositories;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
namespace Bit.Infrastructure.EntityFramework.Tools.Repositories;
/// <inheritdoc cref="ISendRepository"/>
public class SendRepository : Repository<Core.Tools.Entities.Send, Send, Guid>, ISendRepository
{
/// <summary>
/// Initializes the <see cref="SendRepository"/>
/// </summary>
/// <param name="serviceScopeFactory">An IoC service locator.</param>
/// <param name="mapper">An automapper service.</param>
public SendRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
: base(serviceScopeFactory, mapper, (DatabaseContext context) => context.Sends)
{ }
/// <summary>
/// Saves a <see cref="Send"/> in the database.
/// </summary>
/// <param name="send">
/// The send being saved.
/// </param>
/// <returns>
/// A task that completes once the save is complete.
/// The task result contains the saved <see cref="Send"/>.
/// </returns>
public override async Task<Core.Tools.Entities.Send> CreateAsync(Core.Tools.Entities.Send send)
{
send = await base.CreateAsync(send);
using (var scope = ServiceScopeFactory.CreateScope())
{
var dbContext = GetDatabaseContext(scope);
if (send.UserId.HasValue)
{
await UserUpdateStorage(send.UserId.Value);
await dbContext.UserBumpAccountRevisionDateAsync(send.UserId.Value);
await dbContext.SaveChangesAsync();
}
}
return send;
}
/// <inheritdoc />
public async Task<ICollection<Core.Tools.Entities.Send>> GetManyByDeletionDateAsync(DateTime deletionDateBefore)
{
using (var scope = ServiceScopeFactory.CreateScope())
{
var dbContext = GetDatabaseContext(scope);
var results = await dbContext.Sends.Where(s => s.DeletionDate < deletionDateBefore).ToListAsync();
return Mapper.Map<List<Core.Tools.Entities.Send>>(results);
}
}
/// <inheritdoc />
public async Task<ICollection<Core.Tools.Entities.Send>> GetManyByUserIdAsync(Guid userId)
{
using (var scope = ServiceScopeFactory.CreateScope())
{
var dbContext = GetDatabaseContext(scope);
var results = await dbContext.Sends.Where(s => s.UserId == userId).ToListAsync();
return Mapper.Map<List<Core.Tools.Entities.Send>>(results);
}
}
/// <inheritdoc />
public async Task<ICollection<Core.Tools.Entities.Send>> GetManyByOrganizationIdAsync(Guid organizationId)
{
using (var scope = ServiceScopeFactory.CreateScope())
{
var dbContext = GetDatabaseContext(scope);
var results = await dbContext.Sends.Where(s => s.OrganizationId == organizationId).ToListAsync();
return Mapper.Map<List<Core.Tools.Entities.Send>>(results);
}
}
/// <inheritdoc />
public UpdateEncryptedDataForKeyRotation UpdateForKeyRotation(Guid userId,
IEnumerable<Core.Tools.Entities.Send> sends)
{
return async (_, _) =>
{
var newSends = sends.ToDictionary(s => s.Id);
using var scope = ServiceScopeFactory.CreateScope();
var dbContext = GetDatabaseContext(scope);
var userSends = await GetDbSet(dbContext)
.Where(s => s.UserId == userId)
.ToListAsync();
var validSends = userSends
.Where(send => newSends.ContainsKey(send.Id));
foreach (var send in validSends)
{
send.Key = newSends[send.Id].Key;
}
await dbContext.SaveChangesAsync();
};
}
}