Skip to content

Commit

Permalink
🐛 Fix update article
Browse files Browse the repository at this point in the history
  • Loading branch information
wonseok committed Feb 2, 2025
1 parent d7ccd62 commit e8b6ef0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<ArticleEntity, Long> {
fun findTop10ByIdBeforeAndIsDummyOrderByIdDesc(
Expand Down Expand Up @@ -32,4 +33,18 @@ interface ArticleRepository : JpaRepository<ArticleEntity, Long> {
content: String,
id: Long,
): List<ArticleEntity>

@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,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -135,15 +134,15 @@ class ArticleService(
articleRepository.delete(articleEntity)
}

@Transactional(propagation = Propagation.REQUIRES_NEW)
@Transactional
fun updateStatus(
request: UpdateStatusRequest,
articleId: Long,
id: String,
) {
val articleEntity = getArticleEntityById(articleId)
if (articleEntity.seller.id != id) throw ArticlePermissionDeniedException()
articleEntity.status = request.status
articleRepository.updateStatus(articleId, request.status)
}

@Transactional
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e8b6ef0

Please sign in to comment.