Skip to content

Commit

Permalink
Merge pull request #12 from Star-Academy/feature/get-all-accounts
Browse files Browse the repository at this point in the history
Feature/get all accounts
  • Loading branch information
mobinbr authored Aug 19, 2024
2 parents 8e30cb7 + bcc1c34 commit 3cd7807
Show file tree
Hide file tree
Showing 13 changed files with 416 additions and 333 deletions.
6 changes: 6 additions & 0 deletions src/Application/DTOs/AccountCsv/GetAllAccountsResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Application.DTOs.AccountCsv;

public class GetAllAccountsResponse
{
public List<AccountCsvModel> Accounts { get; set; } = new();
}
21 changes: 5 additions & 16 deletions src/Application/DTOs/TransactionCsv/GetAllTransactionsResponse.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
namespace Web.DTOs.Transaction;

public class GetAllTransactionsResponse
{
public List<TransactionDto> Transactions { get; set; } = new();

public class TransactionDto
{
public long TransactionId { get; set; }
public long SourceAccountId { get; set; }
public long DestinationAccountId { get; set; }
public decimal Amount { get; set; }
public DateTime Date { get; set; }
public string Type { get; set; } = string.Empty;
}

namespace Application.DTOs.TransactionCsv;

public class GetAllTransactionsResponse
{
public List<TransactionCsvModel> Transactions { get; set; } = new();
}
19 changes: 10 additions & 9 deletions src/Application/Interfaces/IAccountRepository.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using Domain.Entities;

namespace Application.Interfaces;

public interface IAccountRepository
{
Task CreateBulkAsync(List<Account> accounts);
Task<Account?> GetByIdAsync(long accountId);
Task<List<Transaction>> GetTransactionsByAccountId(long accountId);
using Domain.Entities;

namespace Application.Interfaces;

public interface IAccountRepository
{
Task CreateBulkAsync(List<Account> accounts);
Task<Account?> GetByIdAsync(long accountId);
Task<List<Transaction>> GetTransactionsByAccountId(long accountId);
Task<List<Account>> GetAllAccounts();
}
22 changes: 12 additions & 10 deletions src/Application/Interfaces/Services/IAccountService.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using Application.DTOs;
using Domain.Entities;

namespace Application.Interfaces.Services;

public interface IAccountService
{
Task AddAccountsFromCsvAsync(string filePath);
Task<Account?> GetAccountByIdAsync(long accountId);
Task<Result<List<Transaction>>> GetTransactionsByUserId(long accountId);
using Application.DTOs;
using Application.DTOs.AccountCsv;
using Domain.Entities;

namespace Application.Interfaces.Services;

public interface IAccountService
{
Task AddAccountsFromCsvAsync(string filePath);
Task<Account?> GetAccountByIdAsync(long accountId);
Task<Result<List<Transaction>>> GetTransactionsByUserId(long accountId);
Task<Result<GetAllAccountsResponse>> GetAllAccounts();
}
18 changes: 9 additions & 9 deletions src/Application/Interfaces/Services/ITransactionService.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using Application.DTOs;
using Web.DTOs.Transaction;

namespace Application.Interfaces.Services;

public interface ITransactionService
{
Task AddTransactionsFromCsvAsync(string filePath);
Task<Result<GetAllTransactionsResponse>> GetAllTransactions();
using Application.DTOs;
using Application.DTOs.TransactionCsv;

namespace Application.Interfaces.Services;

public interface ITransactionService
{
Task AddTransactionsFromCsvAsync(string filePath);
Task<Result<GetAllTransactionsResponse>> GetAllTransactions();
}
27 changes: 27 additions & 0 deletions src/Application/Mappers/AccountMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Application.DTOs.AccountCsv;
using Domain.Entities;

namespace Application.Mappers;

public static class AccountMapper
{
public static GetAllAccountsResponse ToGetAllAccountsResponse(this List<Account> accounts)
{
return new GetAllAccountsResponse
{
Accounts = accounts.Select(account => new AccountCsvModel
{
AccountID = account.AccountId,
CardID = account.CardId,
IBAN = account.Iban,
AccountType = account.AccountType,
BranchTelephone = account.BranchTelephone,
BranchAdress = account.BranchAddress,
BranchName = account.BranchName,
OwnerName = account.OwnerName,
OwnerLastName = account.OwnerLastName,
OwnerID = account.OwnerId
}).ToList()
};
}
}
44 changes: 21 additions & 23 deletions src/Application/Mappers/TransactionMapper.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
using Web.DTOs.Transaction;
using Domain.Entities;

namespace Application.Mappers
{
public static class TransactionMapper
{
public static GetAllTransactionsResponse ToGetAllTransactionsResponse(this List<Transaction> transactions)
{
return new GetAllTransactionsResponse
{
Transactions = transactions.Select(transaction => new GetAllTransactionsResponse.TransactionDto
{
TransactionId = transaction.TransactionId,
SourceAccountId = transaction.SourceAccountId,
DestinationAccountId = transaction.DestinationAccountId,
Amount = transaction.Amount,
Date = transaction.Date,
Type = transaction.Type
}).ToList()
};
}
}
using Application.DTOs.TransactionCsv;
using Domain.Entities;

namespace Application.Mappers;
public static class TransactionMapper
{
public static GetAllTransactionsResponse ToGetAllTransactionsResponse(this List<Transaction> transactions)
{
return new GetAllTransactionsResponse
{
Transactions = transactions.Select(transaction => new TransactionCsvModel
{
TransactionID = transaction.TransactionId,
SourceAcount = transaction.SourceAccountId,
DestiantionAccount = transaction.DestinationAccountId,
Amount = transaction.Amount,
Date = transaction.Date,
Type = transaction.Type
}).ToList()
};
}
}
132 changes: 76 additions & 56 deletions src/Application/Services/AccountService.cs
Original file line number Diff line number Diff line change
@@ -1,57 +1,77 @@
using Application.DTOs;
using Application.DTOs.AccountCsv;
using Application.Interfaces;
using Application.Interfaces.Services;
using Application.Services.SharedService;
using Domain.Entities;

namespace Application.Services;

public class AccountService : IAccountService
{
private readonly IAccountRepository _accountRepository;

public AccountService(IAccountRepository accountRepository)
{
_accountRepository = accountRepository;
}

public async Task AddAccountsFromCsvAsync(string filePath)
{
var accountCsvModels = CsvReaderService.ReadFromCsv<AccountCsvModel>(filePath);

var accounts = accountCsvModels.Select(csvModel => new Account
{
AccountId = csvModel.AccountID,
CardId = csvModel.CardID,
Iban = csvModel.IBAN,
AccountType = csvModel.AccountType,
BranchTelephone = csvModel.BranchTelephone,
BranchAddress = csvModel.BranchAdress,
BranchName = csvModel.BranchName,
OwnerName = csvModel.OwnerName,
OwnerLastName = csvModel.OwnerLastName,
OwnerId = csvModel.OwnerID
}).ToList();

await _accountRepository.CreateBulkAsync(accounts);
}

public async Task<Account?> GetAccountByIdAsync(long accountId)
{
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);
}
using Application.DTOs;
using Application.DTOs.AccountCsv;
using Application.Interfaces;
using Application.Interfaces.Services;
using Application.Mappers;
using Application.Services.SharedService;
using Domain.Entities;

namespace Application.Services;

public class AccountService : IAccountService
{
private readonly IAccountRepository _accountRepository;

public AccountService(IAccountRepository accountRepository)
{
_accountRepository = accountRepository;
}

public async Task AddAccountsFromCsvAsync(string filePath)
{
var accountCsvModels = CsvReaderService.ReadFromCsv<AccountCsvModel>(filePath);

var accounts = accountCsvModels.Select(csvModel => new Account
{
AccountId = csvModel.AccountID,
CardId = csvModel.CardID,
Iban = csvModel.IBAN,
AccountType = csvModel.AccountType,
BranchTelephone = csvModel.BranchTelephone,
BranchAddress = csvModel.BranchAdress,
BranchName = csvModel.BranchName,
OwnerName = csvModel.OwnerName,
OwnerLastName = csvModel.OwnerLastName,
OwnerId = csvModel.OwnerID
}).ToList();

await _accountRepository.CreateBulkAsync(accounts);
}

public async Task<Account?> GetAccountByIdAsync(long accountId)
{
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<GetAllAccountsResponse>> GetAllAccounts()
{
try
{
var accounts = await _accountRepository.GetAllAccounts();

if (accounts.Count == 0)
{
return Result<GetAllAccountsResponse>.Fail("No Accounts found");
}
var response = accounts.ToGetAllAccountsResponse();
return Result<GetAllAccountsResponse>.Ok(response);
}
catch (Exception ex)
{
return Result<GetAllAccountsResponse>.Fail($"An error occurred: {ex.Message}");
}
}
}
Loading

0 comments on commit 3cd7807

Please sign in to comment.