From 0337ad48d87ed638d958738ddb2f684f5095eba3 Mon Sep 17 00:00:00 2001 From: amiralirahimii Date: Tue, 20 Aug 2024 17:10:31 +0330 Subject: [PATCH] refactor: move get-transactions-by-account-id to transactions controller --- .../Repositories/IAccountRepository.cs | 1 - .../Repositories/ITransactionRepository.cs | 2 ++ .../Interfaces/Services/IAccountService.cs | 1 - .../Interfaces/Services/ITransactionService.cs | 1 + .../Services/DomainService/AccountService.cs | 13 ------------- .../DomainService/TransactionService.cs | 7 +++++++ .../Repositories/AccountRepository.cs | 14 -------------- .../Repositories/TransactionRepository.cs | 14 ++++++++++++++ src/Web/Controllers/AccountController.cs | 18 ------------------ src/Web/Controllers/TransactionController.cs | 12 ++++++++++++ 10 files changed, 36 insertions(+), 47 deletions(-) diff --git a/src/Application/Interfaces/Repositories/IAccountRepository.cs b/src/Application/Interfaces/Repositories/IAccountRepository.cs index 333c1dd..7386ef0 100644 --- a/src/Application/Interfaces/Repositories/IAccountRepository.cs +++ b/src/Application/Interfaces/Repositories/IAccountRepository.cs @@ -6,6 +6,5 @@ public interface IAccountRepository { Task CreateBulkAsync(List accounts); Task GetByIdAsync(long accountId); - Task> GetTransactionsByAccountId(long accountId); Task> GetAllAccounts(); } \ No newline at end of file diff --git a/src/Application/Interfaces/Repositories/ITransactionRepository.cs b/src/Application/Interfaces/Repositories/ITransactionRepository.cs index 30bb69a..3aacf1b 100644 --- a/src/Application/Interfaces/Repositories/ITransactionRepository.cs +++ b/src/Application/Interfaces/Repositories/ITransactionRepository.cs @@ -6,4 +6,6 @@ public interface ITransactionRepository { Task CreateBulkAsync(List transactions); Task> GetAllTransactions(); + Task> GetBySourceAccountId(long accountId); + Task> GetByDestinationAccountId(long accountId); } \ No newline at end of file diff --git a/src/Application/Interfaces/Services/IAccountService.cs b/src/Application/Interfaces/Services/IAccountService.cs index 6ce3407..0596443 100644 --- a/src/Application/Interfaces/Services/IAccountService.cs +++ b/src/Application/Interfaces/Services/IAccountService.cs @@ -7,6 +7,5 @@ public interface IAccountService { Task AddAccountsFromCsvAsync(string filePath); Task GetAccountByIdAsync(long accountId); - Task>> GetTransactionsByUserId(long accountId); Task>> GetAllAccountsAsync(); } \ No newline at end of file diff --git a/src/Application/Interfaces/Services/ITransactionService.cs b/src/Application/Interfaces/Services/ITransactionService.cs index ec5bb7e..0d316c2 100644 --- a/src/Application/Interfaces/Services/ITransactionService.cs +++ b/src/Application/Interfaces/Services/ITransactionService.cs @@ -7,4 +7,5 @@ public interface ITransactionService { Task AddTransactionsFromCsvAsync(string filePath); Task>> GetAllTransactionsAsync(); + Task> GetTransactionsByAccountIdAsync(long accountId); } \ No newline at end of file diff --git a/src/Application/Services/DomainService/AccountService.cs b/src/Application/Services/DomainService/AccountService.cs index 3fe767a..2d5ed80 100644 --- a/src/Application/Services/DomainService/AccountService.cs +++ b/src/Application/Services/DomainService/AccountService.cs @@ -39,19 +39,6 @@ public async Task AddAccountsFromCsvAsync(string filePath) { return await _accountRepository.GetByIdAsync(accountId); } - - public async Task>> GetTransactionsByUserId(long accountId) - { - var account = await _accountRepository.GetByIdAsync(accountId); - - if (account == null) - { - return Result>.Fail("Account not found"); - } - - var transactions = await _accountRepository.GetTransactionsByAccountId(accountId); - return Result>.Ok(transactions); - } public async Task>> GetAllAccountsAsync() { diff --git a/src/Application/Services/DomainService/TransactionService.cs b/src/Application/Services/DomainService/TransactionService.cs index 88897d5..98b2553 100644 --- a/src/Application/Services/DomainService/TransactionService.cs +++ b/src/Application/Services/DomainService/TransactionService.cs @@ -48,4 +48,11 @@ public async Task>> GetAllTransactionsAsync() return Result>.Fail($"An error occurred: {ex.Message}"); } } + + public async Task> GetTransactionsByAccountIdAsync(long accountId) + { + var source = await _transactionRepository.GetBySourceAccountId(accountId); + var destination = await _transactionRepository.GetByDestinationAccountId(accountId); + return source.Concat(destination).ToList(); + } } \ No newline at end of file diff --git a/src/Infrastructure/Repositories/AccountRepository.cs b/src/Infrastructure/Repositories/AccountRepository.cs index 8941372..aae4a6e 100644 --- a/src/Infrastructure/Repositories/AccountRepository.cs +++ b/src/Infrastructure/Repositories/AccountRepository.cs @@ -25,20 +25,6 @@ public async Task CreateBulkAsync(List accounts) return await _dbContext.Accounts.FindAsync(accountId); } - public async Task> GetTransactionsByAccountId(long accountId) - { - var account = await _dbContext.Accounts - .Include(a => a.SourceTransactions) - .FirstOrDefaultAsync(a => a.AccountId == accountId); - - if (account == null) - { - return new List(); - } - - return account.SourceTransactions.ToList(); - } - public async Task> GetAllAccounts() { return await _dbContext.Accounts.ToListAsync(); diff --git a/src/Infrastructure/Repositories/TransactionRepository.cs b/src/Infrastructure/Repositories/TransactionRepository.cs index 788843b..7b7a774 100644 --- a/src/Infrastructure/Repositories/TransactionRepository.cs +++ b/src/Infrastructure/Repositories/TransactionRepository.cs @@ -23,4 +23,18 @@ public async Task> GetAllTransactions() { return await _dbContext.Transactions.ToListAsync(); } + + public Task> GetBySourceAccountId(long accountId) + { + return _dbContext.Transactions + .Where(transaction => transaction.SourceAccountId == accountId) + .ToListAsync(); + } + + public Task> GetByDestinationAccountId(long accountId) + { + return _dbContext.Transactions + .Where(transaction => transaction.DestinationAccountId == accountId) + .ToListAsync(); + } } \ No newline at end of file diff --git a/src/Web/Controllers/AccountController.cs b/src/Web/Controllers/AccountController.cs index 3bf0f82..167cdcc 100644 --- a/src/Web/Controllers/AccountController.cs +++ b/src/Web/Controllers/AccountController.cs @@ -62,24 +62,6 @@ public async Task GetAccountById(long accountId) return Ok(account.ToAccountDto()); } - [HttpGet("{accountId}")] - [Authorize] - [RequiresAnyRole(Claims.Role, AppRoles.Admin, AppRoles.DataAdmin, AppRoles.DataAnalyst)] - [ProducesResponseType(200)] - [ProducesResponseType(404)] - public async Task GetTransactionsByUserId(long accountId) - { - var result = await _accountService.GetTransactionsByUserId(accountId); - if (!result.Succeed) - { - return NotFound("User do not exist"); - } - - var transactions = result.Value; - - return Ok(transactions!.Select(t => t.ToTransactionDto())); - } - [HttpGet] [Authorize] [RequiresAnyRole(Claims.Role, AppRoles.Admin, AppRoles.DataAdmin, AppRoles.DataAnalyst)] diff --git a/src/Web/Controllers/TransactionController.cs b/src/Web/Controllers/TransactionController.cs index e8211c3..7055010 100644 --- a/src/Web/Controllers/TransactionController.cs +++ b/src/Web/Controllers/TransactionController.cs @@ -66,4 +66,16 @@ public async Task GetAllTransactions() var response = allTransactions.Value!; return Ok(response.ToGotAllTransactionsDto()); } + + [HttpGet("{accountId}")] + [Authorize] + [RequiresAnyRole(Claims.Role, AppRoles.Admin, AppRoles.DataAdmin, AppRoles.DataAnalyst)] + [ProducesResponseType(200)] + [ProducesResponseType(401)] + [ProducesResponseType(403)] + public async Task GetTransactionsByAccountId(long accountId) + { + var transactions = await _transactionService.GetTransactionsByAccountIdAsync(accountId); + return Ok(transactions); + } } \ No newline at end of file