-
Notifications
You must be signed in to change notification settings - Fork 0
UMC 6주차 과제 #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
UMC 6주차 과제 #5
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
src/main/java/com/example/UMC/domain/review/config/QuerydslConfig.java
This file contains hidden or 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.UMC.domain.review.config; | ||
|
|
||
| import com.querydsl.jpa.impl.JPAQueryFactory; | ||
| import jakarta.persistence.EntityManager; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
|
|
||
| @Configuration | ||
| @RequiredArgsConstructor | ||
| public class QuerydslConfig { | ||
|
|
||
| private final EntityManager em; | ||
|
|
||
| @Bean | ||
| public JPAQueryFactory jpaQueryFactory() { | ||
| return new JPAQueryFactory(em); | ||
| } | ||
| } |
37 changes: 37 additions & 0 deletions
37
src/main/java/com/example/UMC/domain/review/controller/ReviewController.java
This file contains hidden or 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,37 @@ | ||
| package com.example.UMC.domain.review.controller; | ||
|
|
||
| import com.example.UMC.domain.review.entity.Review; | ||
| import com.example.UMC.domain.review.repository.ReviewQueryRepository; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.data.domain.Page; | ||
| import org.springframework.data.domain.PageRequest; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| @RestController | ||
| @RequiredArgsConstructor | ||
| @RequestMapping("/reviews") | ||
| public class ReviewController { | ||
|
|
||
| private final ReviewQueryRepository reviewQueryRepository; | ||
|
|
||
| // 태스트 컨틀로러 | ||
|
|
||
| /** | ||
| * 현재 이코드는 무한순회가 도는중 유저 - 리뷰 가 양방향 매핑이라 그래서 entity구조 그대로 하고 무한순회 안돌게 | ||
| * DTO사용이 필요해 보임. | ||
| */ | ||
| @GetMapping | ||
| public Page<Review> getReviews( | ||
| @RequestParam(required = false) Long storeId, | ||
| @RequestParam(required = false) String storeName, | ||
| @RequestParam(required = false) Long regionId, | ||
| @RequestParam(required = false) Integer star, | ||
| @RequestParam(defaultValue = "0") int page, | ||
| @RequestParam(defaultValue = "10") int size | ||
| ) { | ||
| Pageable pageable = PageRequest.of(page, size); | ||
| return reviewQueryRepository.findReviews(storeId, storeName, regionId, star, pageable); | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or 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
16 changes: 16 additions & 0 deletions
16
src/main/java/com/example/UMC/domain/review/repository/ReviewQueryRepository.java
This file contains hidden or 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,16 @@ | ||
| package com.example.UMC.domain.review.repository; | ||
|
|
||
| import com.example.UMC.domain.review.entity.Review; | ||
| import org.springframework.data.domain.Page; | ||
| import org.springframework.data.domain.Pageable; | ||
|
|
||
| public interface ReviewQueryRepository { | ||
|
|
||
| Page<Review> findReviews( | ||
| Long storeId, // 가게ID 필터 | ||
| String storeName, // 가게명 필터 | ||
| Long regionId, // 지역ID 필터 | ||
| Integer star, // 별점 필터 | ||
| Pageable pageable // 페이징 | ||
| ); | ||
| } |
73 changes: 73 additions & 0 deletions
73
src/main/java/com/example/UMC/domain/review/repository/ReviewQueryRepositoryImpl.java
This file contains hidden or 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.UMC.domain.review.repository; | ||
|
|
||
| import com.example.UMC.domain.review.entity.QReview; | ||
| import com.example.UMC.domain.review.entity.Review; | ||
| import com.example.UMC.domain.store.entity.QRegion; | ||
| import com.example.UMC.domain.store.entity.QStore; | ||
| import com.example.UMC.domain.user.entity.QUser; | ||
| import com.querydsl.core.BooleanBuilder; | ||
| import com.querydsl.jpa.impl.JPAQueryFactory; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.context.annotation.Primary; | ||
| import org.springframework.data.domain.PageImpl; | ||
| import org.springframework.stereotype.Repository; | ||
| import org.springframework.data.domain.Page; | ||
| import org.springframework.data.domain.Pageable; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @Repository | ||
| @Primary //컨트롤러 테스트 용도로 잠시 붙힘 | ||
| @RequiredArgsConstructor | ||
| public class ReviewQueryRepositoryImpl implements ReviewQueryRepository { | ||
|
|
||
| private final JPAQueryFactory queryFactory; | ||
|
|
||
| @Override | ||
| public Page<Review> findReviews(Long storeId, String storeName, Long regionId, Integer star, Pageable pageable) { | ||
| // Q클래스들 import | ||
| QReview review = QReview.review; | ||
| QStore store = QStore.store; | ||
| QUser user = QUser.user; | ||
| QRegion region = QRegion.region; | ||
|
|
||
| BooleanBuilder where = new BooleanBuilder(); | ||
|
|
||
| if (storeId != null) { | ||
| where.and(store.id.eq(storeId)); | ||
| } else if (storeName != null && !storeName.isBlank()) { | ||
| where.and(store.name.containsIgnoreCase(storeName)); | ||
| } | ||
|
|
||
| if (regionId != null) { | ||
| where.and(region.id.eq(regionId)); | ||
| } | ||
|
|
||
| if (star != null) { | ||
| where.and(review.rating.eq(star)); | ||
| } | ||
|
|
||
| List<Review> content = queryFactory | ||
| .selectFrom(review) | ||
| .join(review.user, user).fetchJoin() | ||
| .join(review.store, store).fetchJoin() | ||
| .join(review.region, region).fetchJoin() | ||
| .where(where) | ||
| .orderBy(review.createdAt.desc()) | ||
| .offset(pageable.getOffset()) | ||
| .limit(pageable.getPageSize()) | ||
| .fetch(); | ||
|
|
||
| Long total = queryFactory | ||
| .select(review.count()) | ||
| .from(review) | ||
| .join(review.user, user) | ||
| .join(review.store, store) | ||
| .join(review.region, region) | ||
| .where(where) | ||
| .fetchOne(); | ||
|
|
||
| return new PageImpl<>(content, pageable, total == null ? 0 : total); | ||
| } | ||
| } | ||
|
|
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Restful한 uri 설계를 고려했을 때 storeId는 /reviews/{storeId} 이렇게 PathVariable로 넣는게 좋을듯 합니다.