Skip to content

Commit

Permalink
✨ Practice(#6) : RestaurantRepository 설정
Browse files Browse the repository at this point in the history
  • Loading branch information
Ssamssamukja committed Nov 7, 2024
1 parent 16cbf1f commit 26ca5cd
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package umc.spring.repository.restaurantRepository;

import org.springframework.data.jpa.repository.JpaRepository;
import umc.spring.domain.Restaurant;

public interface RestaurantRepository extends JpaRepository<Restaurant, Long>, RestaurantRepositoryCustom {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package umc.spring.repository.restaurantRepository;

import umc.spring.domain.Restaurant;

import java.util.List;

public interface RestaurantRepositoryCustom {
List<Restaurant> dynamicQueryWithBooleanBuilder(String name, Float score);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package umc.spring.repository.restaurantRepository;

import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;
import umc.spring.domain.QRestaurant;
import umc.spring.domain.Restaurant;


import java.util.List;

@Repository
@RequiredArgsConstructor
public class RestaurantRepositoryImpl implements RestaurantRepositoryCustom{
private final JPAQueryFactory jpaQueryFactory;
private final QRestaurant restaurant = QRestaurant.restaurant;

@Override
public List<Restaurant> dynamicQueryWithBooleanBuilder(String name, Float score) {
BooleanBuilder predicate = new BooleanBuilder();

if (name != null) {
predicate.and(restaurant.name.eq(name));
}

if (score != null) {
predicate.and(restaurant.score.goe(4.0f));
}

return jpaQueryFactory
.selectFrom(restaurant)
.where(predicate)
.fetch();
}
}

0 comments on commit 26ca5cd

Please sign in to comment.