Skip to content

Commit

Permalink
[fix] 족보 상세조회 정렬 fix (#133)
Browse files Browse the repository at this point in the history
* [refac] add query for store sort

* [refac] add Finder function

* [refac] refactor responseDTO

* [refac] optimize the query

* [refac] optimize the function

* [refac] refac function name
  • Loading branch information
PicturePark1101 authored Aug 11, 2024
1 parent 6ccc40a commit ace08c4
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ public List<Favorite> findAllByUserId(final Long userId) {
protected Favorite findByIdWithUser(final Long id) {
return favoriteRepository.findByIdWithUser(id).orElseThrow(() -> new NotFoundException(FavoriteErrorCode.FAVORITE_NOT_FOUND));
}

protected Favorite findByIdWithFavoriteStore(final Long id) {
return favoriteRepository.findByIdWithFavoriteStore(id).orElseThrow(() -> new NotFoundException(FavoriteErrorCode.FAVORITE_NOT_FOUND));
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package org.hankki.hankkiserver.api.favorite.service;

import java.util.ArrayList;
import lombok.RequiredArgsConstructor;
import org.hankki.hankkiserver.api.favorite.service.command.FavoritesGetCommand;
import org.hankki.hankkiserver.api.favorite.service.command.FavoritesWithStatusGetCommand;
import org.hankki.hankkiserver.api.favorite.service.response.FavoriteGetResponse;
import org.hankki.hankkiserver.api.favorite.service.response.FavoritesWithStatusGetResponse;
import org.hankki.hankkiserver.domain.favorite.model.Favorite;
import org.hankki.hankkiserver.domain.favoritestore.model.FavoriteStore;
import org.hankki.hankkiserver.api.store.service.StoreFinder;
import org.hankki.hankkiserver.domain.store.model.Store;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -19,11 +22,12 @@
public class FavoriteQueryService {

private final FavoriteFinder favoriteFinder;
private final StoreFinder storeFinder;

@Transactional(readOnly = true)
public FavoriteGetResponse findFavorite(final FavoritesGetCommand command) {
Favorite favorite = favoriteFinder.findById(command.favoriteId());
return FavoriteGetResponse.of(favorite);
Favorite favorite = favoriteFinder.findByIdWithFavoriteStore(command.favoriteId());
return FavoriteGetResponse.of(favorite, findStoresInFavorite(favorite));
}

@Transactional(readOnly = true)
Expand All @@ -41,4 +45,15 @@ private boolean isStoreAlreadyAdded(final List<FavoriteStore> favoriteStore, fin
return favoriteStore.stream()
.anyMatch(f -> f.getStore().getId().equals(commandStoreId));
}
}

private List<Store> findStoresInFavorite(final Favorite favorite){
if (favoriteHasNoStore(favorite)) {
return new ArrayList<>();
}
return storeFinder.findAllByIdsWhereDeletedIsFalseOrderByCreatedAtDes(favorite.getFavoriteStores().stream().map(fs -> fs.getStore().getId()).toList());
}

private boolean favoriteHasNoStore(final Favorite favorite) {
return favorite.getFavoriteStores().isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.hankki.hankkiserver.domain.favorite.model.Favorite;

import java.util.List;
import org.hankki.hankkiserver.domain.store.model.Store;

import static org.hankki.hankkiserver.api.favorite.service.response.util.FavoriteResponseUtil.transformDetail;

Expand All @@ -11,10 +12,10 @@ public record FavoriteGetResponse(
List<String> details,
List<FavoriteStoreResponse> stores
) {
public static FavoriteGetResponse of(final Favorite favorite) {
public static FavoriteGetResponse of(final Favorite favorite, final List<Store> stores) {
return new FavoriteGetResponse(
favorite.getName(),
transformDetail(favorite.getDetail()),
favorite.getFavoriteStores().stream().map(favoriteStore -> FavoriteStoreResponse.of(favoriteStore.getStore())).toList());
stores.stream().map(FavoriteStoreResponse::of).toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,8 @@ protected boolean existsByLatitudeAndLongitude(final double latitude, final doub
public List<Store> findAllDynamicQuery(final Long universityId, final StoreCategory storeCategory, final PriceCategory priceCategory, final SortOption sortOption) {
return storeRepository.findStoreByCategoryAndLowestPriceAndUniversityIdAndIsDeletedFalseOrderBySortOptions(storeCategory, priceCategory, universityId, sortOption);
}
}

public List<Store> findAllByIdsWhereDeletedIsFalseOrderByCreatedAtDes(final List<Long> ids) {
return storeRepository.findAllByIdsAndIsDeletedIsFalseOrderByCreatedAtDesc(ids);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ public interface FavoriteRepository extends JpaRepository<Favorite, Long> {

@Query("select f from Favorite f where f.id = :favoriteId")
Optional<Favorite> findByIdWithUser(@Param("favoriteId") Long favoriteId);
}

@Query("select f from Favorite f left join fetch f.favoriteStores where f.id = :favoriteId")
Optional<Favorite> findByIdWithFavoriteStore(@Param("favoriteId") Long favoriteId);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package org.hankki.hankkiserver.domain.store.repository;

import java.util.List;
import org.hankki.hankkiserver.domain.store.model.Store;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.Optional;
import org.springframework.data.repository.query.Param;

public interface StoreRepository extends JpaRepository<Store, Long>, CustomStoreRepository {
@Query("select s from Store s where s.id = :id and s.isDeleted = false")
Expand All @@ -17,4 +19,7 @@ public interface StoreRepository extends JpaRepository<Store, Long>, CustomStore
Optional<Store> findByPoint_LatitudeAndPoint_Longitude(double latitude, double longitude);

boolean existsByPoint_LatitudeAndPoint_Longitude(double latitude, double longitude);
}

@Query("select s from Store s where s.id in :ids and s.isDeleted = false order by s.createdAt desc")
List<Store> findAllByIdsAndIsDeletedIsFalseOrderByCreatedAtDesc(@Param("ids") List<Long> ids);
}

0 comments on commit ace08c4

Please sign in to comment.