Skip to content

Commit

Permalink
refactor: move get-transactions-by-account-id to transactions controller
Browse files Browse the repository at this point in the history
  • Loading branch information
amiralirahimii committed Aug 20, 2024
1 parent 50fbf27 commit 0337ad4
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ public interface IAccountRepository
{
Task CreateBulkAsync(List<Account> accounts);
Task<Account?> GetByIdAsync(long accountId);
Task<List<Transaction>> GetTransactionsByAccountId(long accountId);
Task<List<Account>> GetAllAccounts();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ public interface ITransactionRepository
{
Task CreateBulkAsync(List<Transaction> transactions);
Task<List<Transaction>> GetAllTransactions();
Task<List<Transaction>> GetBySourceAccountId(long accountId);
Task<List<Transaction>> GetByDestinationAccountId(long accountId);
}
1 change: 0 additions & 1 deletion src/Application/Interfaces/Services/IAccountService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ public interface IAccountService
{
Task<Result> AddAccountsFromCsvAsync(string filePath);
Task<Account?> GetAccountByIdAsync(long accountId);
Task<Result<List<Transaction>>> GetTransactionsByUserId(long accountId);
Task<Result<List<Account>>> GetAllAccountsAsync();
}
1 change: 1 addition & 0 deletions src/Application/Interfaces/Services/ITransactionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ public interface ITransactionService
{
Task<Result> AddTransactionsFromCsvAsync(string filePath);
Task<Result<List<Transaction>>> GetAllTransactionsAsync();
Task<List<Transaction>> GetTransactionsByAccountIdAsync(long accountId);
}
13 changes: 0 additions & 13 deletions src/Application/Services/DomainService/AccountService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,6 @@ public async Task<Result> AddAccountsFromCsvAsync(string filePath)
{
return await _accountRepository.GetByIdAsync(accountId);
}

public async Task<Result<List<Transaction>>> GetTransactionsByUserId(long accountId)
{
var account = await _accountRepository.GetByIdAsync(accountId);

if (account == null)
{
return Result<List<Transaction>>.Fail("Account not found");
}

var transactions = await _accountRepository.GetTransactionsByAccountId(accountId);
return Result<List<Transaction>>.Ok(transactions);
}

public async Task<Result<List<Account>>> GetAllAccountsAsync()
{
Expand Down
7 changes: 7 additions & 0 deletions src/Application/Services/DomainService/TransactionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,11 @@ public async Task<Result<List<Transaction>>> GetAllTransactionsAsync()
return Result<List<Transaction>>.Fail($"An error occurred: {ex.Message}");
}
}

public async Task<List<Transaction>> GetTransactionsByAccountIdAsync(long accountId)
{
var source = await _transactionRepository.GetBySourceAccountId(accountId);
var destination = await _transactionRepository.GetByDestinationAccountId(accountId);
return source.Concat(destination).ToList();
}
}
14 changes: 0 additions & 14 deletions src/Infrastructure/Repositories/AccountRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,6 @@ public async Task CreateBulkAsync(List<Account> accounts)
return await _dbContext.Accounts.FindAsync(accountId);
}

public async Task<List<Transaction>> GetTransactionsByAccountId(long accountId)
{
var account = await _dbContext.Accounts
.Include(a => a.SourceTransactions)
.FirstOrDefaultAsync(a => a.AccountId == accountId);

if (account == null)
{
return new List<Transaction>();
}

return account.SourceTransactions.ToList();
}

public async Task<List<Account>> GetAllAccounts()
{
return await _dbContext.Accounts.ToListAsync();
Expand Down
14 changes: 14 additions & 0 deletions src/Infrastructure/Repositories/TransactionRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,18 @@ public async Task<List<Transaction>> GetAllTransactions()
{
return await _dbContext.Transactions.ToListAsync();
}

public Task<List<Transaction>> GetBySourceAccountId(long accountId)
{
return _dbContext.Transactions
.Where(transaction => transaction.SourceAccountId == accountId)
.ToListAsync();
}

public Task<List<Transaction>> GetByDestinationAccountId(long accountId)
{
return _dbContext.Transactions
.Where(transaction => transaction.DestinationAccountId == accountId)
.ToListAsync();
}
}
18 changes: 0 additions & 18 deletions src/Web/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,6 @@ public async Task<IActionResult> 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<IActionResult> 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)]
Expand Down
12 changes: 12 additions & 0 deletions src/Web/Controllers/TransactionController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,16 @@ public async Task<IActionResult> 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<IActionResult> GetTransactionsByAccountId(long accountId)
{
var transactions = await _transactionService.GetTransactionsByAccountIdAsync(accountId);
return Ok(transactions);
}
}

0 comments on commit 0337ad4

Please sign in to comment.