diff --git a/src/Application/Interfaces/IFileReaderService.cs b/src/Application/Interfaces/IFileReaderService.cs new file mode 100644 index 0000000..c677896 --- /dev/null +++ b/src/Application/Interfaces/IFileReaderService.cs @@ -0,0 +1,6 @@ +namespace Application.Interfaces; + +public interface IFileReaderService +{ + List ReadFromFile(string filePath); +} \ No newline at end of file diff --git a/src/Application/Services/DomainService/AccountService.cs b/src/Application/Services/DomainService/AccountService.cs index 8e8cef1..b4a16a4 100644 --- a/src/Application/Services/DomainService/AccountService.cs +++ b/src/Application/Services/DomainService/AccountService.cs @@ -1,5 +1,6 @@ using Application.DTOs; using Application.DTOs.Account; +using Application.Interfaces; using Application.Interfaces.Repositories; using Application.Interfaces.Services; using Application.Mappers; @@ -11,17 +12,19 @@ namespace Application.Services.DomainService; public class AccountService : IAccountService { private readonly IAccountRepository _accountRepository; + private readonly IFileReaderService _fileReaderService; - public AccountService(IAccountRepository accountRepository) + public AccountService(IAccountRepository accountRepository, IFileReaderService fileReaderService) { _accountRepository = accountRepository; + _fileReaderService = fileReaderService; } public async Task AddAccountsFromCsvAsync(string filePath) { try { - var accountCsvModels = CsvReaderService.ReadFromCsv(filePath); + var accountCsvModels = _fileReaderService.ReadFromFile(filePath); var accounts = accountCsvModels .Select(csvModel => csvModel.ToAccount()) diff --git a/src/Application/Services/DomainService/TransactionService.cs b/src/Application/Services/DomainService/TransactionService.cs index 3cd71eb..9e1e714 100644 --- a/src/Application/Services/DomainService/TransactionService.cs +++ b/src/Application/Services/DomainService/TransactionService.cs @@ -1,5 +1,6 @@ using Application.DTOs; using Application.DTOs.Transaction; +using Application.Interfaces; using Application.Interfaces.Services; using Application.Mappers; using Application.Services.SharedService; @@ -11,15 +12,17 @@ namespace Application.Services.DomainService; public class TransactionService : ITransactionService { private readonly ITransactionRepository _transactionRepository; + private readonly IFileReaderService _fileReaderService; - public TransactionService(ITransactionRepository transactionRepository) + public TransactionService(ITransactionRepository transactionRepository, IFileReaderService fileReaderService) { _transactionRepository = transactionRepository; + _fileReaderService = fileReaderService; } public async Task AddTransactionsFromCsvAsync(string filePath) { - var transactionCsvModels = CsvReaderService.ReadFromCsv(filePath); + var transactionCsvModels = _fileReaderService.ReadFromFile(filePath); var transactions = transactionCsvModels .Select(csvModel => csvModel.ToTransaction()) diff --git a/src/Application/Services/SharedService/CsvReaderService.cs b/src/Application/Services/SharedService/CsvReaderService.cs index c12e326..b7b3692 100644 --- a/src/Application/Services/SharedService/CsvReaderService.cs +++ b/src/Application/Services/SharedService/CsvReaderService.cs @@ -1,12 +1,13 @@ using System.Globalization; +using Application.Interfaces; using CsvHelper; using CsvHelper.Configuration; namespace Application.Services.SharedService; -public static class CsvReaderService +public class CsvReaderService : IFileReaderService { - public static List ReadFromCsv(string filePath) + public List ReadFromFile(string filePath) { using var reader = new StreamReader(filePath); using var csv = new CsvReader(reader, new CsvConfiguration(CultureInfo.InvariantCulture) diff --git a/src/Web/Startup/ServiceExtensions.DI.cs b/src/Web/Startup/ServiceExtensions.DI.cs index 46f6623..13f954f 100644 --- a/src/Web/Startup/ServiceExtensions.DI.cs +++ b/src/Web/Startup/ServiceExtensions.DI.cs @@ -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; @@ -12,6 +13,7 @@ public static partial class ServiceExtensions public static void AddApplicationServices(this IServiceCollection services) { services.AddScoped(); + services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped();