Skip to content

Commit

Permalink
add auctionExchange
Browse files Browse the repository at this point in the history
  • Loading branch information
min-96 committed Jul 28, 2024
1 parent 60b44aa commit cc747bf
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/main/kotlin/com/api/nft/config/RabbitConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ class RabbitConfig {
fun listingExchange() = createFanoutExchange("listingExchange")


// @Bean
// fun listingCancelExchange() = createFanoutExchange("listingCancelExchange")
@Bean
fun auctionExchange() = createFanoutExchange("auctionExchange")
}
19 changes: 19 additions & 0 deletions src/main/kotlin/com/api/nft/domain/nft/NftAuction.kt
Original file line number Diff line number Diff line change
@@ -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,

) {
}
Original file line number Diff line number Diff line change
@@ -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<NftAuction,Long> {
fun findByNftId(nftId: Long) : Mono<NftAuction>

@Query("UPDATE nft_auction SET status_type = :statusType WHERE nft_id = :nftId")
fun updateAuction(nftId: Long, statusType: StatusType): Mono<Void>

fun deleteByNftId(nftId: Long): Mono<Void>

}
2 changes: 1 addition & 1 deletion src/main/kotlin/com/api/nft/enums/Enums.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ enum class TokenType {
SAND, MATIC, ETH, BTC
}

enum class StatusType { RESERVATION, LISTING, RESERVATION_CANCEL, CANCEL, EXPIRED }
enum class StatusType { RESERVATION, ACTIVED, RESERVATION_CANCEL, CANCEL, EXPIRED,LISTING, AUCTION }
17 changes: 10 additions & 7 deletions src/main/kotlin/com/api/nft/rabbitMQ/RabbitMQReceiver.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
) {


Expand All @@ -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()
}
}
48 changes: 48 additions & 0 deletions src/main/kotlin/com/api/nft/service/api/NftAuctionService.kt
Original file line number Diff line number Diff line change
@@ -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<Void> {
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<Void> {
return nftAuctionRepository.save(
NftAuction(
nftId = auction.nftId,
startingPrice = auction.startingPrice,
tokenType = auction.tokenType,
statusType = auction.statusType,
createdDate = auction.createdDateTime,
endDate = auction.endDateTime,
)
).then()
}
}
7 changes: 3 additions & 4 deletions src/main/kotlin/com/api/nft/service/api/NftListingService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand All @@ -37,6 +35,7 @@ class NftListingService(
}
.then(redisService.updateToRedis(newListing.nftId))
}
else -> Mono.empty()
}
}
fun save(listing: ListingResponse) : Mono<NftListing> {
Expand Down
16 changes: 16 additions & 0 deletions src/main/kotlin/com/api/nft/service/dto/AuctionResponse.kt
Original file line number Diff line number Diff line change
@@ -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
)
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ CREATE TYPE token_type AS ENUM (

CREATE TYPE status_type AS ENUM (
'RESERVATION',
'LISTING'
'LISTING',
'AUCTION'
);


Expand Down Expand Up @@ -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
);





0 comments on commit cc747bf

Please sign in to comment.