Skip to content

Commit 84d6c61

Browse files
authored
Merge pull request #37 from Zipbap/refactor/#35
[Refactor]Recipe ์กฐํšŒ์ˆ˜ ๋ฐ˜์˜ ๋กœ์ง ๊ฐœ์„ 
2 parents 9a72562 + 653b58a commit 84d6c61

5 files changed

Lines changed: 42 additions & 8 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package zipbap.user.api.recipe.event
2+
3+
import org.springframework.stereotype.Component
4+
import org.springframework.transaction.annotation.Propagation
5+
import org.springframework.transaction.annotation.Transactional
6+
import org.springframework.transaction.event.TransactionPhase
7+
import org.springframework.transaction.event.TransactionalEventListener
8+
import zipbap.global.domain.recipe.RecipeRepository
9+
10+
@Component
11+
class RecipeViewCountListener(
12+
private val recipeRepository: RecipeRepository
13+
) {
14+
15+
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
16+
@Transactional(propagation = Propagation.REQUIRES_NEW)
17+
fun onRecipeViewed(event: RecipeViewedEvent) {
18+
recipeRepository.updateViewCount(event.recipeId)
19+
}
20+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package zipbap.user.api.recipe.event
2+
3+
data class RecipeViewedEvent(
4+
val recipeId: String,
5+
val viewerUserId: Long
6+
)

โ€Žapi-user/src/main/kotlin/zipbap/user/api/recipe/service/RecipeService.ktโ€Ž

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package zipbap.user.api.recipe.service
22

3+
import org.springframework.context.ApplicationEventPublisher
34
import org.springframework.stereotype.Service
45
import org.springframework.transaction.annotation.Transactional
56
import zipbap.user.api.recipe.converter.RecipeConverter
@@ -17,14 +18,16 @@ import zipbap.global.domain.user.UserRepository
1718
import zipbap.global.global.util.CustomIdGenerator
1819
import zipbap.global.global.exception.GeneralException
1920
import zipbap.global.global.code.status.ErrorStatus
21+
import zipbap.user.api.recipe.event.RecipeViewedEvent
2022

2123
@Service
2224
class RecipeService(
2325
private val recipeRepository: RecipeRepository,
2426
private val cookingOrderRepository: CookingOrderRepository,
2527
private val userRepository: UserRepository,
2628
private val categoryValidator: CategoryValidator,
27-
private val recipeFileService: RecipeFileService
29+
private val recipeFileService: RecipeFileService,
30+
private val publisher: ApplicationEventPublisher
2831
) {
2932

3033
/**
@@ -206,12 +209,10 @@ class RecipeService(
206209
throw GeneralException(ErrorStatus.RECIPE_NOT_FOUND)
207210
}
208211

209-
// ์กฐํšŒ์ˆ˜ ์ฆ๊ฐ€
210-
recipe.viewCount += 1
211-
val savedRecipe = recipeRepository.save(recipe)
212-
213212
val orders = cookingOrderRepository.findAllByRecipeId(recipe.id)
214-
return RecipeConverter.toDto(savedRecipe, orders)
213+
214+
publisher.publishEvent(RecipeViewedEvent(recipeId, userId))
215+
return RecipeConverter.toDto(recipe, orders)
215216
}
216217

217218
/**

โ€Žglobal/build.gradle.ktsโ€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ dependencyManagement {
4545
// โš ๏ธ JDBC ๋“œ๋ผ์ด๋ฒ„๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ชจ๋“ˆ(api-*)์— ๋‘๋Š” ๊ฑธ ๊ถŒ์žฅ. (์—ฌ๊ธฐ์„  ๋บŒ)
4646

4747
// Querydsl (APT๋Š” ์—ฌ๊ธฐ์„œ!)
48-
implementation("com.querydsl:querydsl-jpa:5.0.0:jakarta")
49-
kapt("com.querydsl:querydsl-apt:5.0.0:jakarta")
48+
implementation("com.querydsl:querydsl-jpa:5.1.0:jakarta")
49+
kapt("com.querydsl:querydsl-apt:5.1.0:jakarta")
5050
kapt("jakarta.annotation:jakarta.annotation-api")
5151
kapt("jakarta.persistence:jakarta.persistence-api")
5252

โ€Žglobal/src/main/kotlin/zipbap/global/domain/recipe/RecipeRepository.ktโ€Ž

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package zipbap.global.domain.recipe
22

33
import org.springframework.data.jpa.repository.JpaRepository
4+
import org.springframework.data.jpa.repository.Modifying
45
import org.springframework.data.jpa.repository.Query
56
import org.springframework.data.repository.query.Param
67

@@ -35,4 +36,10 @@ interface RecipeRepository : JpaRepository<Recipe, String> {
3536

3637
// โœ… ์—ฌ๊ธฐ๋งŒ ๋‚จ๊ธฐ๋ฉด ๋จ
3738
fun findAllByMyCategoryId(myCategoryId: String): List<Recipe>
39+
40+
@Modifying(clearAutomatically = true, flushAutomatically = true)
41+
@Query("""
42+
UPDATE Recipe r SET r.viewCount = r.viewCount + 1 WHERE r.id = :recipeId
43+
""")
44+
fun updateViewCount(@Param("recipeId") recipeId: String): Unit
3845
}

0 commit comments

Comments
ย (0)