Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Live auctions are only displayed in the auctions list #96

Merged
merged 3 commits into from
Feb 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
): AuctionParticipantEntity?
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import java.time.Instant

@Repository
interface AuctionRepository : JpaRepository<AuctionEntity, Long> {
fun findTop10ByIdBeforeOrderByIdDesc(id: Long): List<AuctionEntity>
fun findTop10ByIdBeforeAndStatusOrderByIdDesc(
id: Long,
status: Int,
): List<AuctionEntity>

fun findTop10BySellerAndIdLessThanOrderByIdDesc(
seller: UserEntity,
Expand Down
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,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)
}
Expand Down Expand Up @@ -198,7 +212,7 @@ class AuctionService(

@Transactional
fun getPreviousAuctions(auctionId: Long): List<AuctionEntity> {
val auctions = auctionRepository.findTop10ByIdBeforeOrderByIdDesc(auctionId)
val auctions = auctionRepository.findTop10ByIdBeforeAndStatusOrderByIdDesc(auctionId, 0)
refreshPresignedUrlIfExpired(auctions)
return auctions
}
Expand Down Expand Up @@ -245,4 +259,10 @@ class AuctionService(
)
}
}

fun getAuctionsParticipatedByUserNickname(nickname: String): List<AuctionEntity> {
val auctions = participantRepository.findByUserNickname(nickname)
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(),
)