From 2a6cb141c603ecb0e5794098234e35cfbb0f6805 Mon Sep 17 00:00:00 2001 From: dldmsql Date: Thu, 27 Apr 2023 10:28:25 +0900 Subject: [PATCH 1/7] =?UTF-8?q?querydsl=20config=20=ED=8C=8C=EC=9D=BC?= =?UTF-8?q?=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit querydsl 사용을 위한 설정 파일을 작성 --- .../global/config/QueryDslConfig.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/main/java/com/example/myongsick/global/config/QueryDslConfig.java diff --git a/src/main/java/com/example/myongsick/global/config/QueryDslConfig.java b/src/main/java/com/example/myongsick/global/config/QueryDslConfig.java new file mode 100644 index 0000000..a61d081 --- /dev/null +++ b/src/main/java/com/example/myongsick/global/config/QueryDslConfig.java @@ -0,0 +1,19 @@ +package com.example.myongsick.global.config; + +import com.querydsl.jpa.impl.JPAQueryFactory; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class QueryDslConfig { + + @PersistenceContext + private EntityManager em; + + @Bean + public JPAQueryFactory jpaQueryFactory(EntityManager em) { + return new JPAQueryFactory(em); + } +} From a03383676a2b148a7ecf0ec072d26a0ba4064701 Mon Sep 17 00:00:00 2001 From: dldmsql Date: Thu, 27 Apr 2023 10:30:29 +0900 Subject: [PATCH 2/7] =?UTF-8?q?=EC=B0=9C=EA=BD=81=EB=A6=AC=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=9D=91=EB=8B=B5=20DTO=EB=A5=BC=20Q=EA=B0=9D?= =?UTF-8?q?=EC=B2=B4=EB=A1=9C=20=EC=82=AC=EC=9A=A9=EC=9D=84=20=EC=9C=84?= =?UTF-8?q?=ED=95=9C=20=EC=96=B4=EB=85=B8=ED=85=8C=EC=9D=B4=EC=85=98=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit @QueryProjection 어노테이션을 통한 생성자를 이용한 객체 생성 방식으로 매핑 --- .../domain/scrap/dto/ScrapCountResponse.java | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/example/myongsick/domain/scrap/dto/ScrapCountResponse.java b/src/main/java/com/example/myongsick/domain/scrap/dto/ScrapCountResponse.java index e20380e..2250d81 100644 --- a/src/main/java/com/example/myongsick/domain/scrap/dto/ScrapCountResponse.java +++ b/src/main/java/com/example/myongsick/domain/scrap/dto/ScrapCountResponse.java @@ -1,17 +1,14 @@ package com.example.myongsick.domain.scrap.dto; +import com.querydsl.core.annotations.QueryProjection; import io.swagger.annotations.ApiModel; -import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; -import lombok.Setter; @Getter -@Setter @Builder @NoArgsConstructor -@AllArgsConstructor @ApiModel(description = "찜꽁 리스트 응답 객체 ( + 찜꽁수 포함 )") public class ScrapCountResponse { @@ -20,12 +17,28 @@ public class ScrapCountResponse { private String name; private String category; private String address; + private String contact; private String urlAddress; private String distance; - private String contact; private String latitude; private String longitude; private int scrapCount; + @QueryProjection + public ScrapCountResponse(Long storeId, String code, String name, String category, String address, + String contact, String urlAddress, String distance, String latitude, String longitude, + int scrapCount) { + this.storeId = storeId; + this.code = code; + this.name = name; + this.category = category; + this.address = address; + this.contact = contact; + this.urlAddress = urlAddress; + this.distance = distance; + this.latitude = latitude; + this.longitude = longitude; + this.scrapCount = scrapCount; + } public static ScrapCountResponse toDto(CountResponse countResponse) { return ScrapCountResponse.builder() From 9c8de0283211688ce1bcc92a89a8ca985bbbbe93 Mon Sep 17 00:00:00 2001 From: dldmsql Date: Thu, 27 Apr 2023 10:31:35 +0900 Subject: [PATCH 3/7] =?UTF-8?q?=EB=A7=9B=EC=A7=91=EC=A1=B0=ED=9A=8C=20?= =?UTF-8?q?=EC=8B=9C,=20=EC=B0=9C=EA=BD=81=20=EC=88=98=20=ED=8F=AC?= =?UTF-8?q?=ED=95=A8=20=EA=B0=80=EA=B2=8C=20=EB=8D=B0=EC=9D=B4=ED=84=B0=20?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C=20=EC=BF=BC=EB=A6=AC=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 동적 정렬(인기순, 거리순) 적용 --- .../repository/ScrapRepositoryCustom.java | 10 +++ .../scrap/repository/ScrapRepositoryImpl.java | 64 +++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 src/main/java/com/example/myongsick/domain/scrap/repository/ScrapRepositoryCustom.java create mode 100644 src/main/java/com/example/myongsick/domain/scrap/repository/ScrapRepositoryImpl.java diff --git a/src/main/java/com/example/myongsick/domain/scrap/repository/ScrapRepositoryCustom.java b/src/main/java/com/example/myongsick/domain/scrap/repository/ScrapRepositoryCustom.java new file mode 100644 index 0000000..3da9624 --- /dev/null +++ b/src/main/java/com/example/myongsick/domain/scrap/repository/ScrapRepositoryCustom.java @@ -0,0 +1,10 @@ +package com.example.myongsick.domain.scrap.repository; + +import com.example.myongsick.domain.scrap.dto.ScrapCountResponse; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.repository.query.Param; + +public interface ScrapRepositoryCustom { + Page findAllByCampusWithPaging(@Param("campus") String campus, Pageable pageable); +} diff --git a/src/main/java/com/example/myongsick/domain/scrap/repository/ScrapRepositoryImpl.java b/src/main/java/com/example/myongsick/domain/scrap/repository/ScrapRepositoryImpl.java new file mode 100644 index 0000000..01747c4 --- /dev/null +++ b/src/main/java/com/example/myongsick/domain/scrap/repository/ScrapRepositoryImpl.java @@ -0,0 +1,64 @@ +package com.example.myongsick.domain.scrap.repository; + +import static com.example.myongsick.domain.scrap.entity.QScrap.scrap; +import static com.example.myongsick.domain.scrap.entity.QStore.store; + +import com.example.myongsick.domain.scrap.dto.QScrapCountResponse; +import com.example.myongsick.domain.scrap.dto.ScrapCountResponse; +import com.example.myongsick.domain.scrap.entity.CampusType; +import com.example.myongsick.domain.scrap.entity.Store; +import com.querydsl.core.types.Order; +import com.querydsl.core.types.OrderSpecifier; +import com.querydsl.core.types.dsl.PathBuilder; +import com.querydsl.jpa.impl.JPAQueryFactory; +import java.util.ArrayList; +import java.util.List; +import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; +import org.springframework.stereotype.Repository; + +@Repository +@RequiredArgsConstructor +public class ScrapRepositoryImpl implements ScrapRepositoryCustom{ + private final JPAQueryFactory jpaQueryFactory; + + @Override + public Page findAllByCampusWithPaging(String campus, Pageable pageable) { + List result = jpaQueryFactory.select(new QScrapCountResponse(store.id.as("storeId"), store.code, store.name, store.category, store.address, store.contact, store.urlAddress, store.distance, store.latitude, store.longitude, store.scrapList.size().as("scrapCount"))) + .from(store) + .where(store.campus.eq(CampusType.valueOf(campus))) + .groupBy(store.code) + .orderBy(getOrderSpecifier(pageable.getSort()).stream().toArray(OrderSpecifier[]::new)) + .offset(pageable.getOffset()) + .limit(pageable.getPageSize()) + .fetch(); + + Long count = jpaQueryFactory + .select(scrap.count()) + .from(scrap) + .fetchOne(); + + return new PageImpl<>(result, pageable, count); + } + + private List getOrderSpecifier(Sort sort) { + List orderSpecifiers = new ArrayList<>(); + sort.stream() + .forEach(order -> { + Order direction = order.isAscending() ? Order.ASC : Order.DESC; + String property = order.getProperty(); + if(property.equals("scrapCount")) { + orderSpecifiers.add(new OrderSpecifier(direction, store.scrapList.size())); + } else if(property.equals("distance")) { + orderSpecifiers.add(new OrderSpecifier(direction, store.distance.castToNum(Long.class))); + } else { + PathBuilder orderByExpression = new PathBuilder(Store.class, "store"); + orderSpecifiers.add(new OrderSpecifier(direction, orderByExpression.get(property))); + } + }); + return orderSpecifiers; + } +} From 01dbdaecf107b3c9f3d6923671e600bc7ec64d29 Mon Sep 17 00:00:00 2001 From: dldmsql Date: Thu, 27 Apr 2023 10:32:07 +0900 Subject: [PATCH 4/7] =?UTF-8?q?=EB=A7=9B=EC=A7=91=EC=A1=B0=ED=9A=8C=20?= =?UTF-8?q?=EC=8B=9C,=20=EC=B0=9C=EA=BD=81=20=EC=88=98=20=ED=8F=AC?= =?UTF-8?q?=ED=95=A8=20=EA=B0=80=EA=B2=8C=20=EB=8D=B0=EC=9D=B4=ED=84=B0=20?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C=20=EC=BF=BC=EB=A6=AC=20=ED=95=A8=EC=88=98=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 기존에 호출하던 조회 쿼리 함수는 주석으로 대체 --- .../example/myongsick/domain/scrap/service/ScrapService.java | 1 - .../myongsick/domain/scrap/service/ScrapServiceImpl.java | 5 ++++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/example/myongsick/domain/scrap/service/ScrapService.java b/src/main/java/com/example/myongsick/domain/scrap/service/ScrapService.java index fc58fbd..a2f3598 100644 --- a/src/main/java/com/example/myongsick/domain/scrap/service/ScrapService.java +++ b/src/main/java/com/example/myongsick/domain/scrap/service/ScrapService.java @@ -3,7 +3,6 @@ import com.example.myongsick.domain.scrap.dto.ScrapCountResponse; import com.example.myongsick.domain.scrap.dto.ScrapRequest; import com.example.myongsick.domain.scrap.dto.ScrapResponse; -import java.util.Optional; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; diff --git a/src/main/java/com/example/myongsick/domain/scrap/service/ScrapServiceImpl.java b/src/main/java/com/example/myongsick/domain/scrap/service/ScrapServiceImpl.java index ecbd245..474f18e 100644 --- a/src/main/java/com/example/myongsick/domain/scrap/service/ScrapServiceImpl.java +++ b/src/main/java/com/example/myongsick/domain/scrap/service/ScrapServiceImpl.java @@ -10,6 +10,7 @@ import com.example.myongsick.domain.scrap.exception.NotFoundScrapException; import com.example.myongsick.domain.scrap.exception.NotFoundStoreException; import com.example.myongsick.domain.scrap.repository.ScrapRepository; +import com.example.myongsick.domain.scrap.repository.ScrapRepositoryCustom; import com.example.myongsick.domain.scrap.repository.StoreRepository; import com.example.myongsick.domain.user.entity.User; import com.example.myongsick.domain.user.exception.NotFoundUserException; @@ -39,6 +40,7 @@ public class ScrapServiceImpl implements ScrapService{ private final UserRepository userRepository; private final ScrapRepository scrapRepository; private final StoreRepository storeRepository; + private final ScrapRepositoryCustom scrapRepositoryCustom; @Value("${kakao.rest-key}") private String kakaoRestKey; @@ -80,7 +82,8 @@ public void deleteScrap(Long scrapId) { @Override public Page getScrapCount(String campus, Pageable pageable) { - return scrapRepository.findAllCustom(campus, pageable).map(ScrapCountResponse::toDto); +// return scrapRepositoryCustom.findAllCustom(campus, pageable).map(ScrapCountResponse::toDto); + return scrapRepositoryCustom.findAllByCampusWithPaging(campus, pageable); } @Transactional From 65cbce8250e4fa50ac7d30b6713b174d607cbdc2 Mon Sep 17 00:00:00 2001 From: dldmsql Date: Thu, 27 Apr 2023 10:43:31 +0900 Subject: [PATCH 5/7] =?UTF-8?q?=EB=A7=9B=EC=A7=91=EC=A1=B0=ED=9A=8C=20?= =?UTF-8?q?=EC=8B=9C,=20=EC=B0=9C=EA=BD=81=20=EC=88=98=20=ED=8F=AC?= =?UTF-8?q?=ED=95=A8=20=EA=B0=80=EA=B2=8C=20=EB=8D=B0=EC=9D=B4=ED=84=B0=20?= =?UTF-8?q?=EB=8B=A8=EC=9D=BC=20=EC=A1=B0=ED=9A=8C=20=EC=BF=BC=EB=A6=AC=20?= =?UTF-8?q?=ED=95=A8=EC=88=98=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 가게 pk를 통해 데이터 단일 조회 --- .../domain/scrap/repository/ScrapRepositoryCustom.java | 2 ++ .../domain/scrap/repository/ScrapRepositoryImpl.java | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/src/main/java/com/example/myongsick/domain/scrap/repository/ScrapRepositoryCustom.java b/src/main/java/com/example/myongsick/domain/scrap/repository/ScrapRepositoryCustom.java index 3da9624..3fd8c9b 100644 --- a/src/main/java/com/example/myongsick/domain/scrap/repository/ScrapRepositoryCustom.java +++ b/src/main/java/com/example/myongsick/domain/scrap/repository/ScrapRepositoryCustom.java @@ -7,4 +7,6 @@ public interface ScrapRepositoryCustom { Page findAllByCampusWithPaging(@Param("campus") String campus, Pageable pageable); + + ScrapCountResponse findByIdCustom(Long storeId); } diff --git a/src/main/java/com/example/myongsick/domain/scrap/repository/ScrapRepositoryImpl.java b/src/main/java/com/example/myongsick/domain/scrap/repository/ScrapRepositoryImpl.java index 01747c4..41442e1 100644 --- a/src/main/java/com/example/myongsick/domain/scrap/repository/ScrapRepositoryImpl.java +++ b/src/main/java/com/example/myongsick/domain/scrap/repository/ScrapRepositoryImpl.java @@ -44,6 +44,14 @@ public Page findAllByCampusWithPaging(String campus, Pageabl return new PageImpl<>(result, pageable, count); } + @Override + public ScrapCountResponse findByIdCustom(Long storeId) { + return jpaQueryFactory.select(new QScrapCountResponse(store.id.as("storeId"), store.code, store.name, store.category, store.address, store.contact, store.urlAddress, store.distance, store.latitude, store.longitude, store.scrapList.size().as("scrapCount"))) + .from(store) + .where(store.id.eq(storeId)) + .fetchOne(); + } + private List getOrderSpecifier(Sort sort) { List orderSpecifiers = new ArrayList<>(); sort.stream() From f7a29d7f855a234793fe40d4f9cf1457f2cba6b4 Mon Sep 17 00:00:00 2001 From: dldmsql Date: Thu, 27 Apr 2023 10:44:03 +0900 Subject: [PATCH 6/7] =?UTF-8?q?=EB=A7=9B=EC=A7=91=EC=A1=B0=ED=9A=8C=20?= =?UTF-8?q?=EC=8B=9C,=20=EC=B0=9C=EA=BD=81=20=EC=88=98=20=ED=8F=AC?= =?UTF-8?q?=ED=95=A8=20=EA=B0=80=EA=B2=8C=20=EB=8D=B0=EC=9D=B4=ED=84=B0=20?= =?UTF-8?q?=EB=8B=A8=EC=9D=BC=20=EC=A1=B0=ED=9A=8C=20=EC=BF=BC=EB=A6=AC=20?= =?UTF-8?q?=ED=95=A8=EC=88=98=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 기존 조회 쿼리 주석으로 대체 및 null 체크 조건문 추가 --- .../myongsick/domain/scrap/service/ScrapServiceImpl.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/example/myongsick/domain/scrap/service/ScrapServiceImpl.java b/src/main/java/com/example/myongsick/domain/scrap/service/ScrapServiceImpl.java index 474f18e..06a7eb2 100644 --- a/src/main/java/com/example/myongsick/domain/scrap/service/ScrapServiceImpl.java +++ b/src/main/java/com/example/myongsick/domain/scrap/service/ScrapServiceImpl.java @@ -116,6 +116,11 @@ public void updateStore() { @Override public ScrapCountResponse getStoreOne(Long storeId) { - return storeRepository.findByIdCustom(storeId).map(ScrapCountResponse::toDto).orElseThrow(NotFoundStoreException::new); +// return storeRepository.findByIdCustom(storeId).map(ScrapCountResponse::toDto).orElseThrow(NotFoundStoreException::new); + ScrapCountResponse scrapCountResponse = scrapRepositoryCustom.findByIdCustom(storeId); + if( scrapCountResponse == null ) { + throw new NotFoundStoreException(); + } + return scrapCountResponse; } } From 3924d26c8cd2a527a1029160831c78e6d0732ab9 Mon Sep 17 00:00:00 2001 From: dldmsql Date: Thu, 27 Apr 2023 10:58:33 +0900 Subject: [PATCH 7/7] =?UTF-8?q?=EA=B0=80=EA=B2=8C=20=EB=8D=B0=EC=9D=B4?= =?UTF-8?q?=ED=84=B0=20=EB=8B=A8=EC=9D=BC=20=EC=A1=B0=ED=9A=8C=20=EC=BF=BC?= =?UTF-8?q?=EB=A6=AC=20=ED=95=A8=EC=88=98=20null=20=EC=B2=B4=ED=81=AC=20?= =?UTF-8?q?=EB=B0=A9=EC=8B=9D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit if문 처리에서 optional 방식으로 변경 --- .../domain/scrap/repository/ScrapRepositoryCustom.java | 3 ++- .../domain/scrap/repository/ScrapRepositoryImpl.java | 7 ++++--- .../myongsick/domain/scrap/service/ScrapServiceImpl.java | 6 +----- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/example/myongsick/domain/scrap/repository/ScrapRepositoryCustom.java b/src/main/java/com/example/myongsick/domain/scrap/repository/ScrapRepositoryCustom.java index 3fd8c9b..3eb4b9e 100644 --- a/src/main/java/com/example/myongsick/domain/scrap/repository/ScrapRepositoryCustom.java +++ b/src/main/java/com/example/myongsick/domain/scrap/repository/ScrapRepositoryCustom.java @@ -1,6 +1,7 @@ package com.example.myongsick.domain.scrap.repository; import com.example.myongsick.domain.scrap.dto.ScrapCountResponse; +import java.util.Optional; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.repository.query.Param; @@ -8,5 +9,5 @@ public interface ScrapRepositoryCustom { Page findAllByCampusWithPaging(@Param("campus") String campus, Pageable pageable); - ScrapCountResponse findByIdCustom(Long storeId); + Optional findByIdCustom(Long storeId); } diff --git a/src/main/java/com/example/myongsick/domain/scrap/repository/ScrapRepositoryImpl.java b/src/main/java/com/example/myongsick/domain/scrap/repository/ScrapRepositoryImpl.java index 41442e1..a1379f0 100644 --- a/src/main/java/com/example/myongsick/domain/scrap/repository/ScrapRepositoryImpl.java +++ b/src/main/java/com/example/myongsick/domain/scrap/repository/ScrapRepositoryImpl.java @@ -13,6 +13,7 @@ import com.querydsl.jpa.impl.JPAQueryFactory; import java.util.ArrayList; import java.util.List; +import java.util.Optional; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; @@ -45,11 +46,11 @@ public Page findAllByCampusWithPaging(String campus, Pageabl } @Override - public ScrapCountResponse findByIdCustom(Long storeId) { - return jpaQueryFactory.select(new QScrapCountResponse(store.id.as("storeId"), store.code, store.name, store.category, store.address, store.contact, store.urlAddress, store.distance, store.latitude, store.longitude, store.scrapList.size().as("scrapCount"))) + public Optional findByIdCustom(Long storeId) { + return Optional.ofNullable(jpaQueryFactory.select(new QScrapCountResponse(store.id.as("storeId"), store.code, store.name, store.category, store.address, store.contact, store.urlAddress, store.distance, store.latitude, store.longitude, store.scrapList.size().as("scrapCount"))) .from(store) .where(store.id.eq(storeId)) - .fetchOne(); + .fetchOne()); } private List getOrderSpecifier(Sort sort) { diff --git a/src/main/java/com/example/myongsick/domain/scrap/service/ScrapServiceImpl.java b/src/main/java/com/example/myongsick/domain/scrap/service/ScrapServiceImpl.java index 06a7eb2..08f0f25 100644 --- a/src/main/java/com/example/myongsick/domain/scrap/service/ScrapServiceImpl.java +++ b/src/main/java/com/example/myongsick/domain/scrap/service/ScrapServiceImpl.java @@ -117,10 +117,6 @@ public void updateStore() { @Override public ScrapCountResponse getStoreOne(Long storeId) { // return storeRepository.findByIdCustom(storeId).map(ScrapCountResponse::toDto).orElseThrow(NotFoundStoreException::new); - ScrapCountResponse scrapCountResponse = scrapRepositoryCustom.findByIdCustom(storeId); - if( scrapCountResponse == null ) { - throw new NotFoundStoreException(); - } - return scrapCountResponse; + return scrapRepositoryCustom.findByIdCustom(storeId).orElseThrow(NotFoundStoreException::new); } }