From d88391b5a17a767094a7f44e4d6f9c4c5cfc6b43 Mon Sep 17 00:00:00 2001 From: Mobin Barfi Date: Wed, 28 Aug 2024 00:24:42 +0330 Subject: [PATCH] fix: fix upload bugs on adding existing files --- .../Interfaces/Repositories/IAccountRepository.cs | 1 + .../Interfaces/Repositories/ITransactionRepository.cs | 1 + src/Application/Services/DomainService/AccountService.cs | 5 ++++- .../Services/DomainService/TransactionService.cs | 5 ++++- src/Infrastructure/Repositories/AccountRepository.cs | 7 +++++++ src/Infrastructure/Repositories/TransactionRepository.cs | 7 +++++++ 6 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/Application/Interfaces/Repositories/IAccountRepository.cs b/src/Application/Interfaces/Repositories/IAccountRepository.cs index 7386ef0..7e1c1d9 100644 --- a/src/Application/Interfaces/Repositories/IAccountRepository.cs +++ b/src/Application/Interfaces/Repositories/IAccountRepository.cs @@ -7,4 +7,5 @@ public interface IAccountRepository Task CreateBulkAsync(List accounts); Task GetByIdAsync(long accountId); Task> GetAllAccounts(); + Task> GetAllIdsAsync(); } \ No newline at end of file diff --git a/src/Application/Interfaces/Repositories/ITransactionRepository.cs b/src/Application/Interfaces/Repositories/ITransactionRepository.cs index 3aacf1b..684955e 100644 --- a/src/Application/Interfaces/Repositories/ITransactionRepository.cs +++ b/src/Application/Interfaces/Repositories/ITransactionRepository.cs @@ -8,4 +8,5 @@ public interface ITransactionRepository Task> GetAllTransactions(); Task> GetBySourceAccountId(long accountId); Task> GetByDestinationAccountId(long accountId); + Task> GetAllIdsAsync(); } \ No newline at end of file diff --git a/src/Application/Services/DomainService/AccountService.cs b/src/Application/Services/DomainService/AccountService.cs index 2d5ed80..d09360d 100644 --- a/src/Application/Services/DomainService/AccountService.cs +++ b/src/Application/Services/DomainService/AccountService.cs @@ -26,7 +26,10 @@ public async Task AddAccountsFromCsvAsync(string filePath) .ToList(); try { - await _accountRepository.CreateBulkAsync(accounts); + var existingAccountIds = await _accountRepository.GetAllIdsAsync(); + var newAccounts = accounts.Where(a => !existingAccountIds.Contains(a.AccountId)).ToList(); + + await _accountRepository.CreateBulkAsync(newAccounts); return Result.Ok(); } catch (Exception ex) diff --git a/src/Application/Services/DomainService/TransactionService.cs b/src/Application/Services/DomainService/TransactionService.cs index 4b13e8f..3cd71eb 100644 --- a/src/Application/Services/DomainService/TransactionService.cs +++ b/src/Application/Services/DomainService/TransactionService.cs @@ -27,7 +27,10 @@ public async Task AddTransactionsFromCsvAsync(string filePath) try { - await _transactionRepository.CreateBulkAsync(transactions); + var existingTransactionsIds = await _transactionRepository.GetAllIdsAsync(); + var newTransactions = transactions.Where(t => !existingTransactionsIds.Contains(t.TransactionId)).ToList(); + + await _transactionRepository.CreateBulkAsync(newTransactions); return Result.Ok(); } catch (Exception ex) diff --git a/src/Infrastructure/Repositories/AccountRepository.cs b/src/Infrastructure/Repositories/AccountRepository.cs index aae4a6e..705bcfb 100644 --- a/src/Infrastructure/Repositories/AccountRepository.cs +++ b/src/Infrastructure/Repositories/AccountRepository.cs @@ -29,4 +29,11 @@ public async Task> GetAllAccounts() { return await _dbContext.Accounts.ToListAsync(); } + + public async Task> GetAllIdsAsync() + { + return await _dbContext.Accounts + .Select(a => a.AccountId) + .ToListAsync(); + } } \ No newline at end of file diff --git a/src/Infrastructure/Repositories/TransactionRepository.cs b/src/Infrastructure/Repositories/TransactionRepository.cs index 7b7a774..edc5a6c 100644 --- a/src/Infrastructure/Repositories/TransactionRepository.cs +++ b/src/Infrastructure/Repositories/TransactionRepository.cs @@ -37,4 +37,11 @@ public Task> GetByDestinationAccountId(long accountId) .Where(transaction => transaction.DestinationAccountId == accountId) .ToListAsync(); } + + public async Task> GetAllIdsAsync() + { + return await _dbContext.Transactions + .Select(a => a.TransactionId) + .ToListAsync(); + } } \ No newline at end of file