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

πŸ› Fix update article #95

Merged
merged 2 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 @@ -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 @@ -19,7 +19,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 @@ -134,15 +133,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 @@ -267,14 +266,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.save(articleEntity)
articleRepository.updateBuyer(articleId, userService.getUserEntityById(buyerId))
}

@Transactional
Expand Down