classlib/ExpenseTracker.Application/Accounts/Queries/Export/Csv/One/ExportCsvAccountQueryHandler.cs
2024-08-07 21:12:02 +03:00

68 lines
2.3 KiB
C#

using System.Globalization;
using System.Text;
using AutoMapper;
using CsvHelper;
using MediatR;
using ExpenseTracker.Application.Common.Exceptions;
using ExpenseTracker.Application.Common.Interfaces.Repositories;
using ExpenseTracker.Domain.Entities;
namespace ExpenseTracker.Application.Accounts.Queries.Export.Csv.One;
public class ExportCsvAccountQueryHandler : IRequestHandler<ExportCsvAccountQuery, byte[]>
{
private readonly IMapper _mapper;
private readonly IAccountRepository _accountRepository;
private readonly ITransactionRepository _transactionRepository;
public ExportCsvAccountQueryHandler(
IMapper mapper,
IAccountRepository repository,
ITransactionRepository transactionRepository)
{
_mapper = mapper;
_accountRepository = repository;
_transactionRepository = transactionRepository;
}
public async Task<byte[]> Handle(ExportCsvAccountQuery request, CancellationToken cancellationToken)
{
var entity = _accountRepository.Queryable
.GroupJoin(
_transactionRepository.Queryable,
b => b.Id,
t => t.AccountId,
(account, transactions) =>
new Account
{
Id = account.Id,
Name = account.Name,
Description = account.Description,
UserId = account.UserId,
Transactions = transactions
}
)
.FirstOrDefault(e => e.Id == request.Id);
// TODO: Mapping to DTO creates new objects therefore using more resources
// Create custom csv serializer to avoid mapping
var entityDto = _mapper.Map<AccountJsonDto>(entity);
using (var memoryStream = new MemoryStream())
{
using (var streamWriter = new StreamWriter(memoryStream, Encoding.UTF8))
{
using (var csvWriter = new CsvWriter(streamWriter, CultureInfo.InvariantCulture))
{
csvWriter.Context.RegisterClassMap<TransactionCsvMap>();
await csvWriter.WriteRecordsAsync(entityDto.Transactions);
await csvWriter.FlushAsync();
}
}
return memoryStream.ToArray();
}
}
}