-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #120 from MYONGSIK/feature/scrap-service
[feature/scrap-service] native query를 querydsl로 변경
- Loading branch information
Showing
6 changed files
with
129 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/main/java/com/example/myongsick/domain/scrap/repository/ScrapRepositoryCustom.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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); | ||
|
||
Optional<ScrapCountResponse> findByIdCustom(Long storeId); | ||
} |
73 changes: 73 additions & 0 deletions
73
src/main/java/com/example/myongsick/domain/scrap/repository/ScrapRepositoryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
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 java.util.Optional; | ||
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<ScrapCountResponse> findAllByCampusWithPaging(String campus, Pageable pageable) { | ||
List<ScrapCountResponse> 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); | ||
} | ||
|
||
@Override | ||
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()); | ||
} | ||
|
||
private List<OrderSpecifier> getOrderSpecifier(Sort sort) { | ||
List<OrderSpecifier> 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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/main/java/com/example/myongsick/global/config/QueryDslConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |