Skip to content

Commit

Permalink
fix: add collectionLogo to NftMetadataResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
min-96 committed Sep 1, 2024
1 parent a070b76 commit 1c4baa7
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 34 deletions.
5 changes: 4 additions & 1 deletion src/main/kotlin/com/api/nft/config/RedisConfig.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -43,7 +46,7 @@ class RedisConfig {
.autoReconnect(true)
.pingBeforeActivateConnection(true)
.build()
)
)
.build()
redisClusterConfiguration.password = RedisPassword.of("bitnami")
redisClusterConfiguration.maxRedirects = 3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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) ?: ""
)
}
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -22,32 +19,23 @@ 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
nft_listing nl ON n.id = nl.nft_id
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<Long>): Flux<NftMetadataResponse> {
Expand All @@ -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()
}

Expand Down
8 changes: 8 additions & 0 deletions src/test/kotlin/com/api/nft/NftTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -169,4 +171,10 @@ class NftTest(
println(res.toString())
}

@Test
fun test1() {
val nft = nftRepository.findByNftJoinMetadata(10L).block()
println(nft.toString())
}

}

0 comments on commit 1c4baa7

Please sign in to comment.