Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: make csv-reader generic and mockable #29

Merged
merged 1 commit into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading