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..3b434c3 --- /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, + ): AuctionParticipantEntity? +} diff --git a/src/main/kotlin/com/toyProject7/karrot/auction/persistence/AuctionRepository.kt b/src/main/kotlin/com/toyProject7/karrot/auction/persistence/AuctionRepository.kt index e99f52a..a48df9e 100644 --- a/src/main/kotlin/com/toyProject7/karrot/auction/persistence/AuctionRepository.kt +++ b/src/main/kotlin/com/toyProject7/karrot/auction/persistence/AuctionRepository.kt @@ -9,7 +9,10 @@ import java.time.Instant @Repository interface AuctionRepository : JpaRepository { - fun findTop10ByIdBeforeOrderByIdDesc(id: Long): List + fun findTop10ByIdBeforeAndStatusOrderByIdDesc( + id: Long, + status: Int, + ): List fun findTop10BySellerAndIdLessThanOrderByIdDesc( seller: UserEntity, 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 4084565..3dc4763 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,17 @@ class AuctionService( auctionEntity.currentPrice = auctionMessage.price auctionEntity.bidder = bidder + + val existingParticipant = participantRepository.findByUserAndAuction(bidder, auctionEntity) + if (existingParticipant == null) { + 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) } @@ -198,7 +212,7 @@ class AuctionService( @Transactional fun getPreviousAuctions(auctionId: Long): List { - val auctions = auctionRepository.findTop10ByIdBeforeOrderByIdDesc(auctionId) + val auctions = auctionRepository.findTop10ByIdBeforeAndStatusOrderByIdDesc(auctionId, 0) refreshPresignedUrlIfExpired(auctions) return auctions } @@ -245,4 +259,10 @@ class AuctionService( ) } } + + fun getAuctionsParticipatedByUserNickname(nickname: String): List { + val auctions = participantRepository.findByUserNickname(nickname) + 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(), )