From c93a759454825cd218cf258738254792d9d51037 Mon Sep 17 00:00:00 2001 From: Junghoon Kim Date: Sun, 2 Feb 2025 12:25:22 +0900 Subject: [PATCH] :wrench: added auction participant list --- .../auction/controller/AuctionController.kt | 12 ++++++++++ .../auction/persistence/AuctionEntity.kt | 4 ++++ .../persistence/AuctionParticipantEntity.kt | 22 +++++++++++++++++++ .../AuctionParticipantRepository.kt | 18 +++++++++++++++ .../karrot/auction/service/AuctionService.kt | 22 +++++++++++++++++++ .../karrot/user/persistence/UserEntity.kt | 6 +++++ 6 files changed, 84 insertions(+) create mode 100644 src/main/kotlin/com/toyProject7/karrot/auction/persistence/AuctionParticipantEntity.kt create mode 100644 src/main/kotlin/com/toyProject7/karrot/auction/persistence/AuctionParticipantRepository.kt diff --git a/src/main/kotlin/com/toyProject7/karrot/auction/controller/AuctionController.kt b/src/main/kotlin/com/toyProject7/karrot/auction/controller/AuctionController.kt index 13fefa0..ca14f36 100644 --- a/src/main/kotlin/com/toyProject7/karrot/auction/controller/AuctionController.kt +++ b/src/main/kotlin/com/toyProject7/karrot/auction/controller/AuctionController.kt @@ -87,6 +87,18 @@ class AuctionController( } return ResponseEntity.ok(response) } + + @GetMapping("/mypage/auctions") + fun getAuctions( + @AuthUser user: User, + ): ResponseEntity> { + val auctions: List = auctionService.getAuctionsParticipatedByUserNickname(user.nickname) + val response: List = + auctions.map { auction -> + AuctionItem.fromAuction(Auction.fromEntity(auction)) + } + return ResponseEntity.ok(response) + } } data class PostAuctionRequest( diff --git a/src/main/kotlin/com/toyProject7/karrot/auction/persistence/AuctionEntity.kt b/src/main/kotlin/com/toyProject7/karrot/auction/persistence/AuctionEntity.kt index 43f2905..f16b8ed 100644 --- a/src/main/kotlin/com/toyProject7/karrot/auction/persistence/AuctionEntity.kt +++ b/src/main/kotlin/com/toyProject7/karrot/auction/persistence/AuctionEntity.kt @@ -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 @@ -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 = mutableListOf(), ) diff --git a/src/main/kotlin/com/toyProject7/karrot/auction/persistence/AuctionParticipantEntity.kt b/src/main/kotlin/com/toyProject7/karrot/auction/persistence/AuctionParticipantEntity.kt new file mode 100644 index 0000000..4454a84 --- /dev/null +++ b/src/main/kotlin/com/toyProject7/karrot/auction/persistence/AuctionParticipantEntity.kt @@ -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, +) diff --git a/src/main/kotlin/com/toyProject7/karrot/auction/persistence/AuctionParticipantRepository.kt b/src/main/kotlin/com/toyProject7/karrot/auction/persistence/AuctionParticipantRepository.kt new file mode 100644 index 0000000..f0984ad --- /dev/null +++ b/src/main/kotlin/com/toyProject7/karrot/auction/persistence/AuctionParticipantRepository.kt @@ -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 { + @Query("SELECT ap FROM auction_participant ap JOIN ap.user u WHERE u.nickname = :nickname") + fun findByUserNickname( + @Param("nickname") nickname: String, + ): List + + fun findByUserAndAuction( + user: UserEntity, + auction: AuctionEntity, + ): List +} diff --git a/src/main/kotlin/com/toyProject7/karrot/auction/service/AuctionService.kt b/src/main/kotlin/com/toyProject7/karrot/auction/service/AuctionService.kt index 5a4a00f..0afcd0e 100644 --- a/src/main/kotlin/com/toyProject7/karrot/auction/service/AuctionService.kt +++ b/src/main/kotlin/com/toyProject7/karrot/auction/service/AuctionService.kt @@ -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 @@ -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 { @@ -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) } @@ -245,4 +262,9 @@ class AuctionService( ) } } + + fun getAuctionsParticipatedByUserNickname(nickname: String): List { + return participantRepository.findByUserNickname(nickname) + .map { it.auction } + } } diff --git a/src/main/kotlin/com/toyProject7/karrot/user/persistence/UserEntity.kt b/src/main/kotlin/com/toyProject7/karrot/user/persistence/UserEntity.kt index ee890bf..c234689 100644 --- a/src/main/kotlin/com/toyProject7/karrot/user/persistence/UserEntity.kt +++ b/src/main/kotlin/com/toyProject7/karrot/user/persistence/UserEntity.kt @@ -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 @@ -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 = mutableListOf(), )