diff --git a/src/Application/DTOs/AccountCsv/GetAllAccountsResponse.cs b/src/Application/DTOs/AccountCsv/GetAllAccountsResponse.cs new file mode 100644 index 0000000..8594868 --- /dev/null +++ b/src/Application/DTOs/AccountCsv/GetAllAccountsResponse.cs @@ -0,0 +1,6 @@ +namespace Application.DTOs.AccountCsv; + +public class GetAllAccountsResponse +{ + public List Accounts { get; set; } = new(); +} \ No newline at end of file diff --git a/src/Application/DTOs/TransactionCsv/GetAllTransactionsResponse.cs b/src/Application/DTOs/TransactionCsv/GetAllTransactionsResponse.cs index 80f5631..005cf3c 100644 --- a/src/Application/DTOs/TransactionCsv/GetAllTransactionsResponse.cs +++ b/src/Application/DTOs/TransactionCsv/GetAllTransactionsResponse.cs @@ -1,17 +1,6 @@ -namespace Web.DTOs.Transaction; +namespace Application.DTOs.TransactionCsv; public class GetAllTransactionsResponse { - public List 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; - } - + public List Transactions { get; set; } = new(); } \ No newline at end of file diff --git a/src/Application/Interfaces/IAccountRepository.cs b/src/Application/Interfaces/IAccountRepository.cs index 19cc5ba..3ba7dc5 100644 --- a/src/Application/Interfaces/IAccountRepository.cs +++ b/src/Application/Interfaces/IAccountRepository.cs @@ -7,4 +7,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/Services/IAccountService.cs b/src/Application/Interfaces/Services/IAccountService.cs index 6e41d4c..0a3c7b4 100644 --- a/src/Application/Interfaces/Services/IAccountService.cs +++ b/src/Application/Interfaces/Services/IAccountService.cs @@ -1,4 +1,5 @@ using Application.DTOs; +using Application.DTOs.AccountCsv; using Domain.Entities; namespace Application.Interfaces.Services; @@ -8,4 +9,5 @@ public interface IAccountService Task AddAccountsFromCsvAsync(string filePath); Task GetAccountByIdAsync(long accountId); Task>> GetTransactionsByUserId(long accountId); + Task> GetAllAccounts(); } \ No newline at end of file diff --git a/src/Application/Interfaces/Services/ITransactionService.cs b/src/Application/Interfaces/Services/ITransactionService.cs index cb5ffaa..cfec67c 100644 --- a/src/Application/Interfaces/Services/ITransactionService.cs +++ b/src/Application/Interfaces/Services/ITransactionService.cs @@ -1,5 +1,5 @@ using Application.DTOs; -using Web.DTOs.Transaction; +using Application.DTOs.TransactionCsv; namespace Application.Interfaces.Services; diff --git a/src/Application/Mappers/AccountMapper.cs b/src/Application/Mappers/AccountMapper.cs new file mode 100644 index 0000000..f8188b1 --- /dev/null +++ b/src/Application/Mappers/AccountMapper.cs @@ -0,0 +1,27 @@ +using Application.DTOs.AccountCsv; +using Domain.Entities; + +namespace Application.Mappers; + +public static class AccountMapper +{ + public static GetAllAccountsResponse ToGetAllAccountsResponse(this List 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() + }; + } +} \ No newline at end of file diff --git a/src/Application/Mappers/TransactionMapper.cs b/src/Application/Mappers/TransactionMapper.cs index f9d0c22..3c3d2c0 100644 --- a/src/Application/Mappers/TransactionMapper.cs +++ b/src/Application/Mappers/TransactionMapper.cs @@ -1,24 +1,22 @@ -using Web.DTOs.Transaction; +using Application.DTOs.TransactionCsv; using Domain.Entities; -namespace Application.Mappers +namespace Application.Mappers; +public static class TransactionMapper { - public static class TransactionMapper + public static GetAllTransactionsResponse ToGetAllTransactionsResponse(this List transactions) { - public static GetAllTransactionsResponse ToGetAllTransactionsResponse(this List transactions) + return new GetAllTransactionsResponse { - return new GetAllTransactionsResponse + Transactions = transactions.Select(transaction => new TransactionCsvModel { - 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() - }; - } + TransactionID = transaction.TransactionId, + SourceAcount = transaction.SourceAccountId, + DestiantionAccount = transaction.DestinationAccountId, + Amount = transaction.Amount, + Date = transaction.Date, + Type = transaction.Type + }).ToList() + }; } } \ No newline at end of file diff --git a/src/Application/Services/AccountService.cs b/src/Application/Services/AccountService.cs index 27f70f8..75017c6 100644 --- a/src/Application/Services/AccountService.cs +++ b/src/Application/Services/AccountService.cs @@ -2,6 +2,7 @@ using Application.DTOs.AccountCsv; using Application.Interfaces; using Application.Interfaces.Services; +using Application.Mappers; using Application.Services.SharedService; using Domain.Entities; @@ -54,4 +55,23 @@ public async Task>> GetTransactionsByUserId(long accoun var transactions = await _accountRepository.GetTransactionsByAccountId(accountId); return Result>.Ok(transactions); } + + public async Task> GetAllAccounts() + { + try + { + var accounts = await _accountRepository.GetAllAccounts(); + + if (accounts.Count == 0) + { + return Result.Fail("No Accounts found"); + } + var response = accounts.ToGetAllAccountsResponse(); + return Result.Ok(response); + } + catch (Exception ex) + { + return Result.Fail($"An error occurred: {ex.Message}"); + } + } } \ No newline at end of file diff --git a/src/Application/Services/TransactionService.cs b/src/Application/Services/TransactionService.cs index 5b5b9af..f3c6089 100644 --- a/src/Application/Services/TransactionService.cs +++ b/src/Application/Services/TransactionService.cs @@ -5,7 +5,7 @@ using Application.Mappers; using Application.Services.SharedService; using Domain.Entities; -using Web.DTOs.Transaction; +using Application.DTOs.TransactionCsv; namespace Application.Services; diff --git a/src/Infrastructure/Services/AccountRepository.cs b/src/Infrastructure/Services/AccountRepository.cs index f561501..0056b98 100644 --- a/src/Infrastructure/Services/AccountRepository.cs +++ b/src/Infrastructure/Services/AccountRepository.cs @@ -38,4 +38,9 @@ public async Task> GetTransactionsByAccountId(long accountId) return account.SourceTransactions.ToList(); } + + public async Task> GetAllAccounts() + { + return await _dbContext.Accounts.ToListAsync(); + } } \ No newline at end of file diff --git a/src/Web/Controllers/AccountController.cs b/src/Web/Controllers/AccountController.cs index 72b109d..98e8872 100644 --- a/src/Web/Controllers/AccountController.cs +++ b/src/Web/Controllers/AccountController.cs @@ -1,5 +1,7 @@ using Application.Interfaces.Services; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using Web.Helper; using Web.Mappers; namespace Web.Controllers; @@ -58,4 +60,18 @@ public async Task GetTransactionsByUserId(long accountId) return Ok(transactions!.Select(t => t.ToTransactionDto())); } + + [HttpGet] + [Authorize] + public async Task GetAllAccounts() + { + var allAccounts = await _accountService.GetAllAccounts(); + if (!allAccounts.Succeed) + { + return BadRequest(Errors.New(nameof(GetAllAccounts), allAccounts.Message)); + } + + var response = allAccounts.Value!; + return Ok(response.ToGotAllAccountsDto()); + } } \ No newline at end of file diff --git a/src/Web/Mappers/AccountMapper.cs b/src/Web/Mappers/AccountMapper.cs index efcf367..c8c3ca0 100644 --- a/src/Web/Mappers/AccountMapper.cs +++ b/src/Web/Mappers/AccountMapper.cs @@ -1,4 +1,5 @@ -using Domain.Entities; +using Application.DTOs.AccountCsv; +using Domain.Entities; using Web.DTOs.Account; namespace Web.Mappers; @@ -21,4 +22,21 @@ public static AccountDto ToAccountDto(this Account account) OwnerId = account.OwnerId }; } + + public static List ToGotAllAccountsDto(this GetAllAccountsResponse response) + { + return response.Accounts.Select(account => new AccountDto + { + AccountId = account.AccountID, + CardId = account.CardID, + Iban = account.IBAN, + AccountType = account.AccountType, + BranchTelephone = account.BranchTelephone, + BranchAddress = account.BranchAdress, + BranchName = account.BranchName, + OwnerName = account.OwnerName, + OwnerLastName = account.OwnerLastName, + OwnerId = account.OwnerID + }).ToList(); + } } \ No newline at end of file diff --git a/src/Web/Mappers/TransactionMapper.cs b/src/Web/Mappers/TransactionMapper.cs index 9fae733..c6341b1 100644 --- a/src/Web/Mappers/TransactionMapper.cs +++ b/src/Web/Mappers/TransactionMapper.cs @@ -1,3 +1,4 @@ +using Application.DTOs.TransactionCsv; using Domain.Entities; using Web.DTOs.Transaction; @@ -14,20 +15,20 @@ public static TransactionDto ToTransactionDto(this Transaction transaction) DestinationAccountId = transaction.DestinationAccountId, Amount = transaction.Amount, Date = transaction.Date, - Type = transaction.Type + Type = transaction.Type }; } - public static List ToGotAllTransactionsDto(this GetAllTransactionsResponse response) + public static List ToGotAllTransactionsDto(this GetAllTransactionsResponse response) + { + return response.Transactions.Select(transaction => new TransactionDto { - return response.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(); - } + TransactionId = transaction.TransactionID, + SourceAccountId = transaction.SourceAcount, + DestinationAccountId = transaction.DestiantionAccount, + Amount = transaction.Amount, + Date = transaction.Date, + Type = transaction.Type + }).ToList(); + } } \ No newline at end of file