-
Notifications
You must be signed in to change notification settings - Fork 1
/
GetRestaurantService.kt
61 lines (53 loc) · 2.32 KB
/
GetRestaurantService.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.restaurant.be.restaurant.presentation.domain.service
import com.restaurant.be.common.exception.NotFoundRestaurantException
import com.restaurant.be.common.exception.NotFoundUserEmailException
import com.restaurant.be.common.redis.RedisRepository
import com.restaurant.be.restaurant.presentation.dto.GetRestaurantResponse
import com.restaurant.be.restaurant.presentation.dto.GetRestaurantsRequest
import com.restaurant.be.restaurant.presentation.dto.GetRestaurantsResponse
import com.restaurant.be.restaurant.repository.RestaurantEsRepository
import com.restaurant.be.restaurant.repository.RestaurantRepository
import com.restaurant.be.user.repository.UserRepository
import org.springframework.data.domain.PageImpl
import org.springframework.data.domain.Pageable
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
@Service
class GetRestaurantService(
private val restaurantEsRepository: RestaurantEsRepository,
private val redisRepository: RedisRepository,
private val userRepository: UserRepository,
private val restaurantRepository: RestaurantRepository
) {
@Transactional(readOnly = true)
fun getRestaurants(
request: GetRestaurantsRequest,
pageable: Pageable,
email: String
): GetRestaurantsResponse {
val user = userRepository.findByEmail(email) ?: throw NotFoundUserEmailException()
val restaurants = restaurantEsRepository.searchRestaurants(request, pageable)
if (!request.query.isNullOrEmpty()) {
redisRepository.addSearchQuery(user.id ?: 0, request.query)
}
val restaurantProjections = restaurantRepository.findDtoByIds(
restaurants.map { it.id },
user.id ?: 0,
request.like,
pageable
)
return GetRestaurantsResponse(
PageImpl(
restaurantProjections.content.map { it.toDto() },
pageable,
restaurantProjections.size.toLong()
)
)
}
@Transactional(readOnly = true)
fun getRestaurant(restaurantId: Long, email: String): GetRestaurantResponse {
val restaurant = restaurantRepository.findDtoById(restaurantId)
?: throw NotFoundRestaurantException()
return GetRestaurantResponse(restaurant.toDto())
}
}