Skip to content

Commit

Permalink
가게 데이터 단일 조회 쿼리 함수 null 체크 방식 변경
Browse files Browse the repository at this point in the history
if문 처리에서 optional 방식으로 변경
  • Loading branch information
dldmsql committed Apr 27, 2023
1 parent f7a29d7 commit 3924d26
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
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;

public interface ScrapRepositoryCustom {
Page<ScrapCountResponse> findAllByCampusWithPaging(@Param("campus") String campus, Pageable pageable);

ScrapCountResponse findByIdCustom(Long storeId);
Optional<ScrapCountResponse> findByIdCustom(Long storeId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -45,11 +46,11 @@ public Page<ScrapCountResponse> 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<ScrapCountResponse> 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<OrderSpecifier> getOrderSpecifier(Sort sort) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 3924d26

Please sign in to comment.