Skip to content

Commit

Permalink
Merge pull request #29 from Star-Academy/refactor/make-csvreader-mock…
Browse files Browse the repository at this point in the history
…able-generic

refactor: make csv-reader generic and mockable
  • Loading branch information
amiralirahimii authored Sep 1, 2024
2 parents 8639110 + 49a00e5 commit 3d0473d
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 6 deletions.
6 changes: 6 additions & 0 deletions src/Application/Interfaces/IFileReaderService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Application.Interfaces;

public interface IFileReaderService
{
List<T> ReadFromFile<T>(string filePath);
}
7 changes: 5 additions & 2 deletions src/Application/Services/DomainService/AccountService.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Result> AddAccountsFromCsvAsync(string filePath)
{
try
{
var accountCsvModels = CsvReaderService.ReadFromCsv<AccountCsvModel>(filePath);
var accountCsvModels = _fileReaderService.ReadFromFile<AccountCsvModel>(filePath);

var accounts = accountCsvModels
.Select(csvModel => csvModel.ToAccount())
Expand Down
7 changes: 5 additions & 2 deletions src/Application/Services/DomainService/TransactionService.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Result> AddTransactionsFromCsvAsync(string filePath)
{
var transactionCsvModels = CsvReaderService.ReadFromCsv<TransactionCsvModel>(filePath);
var transactionCsvModels = _fileReaderService.ReadFromFile<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;
using CsvHelper;
using CsvHelper.Configuration;

namespace Application.Services.SharedService;

public static class CsvReaderService
public class CsvReaderService : IFileReaderService
{
public static List<T> ReadFromCsv<T>(string filePath)
public List<T> ReadFromFile<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 @@ -12,6 +13,7 @@ public static partial class ServiceExtensions
public static void AddApplicationServices(this IServiceCollection services)
{
services.AddScoped<ITokenService, TokenService>();
services.AddScoped<IFileReaderService, CsvReaderService>();
services.AddScoped<IRoleManagerRepository, RoleManagerRepository>();
services.AddScoped<IUserManagerRepository, UserManagerRepository>();
services.AddScoped<IUserService, UserService>();
Expand Down

0 comments on commit 3d0473d

Please sign in to comment.