Skip to content

Commit

Permalink
feature: add expansion api logic
Browse files Browse the repository at this point in the history
  • Loading branch information
mobinbr committed Aug 27, 2024
1 parent 00839a1 commit 0b769c6
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Application.DTOs.Transaction;

public class GetTransactionsByAccountIdResponse
{
public long AccountId { get; set; }
public List<TransactionCsvModel> TransactionWithSources = new();
}
3 changes: 2 additions & 1 deletion src/Application/Interfaces/Services/ITransactionService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Application.DTOs;
using Application.DTOs.Transaction;
using Domain.Entities;

namespace Application.Interfaces.Services;
Expand All @@ -7,5 +8,5 @@ public interface ITransactionService
{
Task<Result> AddTransactionsFromCsvAsync(string filePath);
Task<Result<List<Transaction>>> GetAllTransactionsAsync();
Task<List<Transaction>> GetTransactionsByAccountIdAsync(long accountId);
Task<Result<List<GetTransactionsByAccountIdResponse>>> GetTransactionsByAccountIdAsync(long accountId);
}
44 changes: 40 additions & 4 deletions src/Application/Services/DomainService/TransactionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,46 @@ public async Task<Result<List<Transaction>>> GetAllTransactionsAsync()
}
}

public async Task<List<Transaction>> GetTransactionsByAccountIdAsync(long accountId)
public async Task<Result<List<GetTransactionsByAccountIdResponse>>> GetTransactionsByAccountIdAsync(long accountId)
{
var source = await _transactionRepository.GetBySourceAccountId(accountId);
var destination = await _transactionRepository.GetByDestinationAccountId(accountId);
return source.Concat(destination).ToList();
try
{
var result = new List<GetTransactionsByAccountIdResponse>();

var transactionsSourceAccountId = await _transactionRepository.GetBySourceAccountId(accountId);

var transactionsDestinationAccountId = await _transactionRepository.GetByDestinationAccountId(accountId);

var allTransactions = transactionsSourceAccountId.Concat(transactionsDestinationAccountId).ToList();

var groupedTransactions = allTransactions
.GroupBy(t => t.SourceAccountId == accountId ? t.DestinationAccountId : t.SourceAccountId)
.ToList();

foreach (var group in groupedTransactions)
{
var response = new GetTransactionsByAccountIdResponse
{
AccountId = group.Key,
TransactionWithSources = group.Select(t => new TransactionCsvModel
{
TransactionID = t.TransactionId,
SourceAcount = t.SourceAccountId,
DestiantionAccount = t.DestinationAccountId,
Amount = t.Amount,
Date = t.Date,
Type = t.Type,
}).ToList()
};

result.Add(response);
}

return Result<List<GetTransactionsByAccountIdResponse>>.Ok(result);
}
catch (Exception ex)
{
return Result<List<GetTransactionsByAccountIdResponse>>.Fail($"An error occurred: {ex.Message}");
}
}
}
10 changes: 9 additions & 1 deletion src/Web/Controllers/TransactionController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ public async Task<IActionResult> GetAllTransactions()
public async Task<IActionResult> GetTransactionsByAccountId(long accountId)
{
var transactions = await _transactionService.GetTransactionsByAccountIdAsync(accountId);
return Ok(transactions.ToGotAllTransactionsDto());

if (!transactions.Succeed)
{
return BadRequest(Errors.New(nameof(GetAllTransactions), transactions.Message));
}

var response = transactions.Value!;

return Ok(response);
}
}

0 comments on commit 0b769c6

Please sign in to comment.