Skip to content

Commit

Permalink
feat: add get-all-accounts api
Browse files Browse the repository at this point in the history
  • Loading branch information
mobinbr committed Aug 19, 2024
1 parent f93c323 commit dc5f4f6
Show file tree
Hide file tree
Showing 13 changed files with 127 additions and 44 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();
}
15 changes: 2 additions & 13 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;
namespace Application.DTOs.TransactionCsv;

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;
}

public List<TransactionCsvModel> Transactions { get; set; } = new();
}
1 change: 1 addition & 0 deletions src/Application/Interfaces/IAccountRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ public interface IAccountRepository
Task CreateBulkAsync(List<Account> accounts);
Task<Account?> GetByIdAsync(long accountId);
Task<List<Transaction>> GetTransactionsByAccountId(long accountId);
Task<List<Account>> GetAllAccounts();
}
2 changes: 2 additions & 0 deletions src/Application/Interfaces/Services/IAccountService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Application.DTOs;
using Application.DTOs.AccountCsv;
using Domain.Entities;

namespace Application.Interfaces.Services;
Expand All @@ -8,4 +9,5 @@ public interface IAccountService
Task AddAccountsFromCsvAsync(string filePath);
Task<Account?> GetAccountByIdAsync(long accountId);
Task<Result<List<Transaction>>> GetTransactionsByUserId(long accountId);
Task<Result<GetAllAccountsResponse>> GetAllAccounts();
}
2 changes: 1 addition & 1 deletion src/Application/Interfaces/Services/ITransactionService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Application.DTOs;
using Web.DTOs.Transaction;
using Application.DTOs.TransactionCsv;

namespace Application.Interfaces.Services;

Expand Down
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()
};
}
}
30 changes: 14 additions & 16 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 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<Transaction> transactions)
{
public static GetAllTransactionsResponse ToGetAllTransactionsResponse(this List<Transaction> 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()
};
}
}
20 changes: 20 additions & 0 deletions src/Application/Services/AccountService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -54,4 +55,23 @@ public async Task<Result<List<Transaction>>> GetTransactionsByUserId(long accoun
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}");
}
}
}
2 changes: 1 addition & 1 deletion src/Application/Services/TransactionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
5 changes: 5 additions & 0 deletions src/Infrastructure/Services/AccountRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,9 @@ public async Task<List<Transaction>> GetTransactionsByAccountId(long accountId)

return account.SourceTransactions.ToList();
}

public async Task<List<Account>> GetAllAccounts()
{
return await _dbContext.Accounts.ToListAsync();
}
}
16 changes: 16 additions & 0 deletions src/Web/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -58,4 +60,18 @@ public async Task<IActionResult> GetTransactionsByUserId(long accountId)

return Ok(transactions!.Select(t => t.ToTransactionDto()));
}

[HttpGet]
[Authorize]
public async Task<IActionResult> 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());
}
}
20 changes: 19 additions & 1 deletion src/Web/Mappers/AccountMapper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Domain.Entities;
using Application.DTOs.AccountCsv;
using Domain.Entities;
using Web.DTOs.Account;

namespace Web.Mappers;
Expand All @@ -21,4 +22,21 @@ public static AccountDto ToAccountDto(this Account account)
OwnerId = account.OwnerId
};
}

public static List<AccountDto> 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();
}
}
25 changes: 13 additions & 12 deletions src/Web/Mappers/TransactionMapper.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Application.DTOs.TransactionCsv;
using Domain.Entities;
using Web.DTOs.Transaction;

Expand All @@ -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<GetAllTransactionsResponse.TransactionDto> ToGotAllTransactionsDto(this GetAllTransactionsResponse response)
public static List<TransactionDto> 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();
}
}

0 comments on commit dc5f4f6

Please sign in to comment.