Skip to content

Commit

Permalink
feat: add ICsvReaderService interface
Browse files Browse the repository at this point in the history
  • Loading branch information
mobinbr committed Sep 7, 2024
1 parent 19f05ce commit b83e8d4
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 6 deletions.
6 changes: 6 additions & 0 deletions src/Application/Interfaces/Services/ICsvReaderService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Application.Interfaces.Services;

public interface ICsvReaderService
{
List<T> ReadFromCsv<T>(string filePath);
}
6 changes: 4 additions & 2 deletions src/Application/Services/DomainService/AccountService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@ namespace Application.Services.DomainService;
public class AccountService : IAccountService
{
private readonly IAccountRepository _accountRepository;
private readonly ICsvReaderService _csvReaderService;

public AccountService(IAccountRepository accountRepository)
public AccountService(IAccountRepository accountRepository, ICsvReaderService csvReaderService)
{
_accountRepository = accountRepository;
_csvReaderService = csvReaderService;
}

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

var accounts = accountCsvModels
.Select(csvModel => csvModel.ToAccount())
Expand Down
6 changes: 4 additions & 2 deletions src/Application/Services/DomainService/TransactionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ namespace Application.Services.DomainService;
public class TransactionService : ITransactionService
{
private readonly ITransactionRepository _transactionRepository;
private readonly ICsvReaderService _csvReaderService;

public TransactionService(ITransactionRepository transactionRepository)
public TransactionService(ITransactionRepository transactionRepository, ICsvReaderService csvReaderService)
{
_transactionRepository = transactionRepository;
_csvReaderService = csvReaderService;
}

public async Task<Result> AddTransactionsFromCsvAsync(string filePath)
{
var transactionCsvModels = CsvReaderService.ReadFromCsv<TransactionCsvModel>(filePath);
var transactionCsvModels = _csvReaderService.ReadFromCsv<TransactionCsvModel>(filePath);

var transactions = transactionCsvModels
.Select(csvModel => csvModel.ToTransaction())
Expand Down
5 changes: 3 additions & 2 deletions src/Application/Services/SharedService/CsvReaderService.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System.Globalization;
using Application.Interfaces.Services;
using CsvHelper;
using CsvHelper.Configuration;

namespace Application.Services.SharedService;

public static class CsvReaderService
public class CsvReaderService : ICsvReaderService
{
public static List<T> ReadFromCsv<T>(string filePath)
public List<T> ReadFromCsv<T>(string filePath)
{
using var reader = new StreamReader(filePath);
using var csv = new CsvReader(reader, new CsvConfiguration(CultureInfo.InvariantCulture)
Expand Down
2 changes: 2 additions & 0 deletions src/Web/Startup/ServiceExtensions.DI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Application.Interfaces.Repositories;
using Application.Interfaces.Services;
using Application.Services.DomainService;
using Application.Services.SharedService;
using Infrastructure.Repositories;
using Web.Services;

Expand All @@ -20,5 +21,6 @@ public static void AddApplicationServices(this IServiceCollection services)
services.AddScoped<ITransactionService, TransactionService>();
services.AddScoped<IAccountRepository, AccountRepository>();
services.AddScoped<IAccountService, AccountService>();
services.AddScoped<ICsvReaderService, CsvReaderService>();
}
}

0 comments on commit b83e8d4

Please sign in to comment.