Skip to content

Commit

Permalink
[feat] imple repository logic
Browse files Browse the repository at this point in the history
  • Loading branch information
PicturePark1101 committed Jul 12, 2024
1 parent 52da8ae commit 7aafe64
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ public Favorite findById(final Long id) {
return favoriteRepository.findById(id).orElseThrow(() -> new NotFoundException(FavoriteErrorCode.FAVORITE_NOT_FOUND));
}

public List<Favorite> findAllByIds(final List<Long> ids) {
protected List<Favorite> findAllByIds(final List<Long> ids) {
return favoriteRepository.findAllByIds(ids);
}

protected Favorite findByIdWithUser(final Long id) {
return favoriteRepository.findByIdWithUser(id).orElseThrow(() -> new NotFoundException(FavoriteErrorCode.FAVORITE_NOT_FOUND));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ public int countByFavorite(final Favorite favorite) {
return favoriteStoreRepository.countByFavorite(favorite);
}

// public FavoriteStore findByFavoriteIdAndStoreId(final Long favoriteId, final Long storeId) {
// return favoriteStoreRepository.findByFavoriteIdAndStoreId(favoriteId, storeId)
// .orElseThrow(() -> new NotFoundException(FavoriteStoreErrorCode.FAVORITE_STORE_NOT_FOUND));
// }
public FavoriteStore findByFavoriteIdAndStoreId(final Long favoriteId, final Long storeId) {
return favoriteStoreRepository.findByFavoriteIdAndStoreId(favoriteId, storeId)
.orElseThrow(() -> new NotFoundException(FavoriteStoreErrorCode.FAVORITE_STORE_NOT_FOUND));
}

public Optional<FavoriteStore> findByFavoriteIdAndStoreId(final Long favoriteId, final Long storeId) {
return favoriteStoreRepository.findByFavoriteIdAndStoreId(favoriteId, storeId);
public boolean isExist(final Long favoriteId, final Long storeId) {
return favoriteStoreRepository.isExist(favoriteId, storeId);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package org.hankki.hankkiserver.api.favoritestore.service;

import java.util.List;
import lombok.RequiredArgsConstructor;
import org.hankki.hankkiserver.domain.favorite.model.Favorite;
import org.hankki.hankkiserver.domain.favoritestore.model.FavoriteStore;
import org.hankki.hankkiserver.domain.favoritestore.repository.FavoriteStoreRepository;
import org.springframework.stereotype.Component;
Expand All @@ -16,4 +14,4 @@ public class FavoriteStoreUpdater {
public FavoriteStore save(FavoriteStore favoriteStore) {
return favoriteStoreRepository.save(favoriteStore);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.hankki.hankkiserver.domain.favorite.repository;

import java.util.List;
import java.util.Optional;
import org.hankki.hankkiserver.domain.favorite.model.Favorite;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
Expand All @@ -13,6 +14,9 @@ public interface FavoriteRepository extends JpaRepository<Favorite, Long> {
@Query("delete from Favorite f where f in :favorites")
void deleteAll(@Param("favorites") List<Favorite> favorites);

@Query("select f from Favorite f where f.id in :favoriteId")
List<Favorite> findAllByIds(@Param("favoriteId") List<Long> favoriteId);
@Query("select f from Favorite f where f.id in :favoriteIds")
List<Favorite> findAllByIds(@Param("favoriteIds") List<Long> favoriteIds);

@Query("select f from Favorite f join fetch f.user where f.id = :favoriteId")
Optional<Favorite> findByIdWithUser(@Param("favoriteId") Long favoriteId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ public interface FavoriteStoreRepository extends JpaRepository<FavoriteStore, Lo
@Query("delete from FavoriteStore fs where fs.favorite in :favorites")
void deleteAllByFavorites(@Param("favorites") List<Favorite> favorites);

@Query("select count(fs) from FavoriteStore fs where fs.favorite in :favorite")
@Query("select count(fs) from FavoriteStore fs where fs.favorite = :favorite")
int countByFavorite(@Param("favorite") Favorite favorite);

@Query("select fs from FavoriteStore fs where fs.favorite.id = :favoriteId and fs.store.id = :storeId")
Optional<FavoriteStore> findByFavoriteIdAndStoreId(@Param("favoriteId") Long favoriteId, @Param("storeId") Long storeId);

@Query("select exists (select f from FavoriteStore f where f.favorite.id = :favoriteId and f.store.id = :storeId)")
boolean isExist(@Param("favoriteId") Long favoriteId, @Param("storeId") Long storeId);
}

0 comments on commit 7aafe64

Please sign in to comment.