-
Notifications
You must be signed in to change notification settings - Fork 0
[Feat] 가게 조회 API 구현 #86
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
24 changes: 24 additions & 0 deletions
24
src/main/java/eatda/controller/store/StorePreviewResponse.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,24 @@ | ||
| package eatda.controller.store; | ||
|
|
||
| import eatda.domain.store.Store; | ||
|
|
||
| public record StorePreviewResponse( | ||
| long id, | ||
| String imageUrl, | ||
| String name, | ||
| String district, | ||
| String neighborhood, | ||
| String category | ||
| ) { | ||
|
|
||
| public StorePreviewResponse(Store store, String imageUrl) { | ||
| this( | ||
| store.getId(), | ||
| imageUrl, | ||
| store.getName(), | ||
| store.getAddressDistrict(), | ||
| store.getAddressNeighborhood(), | ||
| store.getCategory().getCategoryName() | ||
| ); | ||
| } | ||
| } |
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,6 @@ | ||
| package eatda.controller.store; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public record StoresResponse(List<StorePreviewResponse> stores) { | ||
| } |
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 |
|---|---|---|
| @@ -1,13 +1,23 @@ | ||
| package eatda.repository.store; | ||
|
|
||
| import eatda.domain.store.Cheer; | ||
| import eatda.domain.store.Store; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.data.jpa.repository.Query; | ||
| import org.springframework.data.repository.Repository; | ||
|
|
||
| public interface CheerRepository extends Repository<Cheer, Long> { | ||
|
|
||
| Cheer save(Cheer cheer); | ||
|
|
||
| List<Cheer> findAllByOrderByCreatedAtDesc(Pageable pageable); | ||
|
|
||
| @Query(""" | ||
| SELECT c.imageKey FROM Cheer c | ||
| WHERE c.store = :store AND c.imageKey IS NOT NULL | ||
| ORDER BY c.createdAt DESC | ||
| LIMIT 1""") | ||
| Optional<String> findRecentImageKey(Store store); | ||
| } | ||
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 |
|---|---|---|
| @@ -1,9 +1,13 @@ | ||
| package eatda.repository.store; | ||
|
|
||
| import eatda.domain.store.Store; | ||
| import java.util.List; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.data.repository.Repository; | ||
|
|
||
| public interface StoreRepository extends Repository<Store, Long> { | ||
|
|
||
| Store save(Store store); | ||
|
|
||
| List<Store> findAllByOrderByCreatedAtDesc(Pageable 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,7 @@ | |
| import eatda.service.common.ImageService; | ||
| import java.util.List; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.data.domain.PageRequest; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
|
|
@@ -20,8 +20,7 @@ public class CheerService { | |
|
|
||
| @Transactional(readOnly = true) | ||
| public CheersResponse getCheers(int size) { | ||
| PageRequest pageRequest = PageRequest.of(0, size); | ||
| List<Cheer> cheers = cheerRepository.findAllByOrderByCreatedAtDesc(pageRequest); | ||
| List<Cheer> cheers = cheerRepository.findAllByOrderByCreatedAtDesc(Pageable.ofSize(size)); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. size를 클라이언트에게 받는군요...! |
||
| return toCheersResponse(cheers); | ||
| } | ||
|
|
||
|
|
||
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 |
|---|---|---|
| @@ -1,10 +1,21 @@ | ||
| package eatda.service.store; | ||
|
|
||
| import static java.util.stream.Collectors.collectingAndThen; | ||
| import static java.util.stream.Collectors.toList; | ||
|
|
||
| import eatda.client.map.MapClient; | ||
| import eatda.client.map.StoreSearchResult; | ||
| import eatda.controller.store.StorePreviewResponse; | ||
| import eatda.controller.store.StoreSearchResponses; | ||
| import eatda.controller.store.StoresResponse; | ||
| import eatda.domain.store.Store; | ||
| import eatda.repository.store.CheerRepository; | ||
| import eatda.repository.store.StoreRepository; | ||
| import eatda.service.common.ImageService; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| @Service | ||
|
|
@@ -13,6 +24,22 @@ public class StoreService { | |
|
|
||
| private final MapClient mapClient; | ||
| private final StoreSearchFilter storeSearchFilter; | ||
| private final StoreRepository storeRepository; | ||
| private final CheerRepository cheerRepository; | ||
| private final ImageService imageService; | ||
|
|
||
| // TODO : N+1 문제 해결 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 단골 문제가 나온것 같군요 🥲 |
||
| public StoresResponse getStores(int size) { | ||
| return storeRepository.findAllByOrderByCreatedAtDesc(Pageable.ofSize(size)) | ||
| .stream() | ||
| .map(store -> new StorePreviewResponse(store, getStoreImageUrl(store).orElse(null))) | ||
| .collect(collectingAndThen(toList(), StoresResponse::new)); | ||
| } | ||
|
|
||
| private Optional<String> getStoreImageUrl(Store store) { | ||
| return cheerRepository.findRecentImageKey(store) | ||
| .map(imageService::getPresignedUrl); | ||
| } | ||
|
|
||
| public StoreSearchResponses searchStores(String query) { | ||
| List<StoreSearchResult> searchResults = mapClient.searchShops(query); | ||
|
|
||
34 changes: 34 additions & 0 deletions
34
src/test/java/eatda/controller/store/StoreControllerTest.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
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
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
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 |
|---|---|---|
| @@ -1,16 +1,38 @@ | ||
| package eatda.repository; | ||
|
|
||
| import eatda.fixture.CheerGenerator; | ||
| import eatda.fixture.MemberGenerator; | ||
| import eatda.fixture.StoreGenerator; | ||
| import eatda.repository.member.MemberRepository; | ||
| import eatda.repository.store.CheerRepository; | ||
| import eatda.repository.store.StoreRepository; | ||
| import eatda.repository.story.StoryRepository; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; | ||
| import org.springframework.context.annotation.Import; | ||
|
|
||
| @Import({MemberGenerator.class, StoreGenerator.class, CheerGenerator.class}) | ||
| @DataJpaTest | ||
| public abstract class BaseRepositoryTest { | ||
|
|
||
| @Autowired | ||
| protected MemberGenerator memberGenerator; | ||
|
|
||
| @Autowired | ||
| protected StoreGenerator storeGenerator; | ||
|
|
||
| @Autowired | ||
| protected CheerGenerator cheerGenerator; | ||
|
|
||
| @Autowired | ||
| protected MemberRepository memberRepository; | ||
|
|
||
| @Autowired | ||
| protected StoreRepository storeRepository; | ||
|
|
||
| @Autowired | ||
| protected CheerRepository cheerRepository; | ||
|
|
||
| @Autowired | ||
| protected StoryRepository storyRepository; | ||
| } |
44 changes: 44 additions & 0 deletions
44
src/test/java/eatda/repository/store/CheerRepositoryTest.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,44 @@ | ||
| package eatda.repository.store; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import eatda.domain.member.Member; | ||
| import eatda.domain.store.Store; | ||
| import eatda.repository.BaseRepositoryTest; | ||
| import java.util.Optional; | ||
| import org.junit.jupiter.api.Nested; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class CheerRepositoryTest extends BaseRepositoryTest { | ||
|
|
||
| @Nested | ||
| class FindRecentImageKey { | ||
|
|
||
| @Test | ||
| void 응원들_중_최근_null이_아닌_이미지_키를_조회한다() throws InterruptedException { | ||
| Member member = memberGenerator.generate("111"); | ||
| Store store = storeGenerator.generate("농민백암순대", "서울 강남구 대치동 896-33"); | ||
| cheerGenerator.generateCommon(member, store, "image-key-1"); | ||
| Thread.sleep(5); | ||
| cheerGenerator.generateCommon(member, store, "image-key-2"); | ||
| cheerGenerator.generateCommon(member, store, null); | ||
|
|
||
| Optional<String> imageKey = cheerRepository.findRecentImageKey(store); | ||
|
|
||
| assertThat(imageKey).contains("image-key-2"); | ||
| } | ||
|
|
||
| @Test | ||
| void 응원들의_이미지가_모두_비어있다면_해당_값이_없다() { | ||
| Member member = memberGenerator.generate("111"); | ||
| Store store = storeGenerator.generate("농민백암순대", "서울 강남구 대치동 896-33"); | ||
| cheerGenerator.generateCommon(member, store, null); | ||
| cheerGenerator.generateCommon(member, store, null); | ||
| cheerGenerator.generateCommon(member, store, null); | ||
|
|
||
| Optional<String> imageKey = cheerRepository.findRecentImageKey(store); | ||
|
|
||
| assertThat(imageKey).isEmpty(); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
[질문]
옷 JPQL에서 LIMIT 이 부분 문제없나요??
QuerySyntaxException나는것으로 알고있는데 최근에 지원하게 바뀐건지 궁금합니다!아니면 이런식의 방향은 어떠신가요...?
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.
LIMIT을 쓰는 방법도 좋고, (하위 호환을 고려한다면) 승로님이 말해주신 방법도 좋을 것 같아요!
어떻게 하는 것을 선호하실까요?
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.
와우 역시 최근에 지원하게 바뀌었나보네요
Limit이 된다면... 그대로 가시죠 ㅋㅋㅋㅋ
저희 서비스는 신규 서비스니까... 하위 호환은... 잠시 잊는것으로..