diff --git a/src/main/kotlin/com/api/nft/config/RabbitConfig.kt b/src/main/kotlin/com/api/nft/config/RabbitConfig.kt index ebcd01b..ef5b415 100644 --- a/src/main/kotlin/com/api/nft/config/RabbitConfig.kt +++ b/src/main/kotlin/com/api/nft/config/RabbitConfig.kt @@ -35,6 +35,6 @@ class RabbitConfig { fun listingExchange() = createFanoutExchange("listingExchange") - // @Bean - // fun listingCancelExchange() = createFanoutExchange("listingCancelExchange") + @Bean + fun auctionExchange() = createFanoutExchange("auctionExchange") } \ No newline at end of file diff --git a/src/main/kotlin/com/api/nft/domain/nft/NftAuction.kt b/src/main/kotlin/com/api/nft/domain/nft/NftAuction.kt new file mode 100644 index 0000000..d8eaa47 --- /dev/null +++ b/src/main/kotlin/com/api/nft/domain/nft/NftAuction.kt @@ -0,0 +1,19 @@ +package com.api.nft.domain.nft + +import com.api.nft.enums.StatusType +import com.api.nft.enums.TokenType +import org.springframework.data.relational.core.mapping.Table +import java.math.BigDecimal + +@Table("nft_auction") +data class NftAuction( + val id: Long? = null, + val startingPrice: BigDecimal, + val tokenType: TokenType, + val nftId: Long, + val statusType: StatusType, + val createdDate: Long, + val endDate: Long, + +) { +} \ No newline at end of file diff --git a/src/main/kotlin/com/api/nft/domain/nft/repository/NftAuctionRepository.kt b/src/main/kotlin/com/api/nft/domain/nft/repository/NftAuctionRepository.kt new file mode 100644 index 0000000..6545d32 --- /dev/null +++ b/src/main/kotlin/com/api/nft/domain/nft/repository/NftAuctionRepository.kt @@ -0,0 +1,17 @@ +package com.api.nft.domain.nft.repository + +import com.api.nft.domain.nft.NftAuction +import com.api.nft.enums.StatusType +import org.springframework.data.r2dbc.repository.Query +import org.springframework.data.r2dbc.repository.R2dbcRepository +import reactor.core.publisher.Mono + +interface NftAuctionRepository : R2dbcRepository { + fun findByNftId(nftId: Long) : Mono + + @Query("UPDATE nft_auction SET status_type = :statusType WHERE nft_id = :nftId") + fun updateAuction(nftId: Long, statusType: StatusType): Mono + + fun deleteByNftId(nftId: Long): Mono + +} \ No newline at end of file diff --git a/src/main/kotlin/com/api/nft/enums/Enums.kt b/src/main/kotlin/com/api/nft/enums/Enums.kt index 4574ecd..7dde924 100644 --- a/src/main/kotlin/com/api/nft/enums/Enums.kt +++ b/src/main/kotlin/com/api/nft/enums/Enums.kt @@ -25,4 +25,4 @@ enum class TokenType { SAND, MATIC, ETH, BTC } -enum class StatusType { RESERVATION, LISTING, RESERVATION_CANCEL, CANCEL, EXPIRED } \ No newline at end of file +enum class StatusType { RESERVATION, ACTIVED, RESERVATION_CANCEL, CANCEL, EXPIRED,LISTING, AUCTION } \ No newline at end of file diff --git a/src/main/kotlin/com/api/nft/rabbitMQ/RabbitMQReceiver.kt b/src/main/kotlin/com/api/nft/rabbitMQ/RabbitMQReceiver.kt index 7853e6d..3dc84bf 100644 --- a/src/main/kotlin/com/api/nft/rabbitMQ/RabbitMQReceiver.kt +++ b/src/main/kotlin/com/api/nft/rabbitMQ/RabbitMQReceiver.kt @@ -1,5 +1,6 @@ package com.api.nft.rabbitMQ +import com.api.nft.service.api.NftAuctionService import com.api.nft.service.api.NftListingService import com.api.nft.service.dto.ListingResponse import org.springframework.amqp.core.ExchangeTypes @@ -8,10 +9,12 @@ import org.springframework.amqp.rabbit.annotation.Queue import org.springframework.amqp.rabbit.annotation.QueueBinding import org.springframework.amqp.rabbit.annotation.RabbitListener import org.springframework.stereotype.Service +import com.api.nft.service.dto.AuctionResponse as AuctionResponse @Service class RabbitMQReceiver( private val nftListingService: NftListingService, + private val nftAuctionService: NftAuctionService ) { @@ -23,11 +26,11 @@ class RabbitMQReceiver( nftListingService.update(listing).subscribe() } - // @RabbitListener(bindings = [QueueBinding( - // value = Queue(name = "", durable = "false", exclusive = "true", autoDelete = "true"), - // exchange = Exchange(value = "listingCancelExchange", type = ExchangeTypes.FANOUT) - // )]) - // fun listingCancelMessage(listing: ListingResponse){ - // nftListingService.update(listing).subscribe() - // } + @RabbitListener(bindings = [QueueBinding( + value = Queue(name = "", durable = "false", exclusive = "true", autoDelete = "true"), + exchange = Exchange(value = "auctionExchange", type = ExchangeTypes.FANOUT) + )]) + fun auctionMessage(auction: AuctionResponse){ + nftAuctionService.update(auction).subscribe() + } } \ No newline at end of file diff --git a/src/main/kotlin/com/api/nft/service/api/NftAuctionService.kt b/src/main/kotlin/com/api/nft/service/api/NftAuctionService.kt new file mode 100644 index 0000000..ef214c8 --- /dev/null +++ b/src/main/kotlin/com/api/nft/service/api/NftAuctionService.kt @@ -0,0 +1,48 @@ +package com.api.nft.service.api + +import com.api.nft.domain.nft.NftAuction +import com.api.nft.domain.nft.repository.NftAuctionRepository +import com.api.nft.enums.StatusType +import com.api.nft.service.dto.AuctionResponse +import org.springframework.stereotype.Service +import reactor.core.publisher.Mono + +@Service +class NftAuctionService( + private val nftAuctionRepository: NftAuctionRepository, +) { + + fun update(newAuction: AuctionResponse): Mono { + return when (newAuction.statusType) { + StatusType.RESERVATION -> { + save(newAuction) + } + StatusType.ACTIVED -> { + nftAuctionRepository.findByNftId(newAuction.nftId) + .flatMap { + nftAuctionRepository.updateAuction(nftId = newAuction.nftId, statusType = StatusType.AUCTION) + } + } + StatusType.RESERVATION_CANCEL, StatusType.CANCEL, StatusType.EXPIRED -> { + nftAuctionRepository.findByNftId(newAuction.nftId) + .flatMap { nftAuction -> + nftAuctionRepository.deleteByNftId(nftAuction.nftId) + } + + } + else -> Mono.empty() + } + } + fun save(auction: AuctionResponse) : Mono { + return nftAuctionRepository.save( + NftAuction( + nftId = auction.nftId, + startingPrice = auction.startingPrice, + tokenType = auction.tokenType, + statusType = auction.statusType, + createdDate = auction.createdDateTime, + endDate = auction.endDateTime, + ) + ).then() + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/api/nft/service/api/NftListingService.kt b/src/main/kotlin/com/api/nft/service/api/NftListingService.kt index 28f621e..3184e30 100644 --- a/src/main/kotlin/com/api/nft/service/api/NftListingService.kt +++ b/src/main/kotlin/com/api/nft/service/api/NftListingService.kt @@ -20,13 +20,11 @@ class NftListingService( save(newListing) .then(redisService.updateToRedis(newListing.nftId)) } - StatusType.LISTING -> { - println("update type LISTING") + StatusType.ACTIVED -> { nftListingRepository.findByNftId(newListing.nftId) .flatMap { nftListing -> - println("id : " + newListing.nftId) println("statusType : " + newListing.statusType) - nftListingRepository.updateListing(nftId = nftListing.nftId, statusType = newListing.statusType) + nftListingRepository.updateListing(nftId = nftListing.nftId, statusType = StatusType.LISTING) } .then(redisService.updateToRedis(newListing.nftId)) } @@ -37,6 +35,7 @@ class NftListingService( } .then(redisService.updateToRedis(newListing.nftId)) } + else -> Mono.empty() } } fun save(listing: ListingResponse) : Mono { diff --git a/src/main/kotlin/com/api/nft/service/dto/AuctionResponse.kt b/src/main/kotlin/com/api/nft/service/dto/AuctionResponse.kt new file mode 100644 index 0000000..5023096 --- /dev/null +++ b/src/main/kotlin/com/api/nft/service/dto/AuctionResponse.kt @@ -0,0 +1,16 @@ +package com.api.nft.service.dto + +import com.api.nft.enums.StatusType +import com.api.nft.enums.TokenType +import java.math.BigDecimal + +data class AuctionResponse( + val id : Long, + val nftId : Long, + val address: String, + val createdDateTime: Long, + val endDateTime: Long, + val statusType: StatusType, + val startingPrice: BigDecimal, + val tokenType: TokenType +) diff --git a/src/main/resources/db/postgresql/migration/V1__Initial_schema.sql b/src/main/resources/db/postgresql/migration/V1__Initial_schema.sql index f5fcf39..f48d9f2 100644 --- a/src/main/resources/db/postgresql/migration/V1__Initial_schema.sql +++ b/src/main/resources/db/postgresql/migration/V1__Initial_schema.sql @@ -23,7 +23,8 @@ CREATE TYPE token_type AS ENUM ( CREATE TYPE status_type AS ENUM ( 'RESERVATION', - 'LISTING' + 'LISTING', + 'AUCTION' ); @@ -85,6 +86,17 @@ CREATE TABLE IF NOT EXISTS nft_listing ( ); +CREATE TABLE IF NOT EXISTS nft_auction ( + id SERIAL PRIMARY KEY, + nft_id BIGINT REFERENCES nft(id), + starting_price DECIMAL(19, 4) NOT NULL, + token_type token_type, + status_type status_type not null, + created_date BIGINT, + end_date BIGINT +); + +