Skip to content

Instantly share code, notes, and snippets.

@delacrixmorgan
Last active June 7, 2023 13:18
Show Gist options
  • Save delacrixmorgan/df4b797fabe926714f283b6d98402c84 to your computer and use it in GitHub Desktop.
Save delacrixmorgan/df4b797fabe926714f283b6d98402c84 to your computer and use it in GitHub Desktop.
SecurityDtoToModelMapper Question
class SecurityDtoToModelMapper @Inject constructor(
    private val securityDataProvider: SecurityDataProvider,
    private val securityAssetClassDtoToModelMapper: SecurityAssetClassDtoToModelMapper,
    private val securityDividendPolicyDtoToModelMapper: SecurityDividendPolicyDtoToModelMapper,
    private val securityEsgRatingDtoToModelMapper: SecurityEsgRatingDtoToModelMapper,
    private val securityTopHoldingDtoToModelMapper: SecurityTopHoldingDtoToModelMapper,
    private val securityPerformancePointDtoToModelMapper: SecurityPerformancePointDtoToModelMapper,
    private val securityTagsDtoToModelMapper: SecurityTagsDtoToModelMapper,
    private val currencyDtoToModelMapper: CurrencyDtoToModelMapper,
) : Mapper<SecurityDto, Security> {

    override suspend fun invoke(input: SecurityDto): Security {
        val isin = input.isin ?: mappingTerminalError("null ISIN")
        val securitiesData = securityDataProvider.getSecurityData(isin)

        return Security(
            isin = isin,
            name = securitiesData?.name ?: isin,
            assetClass = securitiesData?.assetClass?.let { securityAssetClassDtoToModelMapper(it) } ?: SecurityAssetClass.Stock,
            instrument = SecurityInstrument.ETF,
            currency = securitiesData?.currency?.let { currencyDtoToModelMapper(it) } ?: Currency.EUR,
            dividendPolicy = securitiesData?.dividendPolicy?.let { securityDividendPolicyDtoToModelMapper(it) } ?: SecurityDividendPolicy.None,
            riskClass = securitiesData?.riskClass?.let { SecurityRiskClass(it) } ?: SecurityRiskClass(7),
            esgRating = securitiesData?.esgRating?.let { securityEsgRatingDtoToModelMapper(it) } ?: SecurityEsgRating.Unknown,
            topHoldings = securitiesData?.topHoldings?.let { ListMapper(securityTopHoldingDtoToModelMapper)(it) } ?: emptyList(),
            historicalPerformance = securitiesData?.historicalPerformance?.let { ListMapper(securityPerformancePointDtoToModelMapper)(it) } ?: emptyList(),
            yearlyCost = securitiesData?.yearlyCost ?: 0.0,
            tags = securitiesData?.tags?.let { securityTagsDtoToModelMapper(it) } ?: emptyList(),
            updatedAt = securitiesData?.updatedAt ?: Date(0),
            enabled = input.enabled ?: mappingErrorReturnDefault("enabled was null") { false },
        )
    }
}
class SecurityDtoToModelMapper @Inject constructor(
    private val getSecurityUseCase: GetSecurityUseCase,
) : Mapper<SecurityDto, Security> {

    override suspend fun invoke(input: SecurityDto): Security {
        val isin = input.isin ?: mappingTerminalError("null ISIN")
        return getSecurityUseCase(GetSecurityUseCase.Params(isin)).get()
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment