diff --git a/src/main/kotlin/com/api/nft/config/RedisConfig.kt b/src/main/kotlin/com/api/nft/config/RedisConfig.kt index 67f1409..93b6d9a 100644 --- a/src/main/kotlin/com/api/nft/config/RedisConfig.kt +++ b/src/main/kotlin/com/api/nft/config/RedisConfig.kt @@ -1,5 +1,8 @@ package com.api.nft.config +import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.databind.jsontype.BasicPolymorphicTypeValidator +import com.fasterxml.jackson.module.kotlin.KotlinModule import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration import org.springframework.data.redis.connection.RedisClusterConfiguration @@ -43,7 +46,7 @@ class RedisConfig { .autoReconnect(true) .pingBeforeActivateConnection(true) .build() - ) + ) .build() redisClusterConfiguration.password = RedisPassword.of("bitnami") redisClusterConfiguration.maxRedirects = 3 diff --git a/src/main/kotlin/com/api/nft/controller/dto/NftMetadataResponse.kt b/src/main/kotlin/com/api/nft/controller/dto/NftMetadataResponse.kt index 42be222..36c9a3d 100644 --- a/src/main/kotlin/com/api/nft/controller/dto/NftMetadataResponse.kt +++ b/src/main/kotlin/com/api/nft/controller/dto/NftMetadataResponse.kt @@ -2,6 +2,8 @@ package com.api.nft.controller.dto import com.api.nft.enums.ChainType import com.api.nft.enums.ContractType +import io.r2dbc.spi.Row +import java.math.BigDecimal data class NftMetadataResponse( @@ -14,4 +16,24 @@ data class NftMetadataResponse( val collectionName: String, val image: String, val lastPrice: Double?, -) + val collectionLogo: String? +){ + companion object { + fun fromRow(row: Row): NftMetadataResponse { + val idValue = row.get("id") + println("ID value type: ${idValue?.javaClass}, value: $idValue") + return NftMetadataResponse( + id = row.get("id", Integer::class.java)!!.toLong(), + tokenId = row.get("tokenid", String::class.java)!!, + tokenAddress = row.get("tokenaddress", String::class.java)!!, + chainType = ChainType.valueOf(row.get("chaintype", String::class.java)!!), + nftName = row.get("nftname", String::class.java)!!, + collectionName = row.get("collectionname", String::class.java)!!, + image = row.get("image", String::class.java) ?: "", + contractType = ContractType.valueOf(row.get("contracttype", String::class.java)!!), + lastPrice = row.get("lastprice", BigDecimal::class.java)?.toDouble(), + collectionLogo = row.get("collectionlogo", String::class.java) ?: "" + ) + } + } +} diff --git a/src/main/kotlin/com/api/nft/domain/nft/repository/NftRepositorySupportImpl.kt b/src/main/kotlin/com/api/nft/domain/nft/repository/NftRepositorySupportImpl.kt index da84206..c76c1b4 100644 --- a/src/main/kotlin/com/api/nft/domain/nft/repository/NftRepositorySupportImpl.kt +++ b/src/main/kotlin/com/api/nft/domain/nft/repository/NftRepositorySupportImpl.kt @@ -1,12 +1,9 @@ package com.api.nft.domain.nft.repository import com.api.nft.controller.dto.NftMetadataResponse -import com.api.nft.enums.ChainType -import com.api.nft.enums.ContractType import org.springframework.data.r2dbc.core.R2dbcEntityTemplate import reactor.core.publisher.Flux import reactor.core.publisher.Mono -import java.math.BigDecimal class NftRepositorySupportImpl( private val r2dbcEntityTemplate: R2dbcEntityTemplate @@ -22,9 +19,12 @@ class NftRepositorySupportImpl( n.collection_name AS collectionName, n.contract_type AS contractType, m.image AS image, - nl.price AS lastPrice + nl.price AS lastPrice, + c.logo AS collectionLogo FROM nft n + LEFT JOIN + collection c ON n.collection_name = c.name LEFT JOIN metadata m ON n.id = m.nft_id LEFT JOIN @@ -32,22 +32,10 @@ class NftRepositorySupportImpl( WHERE n.id = :$1 """ - return r2dbcEntityTemplate.databaseClient.sql(query) .bind(0, id) - .map { row, data -> - NftMetadataResponse( - id = (row.get("id") as Number).toLong(), - tokenId = row.get("tokenId", String::class.java)!!, - tokenAddress = row.get("tokenAddress", String::class.java)!!, - chainType = row.get("chainType", ChainType::class.java)!!, - nftName = row.get("nftName", String::class.java)!!, - collectionName = row.get("collectionName", String::class.java)!!, - image = row.get("image", String::class.java) ?: "", - contractType = row.get("contractType", ContractType::class.java)!!, - lastPrice = row.get("lastPrice", BigDecimal::class.java)?.toDouble(), - ) - }.first() + .map { row, _ -> NftMetadataResponse.fromRow(row) } + .first() } override fun findAllByNftJoinMetadata(ids: List): Flux { @@ -63,27 +51,18 @@ class NftRepositorySupportImpl( m.image AS image FROM nft n + LEFT JOIN + collection c ON n.collection_name = c.name LEFT JOIN metadata m ON n.id = m.nft_id - LEFT JOIN nft_listing nl ON nft.id = nl.nft_id + LEFT JOIN + nft_listing nl ON n.id = nl.nft_id WHERE n.id IN (:$1) """ return r2dbcEntityTemplate.databaseClient.sql(query) .bind(0, ids) - .map { row, metadata -> - NftMetadataResponse( - id = (row.get("id") as Number).toLong(), - tokenId = row.get("tokenId", String::class.java)!!, - tokenAddress = row.get("tokenAddress", String::class.java)!!, - chainType = row.get("chainType", ChainType::class.java)!!, - nftName = row.get("nftName", String::class.java)!!, - collectionName = row.get("collectionName", String::class.java)!!, - image = row.get("image", String::class.java) ?: "", - contractType = row.get("contractType", ContractType::class.java)!!, - lastPrice = row.get("lastPrice", BigDecimal::class.java)?.toDouble(), - ) - } + .map { row, _ -> NftMetadataResponse.fromRow(row) } .all() } diff --git a/src/test/kotlin/com/api/nft/NftTest.kt b/src/test/kotlin/com/api/nft/NftTest.kt index f080130..28bc5c2 100644 --- a/src/test/kotlin/com/api/nft/NftTest.kt +++ b/src/test/kotlin/com/api/nft/NftTest.kt @@ -22,10 +22,12 @@ import org.junit.jupiter.api.Test import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.context.SpringBootTest import org.springframework.context.ApplicationEventPublisher +import org.springframework.test.context.ActiveProfiles import reactor.test.StepVerifier import java.math.BigDecimal @SpringBootTest +@ActiveProfiles("local") class NftTest( @Autowired private val moralisApiService: MoralisApiService, @Autowired private val nftService: NftService, @@ -169,4 +171,10 @@ class NftTest( println(res.toString()) } + @Test + fun test1() { + val nft = nftRepository.findByNftJoinMetadata(10L).block() + println(nft.toString()) + } + } \ No newline at end of file