diff --git a/src/main/kotlin/com/toyProject7/karrot/article/persistence/ArticleRepository.kt b/src/main/kotlin/com/toyProject7/karrot/article/persistence/ArticleRepository.kt index f9aaaad..f266368 100644 --- a/src/main/kotlin/com/toyProject7/karrot/article/persistence/ArticleRepository.kt +++ b/src/main/kotlin/com/toyProject7/karrot/article/persistence/ArticleRepository.kt @@ -4,6 +4,7 @@ import com.toyProject7.karrot.user.persistence.UserEntity import org.springframework.data.jpa.repository.JpaRepository import org.springframework.data.jpa.repository.Modifying import org.springframework.data.jpa.repository.Query +import org.springframework.data.repository.query.Param interface ArticleRepository : JpaRepository { fun findTop10ByIdBeforeAndIsDummyOrderByIdDesc( @@ -32,4 +33,18 @@ interface ArticleRepository : JpaRepository { content: String, id: Long, ): List + + @Modifying + @Query("UPDATE articles a SET a.buyer = :buyer WHERE a.id = :articleId") + fun updateBuyer( + @Param("articleId") articleId: Long, + @Param("buyer") buyer: UserEntity, + ) + + @Modifying + @Query("UPDATE articles a SET a.status = :status WHERE a.id = :articleId") + fun updateStatus( + @Param("articleId") articleId: Long, + @Param("status") status: Int, + ) } diff --git a/src/main/kotlin/com/toyProject7/karrot/article/service/ArticleService.kt b/src/main/kotlin/com/toyProject7/karrot/article/service/ArticleService.kt index db60072..9a9c8c4 100644 --- a/src/main/kotlin/com/toyProject7/karrot/article/service/ArticleService.kt +++ b/src/main/kotlin/com/toyProject7/karrot/article/service/ArticleService.kt @@ -20,7 +20,6 @@ import com.toyProject7.karrot.user.service.UserService import org.springframework.context.annotation.Lazy import org.springframework.data.repository.findByIdOrNull import org.springframework.stereotype.Service -import org.springframework.transaction.annotation.Propagation import org.springframework.transaction.annotation.Transactional import java.time.Instant import java.time.temporal.ChronoUnit @@ -135,7 +134,7 @@ class ArticleService( articleRepository.delete(articleEntity) } - @Transactional(propagation = Propagation.REQUIRES_NEW) + @Transactional fun updateStatus( request: UpdateStatusRequest, articleId: Long, @@ -143,7 +142,7 @@ class ArticleService( ) { val articleEntity = getArticleEntityById(articleId) if (articleEntity.seller.id != id) throw ArticlePermissionDeniedException() - articleEntity.status = request.status + articleRepository.updateStatus(articleId, request.status) } @Transactional @@ -269,13 +268,12 @@ class ArticleService( return articles } - @Transactional(propagation = Propagation.REQUIRES_NEW) + @Transactional fun updateBuyer( articleId: Long, buyerId: String, ) { - val articleEntity = articleRepository.findByIdOrNull(articleId) ?: throw ArticleNotFoundException() - articleEntity.buyer = userService.getUserEntityById(buyerId) + articleRepository.updateBuyer(articleId, userService.getUserEntityById(buyerId)) } @Transactional