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

70 lines
2.4 KiB
C#

using System.Text;
using AutoMapper;
using MediatR;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using ExpenseTracker.Application.Common.Interfaces.Repositories;
using ExpenseTracker.Domain.Entities;
namespace ExpenseTracker.Application.Accounts.Queries.Export.Json.One;
public class ExportJsonAccountQueryHandler : IRequestHandler<ExportJsonAccountQuery, byte[]>
{
private readonly IMapper _mapper;
private readonly IAccountRepository _accountRepository;
private readonly ITransactionRepository _transactionRepository;
public ExportJsonAccountQueryHandler(
IMapper mapper,
IAccountRepository repository,
ITransactionRepository transactionRepository)
{
_mapper = mapper;
_accountRepository = repository;
_transactionRepository = transactionRepository;
}
public async Task<byte[]> Handle(ExportJsonAccountQuery 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,
Currency = account.Currency,
UserId = account.UserId,
Transactions = transactions
}
)
.FirstOrDefault(e => e.Id == request.Id);
// TODO: Mapping to DTO creates new objects therefore using more resources
// Create custom json serializer to avoid mapping
var entityDto = _mapper.Map<AccountJsonDto>(entity);
var jsonSerializerSettings = new JsonSerializerSettings()
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
var json = JsonConvert.SerializeObject(entityDto, jsonSerializerSettings);
using (var memoryStream = new MemoryStream())
{
using (var streamWriter = new StreamWriter(memoryStream, Encoding.UTF8))
{
await streamWriter.WriteAsync(json);
await streamWriter.FlushAsync();
}
return memoryStream.ToArray();
}
}
}