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

[KAN-101] chat gpt 기반 음식점 추천 (오늘의픽 API) #71

Merged
merged 1 commit into from
May 26, 2024
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
19 changes: 19 additions & 0 deletions src/main/kotlin/com/restaurant/be/common/redis/RedisRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,29 @@ class RedisRepository(
companion object {
private const val SEARCH_PREFIX = "SR:" // 검색어를 저장할 때 사용할 키 접두사
private const val MAX_HISTORY = 5 // 저장할 최대 검색어 수
private const val RECOMMENDATION_PREFIX = "RECOMMENDATION:"
}

val REFRESH_PREFIX: String = "RT:"

// 사용자별 추천 식당을 조회하는 메서드
fun getRecommendation(userId: Long): List<Long> {
val values = redisTemplate.opsForValue()
val key = "$RECOMMENDATION_PREFIX$userId"
val recommendations = values.get(key)
if (recommendations != null) {
return recommendations.split(",").map { it.toLong() }
}

val defaultKey = RECOMMENDATION_PREFIX + "0"
val defaultRecommendations = values.get(defaultKey)
if (defaultRecommendations != null) {
return defaultRecommendations.split(",").map { it.toLong() }
}

return emptyList()
}

// 사용자별 검색어를 추가하는 메서드
fun addSearchQuery(userId: Long, query: String) {
val key = "$SEARCH_PREFIX$userId" // 사용자별 고유 키 생성
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.restaurant.be.restaurant.domain.service

import com.restaurant.be.common.exception.NotFoundUserException
import com.restaurant.be.common.redis.RedisRepository
import com.restaurant.be.restaurant.presentation.controller.dto.RecommendRestaurantResponse
import com.restaurant.be.restaurant.repository.RestaurantRepository
import com.restaurant.be.user.repository.UserRepository
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
class RecommendRestaurantService(
private val redisRepository: RedisRepository,
private val restaurantRepository: RestaurantRepository,
private val userRepository: UserRepository
) {

@Transactional(readOnly = true)
fun recommendRestaurants(email: String): RecommendRestaurantResponse {
val userId = userRepository.findByEmail(email)?.id ?: throw NotFoundUserException()

val restaurantIds = redisRepository.getRecommendation(userId)

val restaurantDtos = restaurantRepository.findDtoByIds(restaurantIds, userId)

val randomRestaurantDtos = if (restaurantDtos.size > 5) {
restaurantDtos.shuffled().take(5)
} else {
restaurantDtos
}

return RecommendRestaurantResponse(
restaurants = randomRestaurantDtos.map { it.toDto() }
)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.restaurant.be.restaurant.presentation.controller

import com.restaurant.be.common.response.CommonResponse
import com.restaurant.be.restaurant.domain.service.RecommendRestaurantService
import com.restaurant.be.restaurant.presentation.controller.dto.RecommendRestaurantResponse
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
Expand All @@ -16,7 +17,9 @@ import java.security.Principal
@Api(tags = ["02. Restaurant Info"], description = "음식점 서비스")
@RestController
@RequestMapping("/v1/restaurants")
class RecommendRestaurantController {
class RecommendRestaurantController(
private val recommendRestaurantService: RecommendRestaurantService
) {

@GetMapping("recommend")
@PreAuthorize("hasRole('USER')")
Expand All @@ -29,6 +32,7 @@ class RecommendRestaurantController {
fun getRecommendRestaurants(
principal: Principal
): CommonResponse<RecommendRestaurantResponse> {
return CommonResponse.success()
val response = recommendRestaurantService.recommendRestaurants(principal.name)
return CommonResponse.success(response)
}
}
Loading