Skip to content

Commit

Permalink
[feat] create logic for get store information api
Browse files Browse the repository at this point in the history
  • Loading branch information
Parkjyun committed Jul 12, 2024
1 parent f7911e5 commit 2a4e0a1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ protected Store findByIdWhereDeletedIsFalse(Long id) {
return storeRepository.findByIdAndIsDeletedIsFalse(id)
.orElseThrow(() -> new NotFoundException(StoreErrorCode.STORE_NOT_FOUND));
}

protected Store findStoreByIdWithHeart(Long id) {
return storeRepository.findStoreByIdWithHeart(id)
.orElseThrow(() -> new NotFoundException(StoreErrorCode.STORE_NOT_FOUND));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,41 @@


import lombok.RequiredArgsConstructor;
import org.hankki.hankkiserver.api.menu.service.MenuFinder;
import org.hankki.hankkiserver.api.store.parameter.PriceCategory;
import org.hankki.hankkiserver.api.store.parameter.SortOption;
import org.hankki.hankkiserver.api.store.service.response.*;
import org.hankki.hankkiserver.domain.store.model.Store;
import org.hankki.hankkiserver.domain.store.model.StoreCategory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
@RequiredArgsConstructor
public class StoreQueryService {

private final StoreFinder storeFinder;
private final MenuFinder menuFinder;

@Transactional(readOnly = true)//주어진 pk를 가진 식당의 정보를 조회 이때 식당의 상태가 is_deleted면 404를 나타낸다.
@Transactional(readOnly = true)
public StoreThumbnailResponse getStoreThumbnail(Long id) {
return StoreThumbnailResponse.of(storeFinder.findByIdWhereDeletedIsFalse(id));
}

@Transactional(readOnly = true)
public StoreGetResponse getStoreInformation(Long id) {

Store store = storeFinder.findStoreByIdWithHeart(id);

return StoreGetResponse.of(store,
isLiked(id, store),
getImageUrlsFromStore(store),
getMenus(store));
}

public CategoriesResponse getCategories() {
return new CategoriesResponse(Arrays.stream(StoreCategory.values())
.map(CategoryResponse::of)
Expand All @@ -39,4 +54,18 @@ public PriceCategoriesResponse getPriceCategories() {
.map(PriceCategoryResponse::of)
.toList());
}

private List<String> getImageUrlsFromStore(Store store) {
return store.getImages().stream()
.map(storeImage -> storeImage.getImageUrl())
.toList();
}

private List<MenuResponse> getMenus(Store store) {
return menuFinder.findAllByStore(store).stream().map(MenuResponse::of).toList();
}

private boolean isLiked(Long id, Store store) {
return store.getHearts().stream().anyMatch(heart -> heart.getUser().getId().equals(id));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@
public interface StoreRepository extends JpaRepository<Store, Long> {
@Query("select s from Store s where s.id = :id and s.isDeleted = false")
Optional<Store> findByIdAndIsDeletedIsFalse(Long id);

@Query("select s from Store s join fetch s.hearts where s.id = :id and s.isDeleted = false")
Optional<Store> findStoreByIdWithHeart(Long id);
}

0 comments on commit 2a4e0a1

Please sign in to comment.