From b83e8d4eb171f86e2a225cd5810d738216506817 Mon Sep 17 00:00:00 2001 From: Mobin Barfi Date: Sat, 7 Sep 2024 16:29:25 +0330 Subject: [PATCH] feat: add ICsvReaderService interface --- src/Application/Interfaces/Services/ICsvReaderService.cs | 6 ++++++ src/Application/Services/DomainService/AccountService.cs | 6 ++++-- .../Services/DomainService/TransactionService.cs | 6 ++++-- src/Application/Services/SharedService/CsvReaderService.cs | 5 +++-- src/Web/Startup/ServiceExtensions.DI.cs | 2 ++ 5 files changed, 19 insertions(+), 6 deletions(-) create mode 100644 src/Application/Interfaces/Services/ICsvReaderService.cs diff --git a/src/Application/Interfaces/Services/ICsvReaderService.cs b/src/Application/Interfaces/Services/ICsvReaderService.cs new file mode 100644 index 0000000..dc56c7b --- /dev/null +++ b/src/Application/Interfaces/Services/ICsvReaderService.cs @@ -0,0 +1,6 @@ +namespace Application.Interfaces.Services; + +public interface ICsvReaderService +{ + List ReadFromCsv(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..f893788 100644 --- a/src/Application/Services/DomainService/AccountService.cs +++ b/src/Application/Services/DomainService/AccountService.cs @@ -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 AddAccountsFromCsvAsync(string filePath) { try { - var accountCsvModels = CsvReaderService.ReadFromCsv(filePath); + var accountCsvModels = _csvReaderService.ReadFromCsv(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..6729940 100644 --- a/src/Application/Services/DomainService/TransactionService.cs +++ b/src/Application/Services/DomainService/TransactionService.cs @@ -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 AddTransactionsFromCsvAsync(string filePath) { - var transactionCsvModels = CsvReaderService.ReadFromCsv(filePath); + var transactionCsvModels = _csvReaderService.ReadFromCsv(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..2cf14b8 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.Services; using CsvHelper; using CsvHelper.Configuration; namespace Application.Services.SharedService; -public static class CsvReaderService +public class CsvReaderService : ICsvReaderService { - public static List ReadFromCsv(string filePath) + public List ReadFromCsv(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..d43179f 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; @@ -20,5 +21,6 @@ public static void AddApplicationServices(this IServiceCollection services) services.AddScoped(); services.AddScoped(); services.AddScoped(); + services.AddScoped(); } } \ No newline at end of file