using FinlyticCore.Dtos.Assets; using FinlyticCore.Entities.Assets; namespace FinlyticCore.Util; public static class AssetMapper { /// /// Mappt eine AssetEntity (Datenbank) sicher auf ein zyklusfreies AssetDto (MQTT Payload). /// public static AssetDto ToDto(this AssetEntity entity) { // 1. Tags zyklusfrei mappen var dtoTags = entity.Tags.Select(t => new TagDto { Id = t.Id, Name = t.Name, Type = t.Type }).ToList(); // 2. Polymorphes Mapping basierend auf dem Laufzeittyp return entity switch { StockEntity stock => new StockDto { Isin = stock.Isin, Name = stock.Name, Type = stock.Type, InstrumentCategory = stock.InstrumentCategory, HasCfd = stock.HasCfd, ImageId = stock.ImageId, UpdateAt = stock.UpdateAt, 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 = etf.ImageId, UpdateAt = etf.UpdateAt, LastUpdatedAt = etf.LastUpdatedAt, Tags = dtoTags, DerivativeProductCategories = etf.DerivativeProductCategories, EtfDescription = etf.EtfDescription, MappedEtfIndexName = etf.MappedEtfIndexName, Subtitle = etf.Subtitle, SearchSubtitle = etf.SearchSubtitle }, CryptoEntity crypto => new CryptoDto { Isin = crypto.Isin, Name = crypto.Name, Type = crypto.Type, InstrumentCategory = crypto.InstrumentCategory, HasCfd = crypto.HasCfd, ImageId = crypto.ImageId, UpdateAt = crypto.UpdateAt, LastUpdatedAt = crypto.LastUpdatedAt, Tags = dtoTags, Subtitle = crypto.Subtitle, SearchSubtitle = crypto.SearchSubtitle }, BondEntity bond => new BondDto { Isin = bond.Isin, Name = bond.Name, Type = bond.Type, InstrumentCategory = bond.InstrumentCategory, HasCfd = bond.HasCfd, ImageId = bond.ImageId, UpdateAt = bond.UpdateAt, LastUpdatedAt = bond.LastUpdatedAt, Tags = dtoTags, BondIssuerName = bond.BondIssuerName, SearchSubtitle = bond.SearchSubtitle }, DerivativeEntity deriv => new DerivativeDto { Isin = deriv.Isin, Name = deriv.Name, Type = deriv.Type, InstrumentCategory = deriv.InstrumentCategory, HasCfd = deriv.HasCfd, ImageId = deriv.ImageId, UpdateAt = deriv.UpdateAt, LastUpdatedAt = deriv.LastUpdatedAt, Tags = dtoTags, DerivativeProductCategories = deriv.DerivativeProductCategories, UnderlyingIsin = deriv.UnderlyingIsin }, SyntheticEntity synth => new SyntheticDto { Isin = synth.Isin, Name = synth.Name, Type = synth.Type, InstrumentCategory = synth.InstrumentCategory, HasCfd = synth.HasCfd, ImageId = synth.ImageId, UpdateAt = synth.UpdateAt, LastUpdatedAt = synth.LastUpdatedAt, Tags = dtoTags, DerivativeProductCategories = synth.DerivativeProductCategories }, _ => throw new NotSupportedException($"Mapping for type {entity.GetType().Name} is not supported.") }; } /// /// Mappt direkt eine ganze Liste von AssetEntities. /// public static List ToDtoList(this IEnumerable entities) { return entities.Select(e => e.ToDto()).ToList(); } }