diff --git a/Crypter.Core/Features/Keys/Commands/InsertKeyPairCommand.cs b/Crypter.Core/Features/Keys/Commands/InsertKeyPairCommand.cs index c720b920a..4ce340959 100644 --- a/Crypter.Core/Features/Keys/Commands/InsertKeyPairCommand.cs +++ b/Crypter.Core/Features/Keys/Commands/InsertKeyPairCommand.cs @@ -52,19 +52,17 @@ public InsertKeyPairCommandHandler(DataContext dataContext) public async Task> Handle(InsertKeyPairCommand request, CancellationToken cancellationToken) { - UserKeyPairEntity? keyPairEntity = await _dataContext.UserKeyPairs - .FirstOrDefaultAsync(x => x.Owner == request.UserId, CancellationToken.None); + bool keyPairExists = await _dataContext.UserKeyPairs.AnyAsync(x => x.Owner == request.UserId, CancellationToken.None); - if (keyPairEntity is null) + if (!keyPairExists) { UserKeyPairEntity newEntity = new UserKeyPairEntity(request.UserId, request.Data.EncryptedPrivateKey, request.Data.PublicKey, request.Data.Nonce, DateTime.UtcNow); _dataContext.UserKeyPairs.Add(newEntity); await _dataContext.SaveChangesAsync(CancellationToken.None); + return Unit.Default; } - return keyPairEntity is null - ? Unit.Default - : InsertKeyPairError.KeyPairAlreadyExists; + return InsertKeyPairError.KeyPairAlreadyExists; } } diff --git a/Crypter.Core/Features/Keys/Commands/UpsertMasterKeyCommand.cs b/Crypter.Core/Features/Keys/Commands/UpsertMasterKeyCommand.cs index 8b28fd552..8800b25ee 100644 --- a/Crypter.Core/Features/Keys/Commands/UpsertMasterKeyCommand.cs +++ b/Crypter.Core/Features/Keys/Commands/UpsertMasterKeyCommand.cs @@ -80,10 +80,10 @@ public async Task> Handle(UpsertMasterKeyComm _ => InsertMasterKeyError.InvalidPassword, async _ => { - UserMasterKeyEntity? masterKeyEntity = await _dataContext.UserMasterKeys - .FirstOrDefaultAsync(x => x.Owner == request.UserId, CancellationToken.None); + bool masterKeyExists = await _dataContext.UserMasterKeys + .AnyAsync(x => x.Owner == request.UserId, CancellationToken.None); - if (masterKeyEntity is not null && !request.AllowReplacement) + if (masterKeyExists && !request.AllowReplacement) { return InsertMasterKeyError.Conflict; } diff --git a/Crypter.Core/Features/Reports/Queries/ApplicationAnalyticsReportQuery.cs b/Crypter.Core/Features/Reports/Queries/ApplicationAnalyticsReportQuery.cs index 9467aef9e..845507cf2 100644 --- a/Crypter.Core/Features/Reports/Queries/ApplicationAnalyticsReportQuery.cs +++ b/Crypter.Core/Features/Reports/Queries/ApplicationAnalyticsReportQuery.cs @@ -57,7 +57,7 @@ public async Task Handle(ApplicationAnalyticsReportQ DateTimeOffset now = DateTimeOffset.UtcNow; DateTimeOffset reportBegin = DateTimeOffset.UtcNow.AddDays(-request.ReportPeriodDays); - List events = await _dataContext.EventLogs + List events = await _dataContext.EventLogs.AsNoTracking() .Where(eventLog => eventLog.Timestamp <= now && eventLog.Timestamp >= reportBegin) .ToListAsync(cancellationToken); diff --git a/Crypter.Core/Features/Transfer/Events/FailedMultipartFileTransferAbandonEvent.cs b/Crypter.Core/Features/Transfer/Events/FailedMultipartFileTransferAbandonEvent.cs index e3e252e08..41b01880e 100644 --- a/Crypter.Core/Features/Transfer/Events/FailedMultipartFileTransferAbandonEvent.cs +++ b/Crypter.Core/Features/Transfer/Events/FailedMultipartFileTransferAbandonEvent.cs @@ -30,7 +30,6 @@ using Crypter.Common.Contracts.Features.Transfer; using Crypter.Common.Enums; using Crypter.Core.Services; -using Crypter.DataAccess; using Hangfire; using MediatR; @@ -43,16 +42,13 @@ internal class FailedMultipartFileTransferAbandonEventHandler : INotificationHandler { private readonly IBackgroundJobClient _backgroundJobClient; - private readonly DataContext _dataContext; private readonly IHangfireBackgroundService _hangfireBackgroundService; public FailedMultipartFileTransferAbandonEventHandler( IBackgroundJobClient backgroundJobClient, - DataContext dataContext, IHangfireBackgroundService hangfireBackgroundService) { _backgroundJobClient = backgroundJobClient; - _dataContext = dataContext; _hangfireBackgroundService = hangfireBackgroundService; } diff --git a/Crypter.Core/Features/Transfer/Events/SuccessfulMultipartFileTransferAbandonEvent.cs b/Crypter.Core/Features/Transfer/Events/SuccessfulMultipartFileTransferAbandonEvent.cs index ec8f343ab..4574efca9 100644 --- a/Crypter.Core/Features/Transfer/Events/SuccessfulMultipartFileTransferAbandonEvent.cs +++ b/Crypter.Core/Features/Transfer/Events/SuccessfulMultipartFileTransferAbandonEvent.cs @@ -29,7 +29,6 @@ using System.Threading.Tasks; using Crypter.Common.Enums; using Crypter.Core.Services; -using Crypter.DataAccess; using Hangfire; using MediatR; @@ -42,16 +41,13 @@ internal class SuccessfulMultipartFileTransferAbandonEventHandler : INotificationHandler { private readonly IBackgroundJobClient _backgroundJobClient; - private readonly DataContext _dataContext; private readonly IHangfireBackgroundService _hangfireBackgroundService; public SuccessfulMultipartFileTransferAbandonEventHandler( IBackgroundJobClient backgroundJobClient, - DataContext dataContext, IHangfireBackgroundService hangfireBackgroundService) { _backgroundJobClient = backgroundJobClient; - _dataContext = dataContext; _hangfireBackgroundService = hangfireBackgroundService; } diff --git a/Crypter.Core/Features/UserAuthentication/Common.cs b/Crypter.Core/Features/UserAuthentication/Common.cs index b5f145209..713a22f1b 100644 --- a/Crypter.Core/Features/UserAuthentication/Common.cs +++ b/Crypter.Core/Features/UserAuthentication/Common.cs @@ -92,7 +92,7 @@ Maybe VerifyUserPasswordIsMigrated(UserEntity user, T error) Task> FetchUserAsync(T error) { return Either.FromRightAsync( - dataContext.Users.FirstOrDefaultAsync(x => x.Id == userId, cancellationToken), + dataContext.Users.AsNoTracking().FirstOrDefaultAsync(x => x.Id == userId, cancellationToken), error); }