Skip to content

Commit

Permalink
[KAN-104] 음식점 통합테스트 추가 - 좋아요 조회, 좋아요 요청/취소 테스트 + 버그 수정 (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
sinkyoungdeok authored May 27, 2024
1 parent ce580b9 commit 4e80762
Show file tree
Hide file tree
Showing 4 changed files with 486 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import com.restaurant.be.restaurant.presentation.controller.dto.GetLikeRestauran
import com.restaurant.be.restaurant.presentation.controller.dto.LikeRestaurantResponse
import com.restaurant.be.restaurant.repository.RestaurantLikeRepository
import com.restaurant.be.restaurant.repository.RestaurantRepository
import com.restaurant.be.restaurant.repository.dto.RestaurantProjectionDto
import com.restaurant.be.user.domain.entity.User
import com.restaurant.be.user.repository.UserRepository
import org.springframework.data.domain.Pageable
import org.springframework.stereotype.Service
Expand All @@ -23,31 +21,36 @@ class LikeRestaurantService(
) {
@Transactional
fun likeRestaurant(email: String, restaurantId: Long, isLike: Boolean): LikeRestaurantResponse {
val user: User = userRepository.findByEmail(email) ?: throw NotFoundUserEmailException()
val userId: Long = user.id ?: throw NotFoundUserException()
val userId: Long = userRepository.findByEmail(email)?.id ?: throw NotFoundUserException()

val restaurant: RestaurantProjectionDto = restaurantRepository.findDtoById(restaurantId)
val restaurantDto = restaurantRepository.findDtoById(restaurantId)
?: throw NotFoundRestaurantException()

// 좋아요 요청
val restaurant = restaurantRepository.findById(restaurantId)
.orElseThrow { NotFoundRestaurantException() }

if (isLike) {
// 실제 좋아요가 아닐 시 Insert
if (!restaurant.isLike) {
if (!restaurantDto.isLike) {
restaurantLikeRepository.save(
RestaurantLike(
restaurantId = restaurantId,
userId = userId
)
)
restaurant.likeCount += 1
}
} else {
restaurantLikeRepository.deleteByUserIdAndRestaurantId(userId, restaurantId)
if (restaurantDto.isLike) {
restaurant.likeCount -= 1
restaurantLikeRepository.deleteByUserIdAndRestaurantId(userId, restaurantId)
}
}

restaurantRepository.save(restaurant)
return LikeRestaurantResponse(restaurantRepository.findDtoById(restaurantId)!!.toDto())
}

@Transactional
@Transactional(readOnly = true)
fun getMyLikeRestaurant(pageable: Pageable, email: String): GetLikeRestaurantsResponse {
val userId = userRepository.findByEmail(email)?.id ?: throw NotFoundUserEmailException()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,6 @@ class RestaurantRepositoryCustomImpl(
val total = myLikeQuery.fetchCount()

val restaurantIds = myLikeQuery
.offset(pageable.offset)
.limit(pageable.pageSize.toLong())
.fetch()

val restaurantInfos = queryFactory
Expand All @@ -156,6 +154,8 @@ class RestaurantRepositoryCustomImpl(
.leftJoin(restaurantLike).on(restaurant.id.eq(restaurantLike.restaurantId))
.orderBy(*orderSpecifier.toTypedArray())
.fetchJoin()
.offset(pageable.offset)
.limit(pageable.pageSize.toLong())
.fetch()

val menus = queryFactory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1737,7 +1737,7 @@ class GetRestaurantControllerTest(
actualResult.data!!.restaurants.content[0].name shouldBe "목구멍 율전점1"
}

it("when 2 data and set size 1 page 2 should return 2's restaurant") {
it("when 2 data and set size 1 page 1 should return 2's restaurant") {
// given
val restaurantEntity1 = RestaurantUtil.generateRestaurantEntity(
name = "목구멍 율전점1"
Expand Down
Loading

0 comments on commit 4e80762

Please sign in to comment.