-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from Star-Academy/feature/get-all-accounts
Feature/get all accounts
- Loading branch information
Showing
13 changed files
with
416 additions
and
333 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
21
src/Application/DTOs/TransactionCsv/GetAllTransactionsResponse.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"); | ||
} | ||
} | ||
} |
Oops, something went wrong.