Files
Finlytic/FinlyticAssets/Util/AssetMapper.cs
T

118 lines
4.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using FinlyticAssets.Entities;
using FinlyticCore.Dtos.Assets;
namespace FinlyticAssets.Util;
public static class AssetMapper
{
public static AssetDto ToDto(this AssetEntity entity)
{
var dtoTags = entity.Tags.Select(t => new TagDto
{
Id = t.Id,
Name = t.Name,
Type = t.Type
}).ToList();
var dynamicLogoUrl = $"/api/v1/logo/{entity.Isin}";
return entity switch
{
StockEntity stock => new StockDto
{
Isin = stock.Isin,
Name = stock.Name,
Type = stock.Type,
InstrumentCategory = stock.InstrumentCategory,
HasCfd = stock.HasCfd,
ImageId = dynamicLogoUrl,
LastUpdatedAt = stock.LastUpdatedAt,
Tags = dtoTags,
DerivativeProductCategories = stock.DerivativeProductCategories
},
EtfEntity etf => new EtfDto
{
Isin = etf.Isin,
Name = etf.Name,
Type = etf.Type,
InstrumentCategory = etf.InstrumentCategory,
HasCfd = etf.HasCfd,
ImageId = dynamicLogoUrl,
LastUpdatedAt = etf.LastUpdatedAt,
Tags = dtoTags,
DerivativeProductCategories = etf.DerivativeProductCategories,
EtfDescription = etf.EtfDescription,
MappedEtfIndexName = etf.MappedEtfIndexName,
Subtitle = etf.Subtitle,
SearchSubtitle = etf.SearchSubtitle
},
SyntheticEntity synth => new SyntheticDto
{
Isin = synth.Isin,
Name = synth.Name,
Type = synth.Type,
InstrumentCategory = synth.InstrumentCategory,
HasCfd = synth.HasCfd,
ImageId = dynamicLogoUrl,
LastUpdatedAt = synth.LastUpdatedAt,
Tags = dtoTags,
DerivativeProductCategories = synth.DerivativeProductCategories
},
_ => new StockDto
{
Isin = entity.Isin,
Name = entity.Name,
Type = entity.Type,
InstrumentCategory = entity.InstrumentCategory,
HasCfd = entity.HasCfd,
ImageId = dynamicLogoUrl,
LastUpdatedAt = entity.LastUpdatedAt,
Tags = dtoTags
}
};
}
public static DerivativeDto ToDto(this DerivativeEntity deriv)
{
return new DerivativeDto
{
Isin = deriv.Isin,
Name = deriv.Name,
Type = "derivative",
InstrumentCategory = "derivative",
HasCfd = false,
ImageId = $"/api/v1/logo/{deriv.UnderlyingIsin}",
LastUpdatedAt = deriv.LastUpdatedAt,
Tags = new List<TagDto>(),
DerivativeProductCategories = deriv.DerivativeProductCategories,
UnderlyingIsin = deriv.UnderlyingIsin,
OptionType = deriv.OptionType.ToString(),
ProductCategoryName = deriv.ProductCategoryName,
NextGenProductCategoryName = deriv.NextGenProductCategoryName,
Strike = deriv.Strike,
Barrier = deriv.Barrier,
Leverage = deriv.Leverage,
Size = deriv.Size,
Factor = deriv.Factor,
Delta = deriv.Delta,
Currency = deriv.Currency,
Expiry = deriv.Expiry,
Issuer = deriv.Issuer,
IssuerDisplayName = deriv.IssuerDisplayName,
IssuerImageId = null
};
}
public static List<AssetDto> ToDtoList(this IEnumerable<AssetEntity> entities)
{
return entities.Select(e => e.ToDto()).ToList();
}
public static List<DerivativeDto> ToDtoList(this IEnumerable<DerivativeEntity> derivatives)
{
return derivatives.Select(d => d.ToDto()).ToList();
}
}