Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix upload bugs on adding existing files #24

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ public interface IAccountRepository
Task CreateBulkAsync(List<Account> accounts);
Task<Account?> GetByIdAsync(long accountId);
Task<List<Account>> GetAllAccounts();
Task<List<long>> GetAllIdsAsync();
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ public interface ITransactionRepository
Task<List<Transaction>> GetAllTransactions();
Task<List<Transaction>> GetBySourceAccountId(long accountId);
Task<List<Transaction>> GetByDestinationAccountId(long accountId);
Task<List<long>> GetAllIdsAsync();
}
5 changes: 4 additions & 1 deletion src/Application/Services/DomainService/AccountService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ public async Task<Result> 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)
Expand Down
5 changes: 4 additions & 1 deletion src/Application/Services/DomainService/TransactionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ public async Task<Result> 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)
Expand Down
7 changes: 7 additions & 0 deletions src/Infrastructure/Repositories/AccountRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,11 @@ public async Task<List<Account>> GetAllAccounts()
{
return await _dbContext.Accounts.ToListAsync();
}

public async Task<List<long>> GetAllIdsAsync()
{
return await _dbContext.Accounts
.Select(a => a.AccountId)
.ToListAsync();
}
}
7 changes: 7 additions & 0 deletions src/Infrastructure/Repositories/TransactionRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,11 @@ public Task<List<Transaction>> GetByDestinationAccountId(long accountId)
.Where(transaction => transaction.DestinationAccountId == accountId)
.ToListAsync();
}

public async Task<List<long>> GetAllIdsAsync()
{
return await _dbContext.Transactions
.Select(a => a.TransactionId)
.ToListAsync();
}
}
Loading