From dc5f4f6e0d0d73de5de69a0ebf375f1cfb89c61c Mon Sep 17 00:00:00 2001 From: Mobin Barfi Date: Mon, 19 Aug 2024 15:53:10 +0330 Subject: [PATCH 1/2] feat: add get-all-accounts api --- .../DTOs/AccountCsv/GetAllAccountsResponse.cs | 6 ++++ .../GetAllTransactionsResponse.cs | 15 ++-------- .../Interfaces/IAccountRepository.cs | 1 + .../Interfaces/Services/IAccountService.cs | 2 ++ .../Services/ITransactionService.cs | 2 +- src/Application/Mappers/AccountMapper.cs | 27 +++++++++++++++++ src/Application/Mappers/TransactionMapper.cs | 30 +++++++++---------- src/Application/Services/AccountService.cs | 20 +++++++++++++ .../Services/TransactionService.cs | 2 +- .../Services/AccountRepository.cs | 5 ++++ src/Web/Controllers/AccountController.cs | 16 ++++++++++ src/Web/Mappers/AccountMapper.cs | 20 ++++++++++++- src/Web/Mappers/TransactionMapper.cs | 25 ++++++++-------- 13 files changed, 127 insertions(+), 44 deletions(-) create mode 100644 src/Application/DTOs/AccountCsv/GetAllAccountsResponse.cs create mode 100644 src/Application/Mappers/AccountMapper.cs 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 From f6c9c6b0f3b6876059519a259fdd940a9f2c6cfd Mon Sep 17 00:00:00 2001 From: Mobin Barfi Date: Mon, 19 Aug 2024 16:15:20 +0330 Subject: [PATCH 2/2] fix: fix merge conflicts for get-all-accounts branch into dev --- .../DTOs/AccountCsv/GetAllAccountsResponse.cs | 6 + .../GetAllTransactionsResponse.cs | 15 +- .../Interfaces/IAccountRepository.cs | 19 +-- .../Interfaces/Services/IAccountService.cs | 22 +-- .../Services/ITransactionService.cs | 18 +-- src/Application/Mappers/AccountMapper.cs | 27 ++++ src/Application/Mappers/TransactionMapper.cs | 44 +++--- src/Application/Services/AccountService.cs | 132 +++++++++-------- .../Services/TransactionService.cs | 110 +++++++------- .../Services/AccountRepository.cs | 85 +++++------ src/Web/Controllers/AccountController.cs | 136 ++++++++++-------- src/Web/Mappers/AccountMapper.cs | 64 ++++++--- src/Web/Mappers/TransactionMapper.cs | 65 ++++----- 13 files changed, 413 insertions(+), 330 deletions(-) create mode 100644 src/Application/DTOs/AccountCsv/GetAllAccountsResponse.cs create mode 100644 src/Application/Mappers/AccountMapper.cs 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 5c5ba49..3fac770 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 88ca7ad..11ccc7c 100644 --- a/src/Application/Interfaces/IAccountRepository.cs +++ b/src/Application/Interfaces/IAccountRepository.cs @@ -1,10 +1,11 @@ -using Domain.Entities; - -namespace Application.Interfaces; - -public interface IAccountRepository -{ - Task CreateBulkAsync(List accounts); - Task GetByIdAsync(long accountId); - Task> GetTransactionsByAccountId(long accountId); +using Domain.Entities; + +namespace Application.Interfaces; + +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 9a87aeb..3b465b0 100644 --- a/src/Application/Interfaces/Services/IAccountService.cs +++ b/src/Application/Interfaces/Services/IAccountService.cs @@ -1,11 +1,13 @@ -using Application.DTOs; -using Domain.Entities; - -namespace Application.Interfaces.Services; - -public interface IAccountService -{ - Task AddAccountsFromCsvAsync(string filePath); - Task GetAccountByIdAsync(long accountId); - Task>> 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 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 5d5cd4c..3b453dd 100644 --- a/src/Application/Interfaces/Services/ITransactionService.cs +++ b/src/Application/Interfaces/Services/ITransactionService.cs @@ -1,10 +1,10 @@ -using Application.DTOs; -using Web.DTOs.Transaction; - -namespace Application.Interfaces.Services; - -public interface ITransactionService -{ - Task AddTransactionsFromCsvAsync(string filePath); - Task> GetAllTransactions(); +using Application.DTOs; +using Application.DTOs.TransactionCsv; + +namespace Application.Interfaces.Services; + +public interface ITransactionService +{ + Task AddTransactionsFromCsvAsync(string filePath); + Task> GetAllTransactions(); } \ No newline at end of file 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 0a27a6d..4cd65a0 100644 --- a/src/Application/Mappers/TransactionMapper.cs +++ b/src/Application/Mappers/TransactionMapper.cs @@ -1,24 +1,22 @@ -using Web.DTOs.Transaction; -using Domain.Entities; - -namespace Application.Mappers -{ - public static class TransactionMapper - { - public static GetAllTransactionsResponse ToGetAllTransactionsResponse(this List 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 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() + }; + } } \ No newline at end of file diff --git a/src/Application/Services/AccountService.cs b/src/Application/Services/AccountService.cs index c75e89c..201c3ac 100644 --- a/src/Application/Services/AccountService.cs +++ b/src/Application/Services/AccountService.cs @@ -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(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 GetAccountByIdAsync(long accountId) - { - return await _accountRepository.GetByIdAsync(accountId); - } - - public async Task>> GetTransactionsByUserId(long accountId) - { - var account = await _accountRepository.GetByIdAsync(accountId); - - if (account == null) - { - return Result>.Fail("Account not found"); - } - - var transactions = await _accountRepository.GetTransactionsByAccountId(accountId); - return Result>.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(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 GetAccountByIdAsync(long accountId) + { + return await _accountRepository.GetByIdAsync(accountId); + } + + public async Task>> GetTransactionsByUserId(long accountId) + { + var account = await _accountRepository.GetByIdAsync(accountId); + + if (account == null) + { + return Result>.Fail("Account not found"); + } + + 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 378e859..927caed 100644 --- a/src/Application/Services/TransactionService.cs +++ b/src/Application/Services/TransactionService.cs @@ -1,56 +1,56 @@ -using Application.DTOs; -using Application.DTOs.TransactionCsv; -using Application.Interfaces; -using Application.Interfaces.Services; -using Application.Mappers; -using Application.Services.SharedService; -using Domain.Entities; -using Web.DTOs.Transaction; - -namespace Application.Services; - -public class TransactionService : ITransactionService -{ - private readonly ITransactionRepository _transactionRepository; - - public TransactionService(ITransactionRepository transactionRepository) - { - _transactionRepository = transactionRepository; - } - - public async Task AddTransactionsFromCsvAsync(string filePath) - { - var transactionCsvModels = CsvReaderService.ReadFromCsv(filePath); - - var transactions = transactionCsvModels.Select(csvModel => new Transaction - { - TransactionId = csvModel.TransactionID, - SourceAccountId = csvModel.SourceAcount, - DestinationAccountId = csvModel.DestiantionAccount, - Amount = csvModel.Amount, - Date = csvModel.Date, - Type = csvModel.Type - }).ToList(); - - await _transactionRepository.CreateBulkAsync(transactions); - } - - public async Task> GetAllTransactions() - { - try - { - var transactions = await _transactionRepository.GetAllTransactions(); - - if (transactions.Count == 0) - { - return Result.Fail("No transactions found"); - } - var response = transactions.ToGetAllTransactionsResponse(); - return Result.Ok(response); - } - catch (Exception ex) - { - return Result.Fail($"An error occurred: {ex.Message}"); - } - } +using Application.DTOs; +using Application.DTOs.TransactionCsv; +using Application.Interfaces; +using Application.Interfaces.Services; +using Application.Mappers; +using Application.Services.SharedService; +using Domain.Entities; +using Application.DTOs.TransactionCsv; + +namespace Application.Services; + +public class TransactionService : ITransactionService +{ + private readonly ITransactionRepository _transactionRepository; + + public TransactionService(ITransactionRepository transactionRepository) + { + _transactionRepository = transactionRepository; + } + + public async Task AddTransactionsFromCsvAsync(string filePath) + { + var transactionCsvModels = CsvReaderService.ReadFromCsv(filePath); + + var transactions = transactionCsvModels.Select(csvModel => new Transaction + { + TransactionId = csvModel.TransactionID, + SourceAccountId = csvModel.SourceAcount, + DestinationAccountId = csvModel.DestiantionAccount, + Amount = csvModel.Amount, + Date = csvModel.Date, + Type = csvModel.Type + }).ToList(); + + await _transactionRepository.CreateBulkAsync(transactions); + } + + public async Task> GetAllTransactions() + { + try + { + var transactions = await _transactionRepository.GetAllTransactions(); + + if (transactions.Count == 0) + { + return Result.Fail("No transactions found"); + } + var response = transactions.ToGetAllTransactionsResponse(); + 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/Infrastructure/Services/AccountRepository.cs b/src/Infrastructure/Services/AccountRepository.cs index 5ff80ce..613a674 100644 --- a/src/Infrastructure/Services/AccountRepository.cs +++ b/src/Infrastructure/Services/AccountRepository.cs @@ -1,41 +1,46 @@ -using Application.Interfaces; -using Domain.Entities; -using Infrastructure.Data; -using Microsoft.EntityFrameworkCore; - -namespace Infrastructure.Services; - -public class AccountRepository : IAccountRepository -{ - private readonly ApplicationDbContext _dbContext; - - public AccountRepository(ApplicationDbContext dbContext) - { - _dbContext = dbContext; - } - - public async Task CreateBulkAsync(List accounts) - { - await _dbContext.Accounts.AddRangeAsync(accounts); - await _dbContext.SaveChangesAsync(); - } - - public async Task GetByIdAsync(long accountId) - { - return await _dbContext.Accounts.FindAsync(accountId); - } - - public async Task> GetTransactionsByAccountId(long accountId) - { - var account = await _dbContext.Accounts - .Include(a => a.SourceTransactions) - .FirstOrDefaultAsync(a => a.AccountId == accountId); - - if (account == null) - { - return new List(); - } - - return account.SourceTransactions.ToList(); - } +using Application.Interfaces; +using Domain.Entities; +using Infrastructure.Data; +using Microsoft.EntityFrameworkCore; + +namespace Infrastructure.Services; + +public class AccountRepository : IAccountRepository +{ + private readonly ApplicationDbContext _dbContext; + + public AccountRepository(ApplicationDbContext dbContext) + { + _dbContext = dbContext; + } + + public async Task CreateBulkAsync(List accounts) + { + await _dbContext.Accounts.AddRangeAsync(accounts); + await _dbContext.SaveChangesAsync(); + } + + public async Task GetByIdAsync(long accountId) + { + return await _dbContext.Accounts.FindAsync(accountId); + } + + public async Task> GetTransactionsByAccountId(long accountId) + { + var account = await _dbContext.Accounts + .Include(a => a.SourceTransactions) + .FirstOrDefaultAsync(a => a.AccountId == accountId); + + if (account == null) + { + return new List(); + } + + 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 f833f02..92ec42f 100644 --- a/src/Web/Controllers/AccountController.cs +++ b/src/Web/Controllers/AccountController.cs @@ -1,61 +1,77 @@ -using Application.Interfaces.Services; -using Microsoft.AspNetCore.Mvc; -using Web.Mappers; - -namespace Web.Controllers; - -[ApiController] -[Route("[controller]/[action]")] -public class AccountController : ControllerBase -{ - private readonly IAccountService _accountService; - - public AccountController(IAccountService accountService) - { - _accountService = accountService; - } - - [HttpPost] - public async Task ImportAccounts([FromForm] IFormFile file) - { - if (file.Length == 0) - return BadRequest("No file uploaded."); - - var filePath = Path.GetTempFileName(); - - await using (var stream = System.IO.File.Create(filePath)) - { - await file.CopyToAsync(stream); - } - - await _accountService.AddAccountsFromCsvAsync(filePath); - - return Ok("Accounts imported successfully."); - } - - [HttpGet("{accountId}")] - public async Task GetAccountById(long accountId) - { - var account = await _accountService.GetAccountByIdAsync(accountId); - if (account == null) - { - return NotFound(); - } - - return Ok(account.ToAccountDto()); - } - - [HttpGet("{accountId}")] - public async Task 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())); - } +using Application.Interfaces.Services; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Web.Helper; +using Web.Mappers; + +namespace Web.Controllers; + +[ApiController] +[Route("[controller]/[action]")] +public class AccountController : ControllerBase +{ + private readonly IAccountService _accountService; + + public AccountController(IAccountService accountService) + { + _accountService = accountService; + } + + [HttpPost] + public async Task ImportAccounts([FromForm] IFormFile file) + { + if (file.Length == 0) + return BadRequest("No file uploaded."); + + var filePath = Path.GetTempFileName(); + + await using (var stream = System.IO.File.Create(filePath)) + { + await file.CopyToAsync(stream); + } + + await _accountService.AddAccountsFromCsvAsync(filePath); + + return Ok("Accounts imported successfully."); + } + + [HttpGet("{accountId}")] + public async Task GetAccountById(long accountId) + { + var account = await _accountService.GetAccountByIdAsync(accountId); + if (account == null) + { + return NotFound(); + } + + return Ok(account.ToAccountDto()); + } + + [HttpGet("{accountId}")] + public async Task 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] + 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 a05c625..f127c9d 100644 --- a/src/Web/Mappers/AccountMapper.cs +++ b/src/Web/Mappers/AccountMapper.cs @@ -1,24 +1,42 @@ -using Domain.Entities; -using Web.DTOs.Account; - -namespace Web.Mappers; - -public static class AccountMapper -{ - public static AccountDto ToAccountDto(this Account account) - { - return new AccountDto - { - AccountId = account.AccountId, - CardId = account.CardId, - Iban = account.Iban, - AccountType = account.AccountType, - BranchTelephone = account.BranchTelephone, - BranchAddress = account.BranchAddress, - BranchName = account.BranchName, - OwnerName = account.OwnerName, - OwnerLastName = account.OwnerLastName, - OwnerId = account.OwnerId - }; - } +using Application.DTOs.AccountCsv; +using Domain.Entities; +using Web.DTOs.Account; + +namespace Web.Mappers; + +public static class AccountMapper +{ + public static AccountDto ToAccountDto(this Account account) + { + return new AccountDto + { + AccountId = account.AccountId, + CardId = account.CardId, + Iban = account.Iban, + AccountType = account.AccountType, + BranchTelephone = account.BranchTelephone, + BranchAddress = account.BranchAddress, + BranchName = account.BranchName, + OwnerName = account.OwnerName, + OwnerLastName = account.OwnerLastName, + 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 6905884..c6341b1 100644 --- a/src/Web/Mappers/TransactionMapper.cs +++ b/src/Web/Mappers/TransactionMapper.cs @@ -1,33 +1,34 @@ -using Domain.Entities; -using Web.DTOs.Transaction; - -namespace Web.Mappers; - -public static class TransactionMapper -{ - public static TransactionDto ToTransactionDto(this Transaction transaction) - { - return new TransactionDto - { - TransactionId = transaction.TransactionId, - SourceAccountId = transaction.SourceAccountId, - DestinationAccountId = transaction.DestinationAccountId, - Amount = transaction.Amount, - Date = transaction.Date, - Type = transaction.Type - }; - } - - public static List ToGotAllTransactionsDto(this GetAllTransactionsResponse response) - { - 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(); - } +using Application.DTOs.TransactionCsv; +using Domain.Entities; +using Web.DTOs.Transaction; + +namespace Web.Mappers; + +public static class TransactionMapper +{ + public static TransactionDto ToTransactionDto(this Transaction transaction) + { + return new TransactionDto + { + TransactionId = transaction.TransactionId, + SourceAccountId = transaction.SourceAccountId, + DestinationAccountId = transaction.DestinationAccountId, + Amount = transaction.Amount, + Date = transaction.Date, + Type = transaction.Type + }; + } + + public static List ToGotAllTransactionsDto(this GetAllTransactionsResponse response) + { + return response.Transactions.Select(transaction => new TransactionDto + { + 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