Skip to content

Commit

Permalink
🔧 added auction participant list
Browse files Browse the repository at this point in the history
  • Loading branch information
jafacode committed Feb 2, 2025
1 parent 7fecfbb commit c93a759
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ class AuctionController(
}
return ResponseEntity.ok(response)
}

@GetMapping("/mypage/auctions")
fun getAuctions(
@AuthUser user: User,
): ResponseEntity<List<AuctionItem>> {
val auctions: List<AuctionEntity> = auctionService.getAuctionsParticipatedByUserNickname(user.nickname)
val response: List<AuctionItem> =
auctions.map { auction ->
AuctionItem.fromAuction(Auction.fromEntity(auction))
}
return ResponseEntity.ok(response)
}
}

data class PostAuctionRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package com.toyProject7.karrot.auction.persistence

import com.toyProject7.karrot.image.persistence.ImageUrlEntity
import com.toyProject7.karrot.user.persistence.UserEntity
import jakarta.persistence.CascadeType
import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.FetchType
import jakarta.persistence.GeneratedValue
import jakarta.persistence.GenerationType
import jakarta.persistence.Id
Expand Down Expand Up @@ -49,4 +51,6 @@ class AuctionEntity(
var updatedAt: Instant,
@Column(name = "view_count")
var viewCount: Int,
@OneToMany(mappedBy = "auction", cascade = [CascadeType.ALL], orphanRemoval = true, fetch = FetchType.LAZY)
var participations: MutableList<AuctionParticipantEntity> = mutableListOf(),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.toyProject7.karrot.auction.persistence

import com.toyProject7.karrot.user.persistence.UserEntity
import jakarta.persistence.Entity
import jakarta.persistence.GeneratedValue
import jakarta.persistence.GenerationType
import jakarta.persistence.Id
import jakarta.persistence.JoinColumn
import jakarta.persistence.ManyToOne

@Entity(name = "auction_participant")
data class AuctionParticipantEntity(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long = 0,
@ManyToOne
@JoinColumn(name = "user_id", nullable = false)
val user: UserEntity,
@ManyToOne
@JoinColumn(name = "auction_id", nullable = false)
val auction: AuctionEntity,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.toyProject7.karrot.auction.persistence

import com.toyProject7.karrot.user.persistence.UserEntity
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.query.Param

interface AuctionParticipantRepository : JpaRepository<AuctionParticipantEntity, String> {
@Query("SELECT ap FROM auction_participant ap JOIN ap.user u WHERE u.nickname = :nickname")
fun findByUserNickname(
@Param("nickname") nickname: String,
): List<AuctionParticipantEntity>

fun findByUserAndAuction(
user: UserEntity,
auction: AuctionEntity,
): List<AuctionParticipantEntity>
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import com.toyProject7.karrot.auction.controller.PostAuctionRequest
import com.toyProject7.karrot.auction.persistence.AuctionEntity
import com.toyProject7.karrot.auction.persistence.AuctionLikesEntity
import com.toyProject7.karrot.auction.persistence.AuctionLikesRepository
import com.toyProject7.karrot.auction.persistence.AuctionParticipantEntity
import com.toyProject7.karrot.auction.persistence.AuctionParticipantRepository
import com.toyProject7.karrot.auction.persistence.AuctionRepository
import com.toyProject7.karrot.chatRoom.service.ChatRoomService
import com.toyProject7.karrot.image.persistence.ImageUrlEntity
Expand All @@ -36,6 +38,7 @@ class AuctionService(
@Lazy private val imageService: ImageService,
private val articleRepository: ArticleRepository,
private val chatRoomService: ChatRoomService,
private val participantRepository: AuctionParticipantRepository,
) {
@Transactional
fun updatePrice(auctionMessage: AuctionMessage): AuctionMessage {
Expand All @@ -53,6 +56,20 @@ class AuctionService(

auctionEntity.currentPrice = auctionMessage.price
auctionEntity.bidder = bidder

val existingParticipant = participantRepository.findByUserAndAuction(bidder, auctionEntity)
if (existingParticipant != null) {
throw IllegalStateException("User is already participating in this auction")
}

val participation =
AuctionParticipantEntity(
user = bidder,
auction = auctionEntity,
)

participantRepository.save(participation)

if (ChronoUnit.SECONDS.between(Instant.now(), auctionEntity.endTime) <= 60) {
auctionEntity.endTime = Instant.now().plus(1, ChronoUnit.MINUTES)
}
Expand Down Expand Up @@ -245,4 +262,9 @@ class AuctionService(
)
}
}

fun getAuctionsParticipatedByUserNickname(nickname: String): List<AuctionEntity> {
return participantRepository.findByUserNickname(nickname)
.map { it.auction }
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package com.toyProject7.karrot.user.persistence

import com.toyProject7.karrot.auction.persistence.AuctionParticipantEntity
import com.toyProject7.karrot.image.persistence.ImageUrlEntity
import jakarta.persistence.CascadeType
import jakarta.persistence.Column
import jakarta.persistence.DiscriminatorColumn
import jakarta.persistence.Entity
import jakarta.persistence.FetchType
import jakarta.persistence.GeneratedValue
import jakarta.persistence.GenerationType
import jakarta.persistence.Id
import jakarta.persistence.Inheritance
import jakarta.persistence.InheritanceType
import jakarta.persistence.JoinColumn
import jakarta.persistence.OneToMany
import jakarta.persistence.OneToOne
import jakarta.persistence.Table
import java.time.Instant
Expand All @@ -35,4 +39,6 @@ class UserEntity(
var imageUrl: ImageUrlEntity? = null,
@Column(name = "updated_at", nullable = false)
var updatedAt: Instant,
@OneToMany(mappedBy = "user", cascade = [CascadeType.ALL], orphanRemoval = true, fetch = FetchType.LAZY)
val participations: MutableList<AuctionParticipantEntity> = mutableListOf(),
)

0 comments on commit c93a759

Please sign in to comment.