Skip to content

Commit

Permalink
[feat] 식당 세부 조회 시 좋아요 테이블 없는 경우 404 나오는 에러 수정 (#108)
Browse files Browse the repository at this point in the history
* [fix] add distinct keyworkd in method

* [fix] fix hearts to be initialized as an empty list

* [fix] fix controller to get userId as a parameter

* [fix] fix logic related with isLiked field in response
  • Loading branch information
Parkjyun authored Jul 16, 2024
1 parent db20484 commit 844949b
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ public class StoreController {
private final StoreQueryService storeQueryService;
private final HeartCommandService heartCommandService;

@GetMapping("/stores/{id}")
public HankkiResponse<StoreGetResponse> getStore(@PathVariable final Long id) {
return HankkiResponse.success(CommonSuccessCode.OK, storeQueryService.getStoreInformation(id));
@GetMapping("/stores/{storeId}")
public HankkiResponse<StoreGetResponse> getStore(@PathVariable final Long storeId,
@UserId final Long userId) {
return HankkiResponse.success(CommonSuccessCode.OK, storeQueryService.getStoreInformation(storeId, userId));
}

@GetMapping("/stores/{id}/thumbnail")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ public StoreThumbnailResponse getStoreThumbnail(final Long id) {
}

@Transactional(readOnly = true)
public StoreGetResponse getStoreInformation(final Long id) {
public StoreGetResponse getStoreInformation(final Long storeId, final Long userId) {

Store store = storeFinder.findByIdWithHeartAndIsDeletedFalse(id);
Store store = storeFinder.findByIdWithHeartAndIsDeletedFalse(storeId);

return StoreGetResponse.of(store,
isLiked(id, store),
isLiked(userId, store),
getImageUrlsFromStore(store),
getMenus(store));
}
Expand Down Expand Up @@ -72,11 +72,11 @@ private List<MenuResponse> getMenus(final Store store) {
return menuFinder.findAllByStore(store).stream().map(MenuResponse::of).toList();
}

private boolean isLiked(final Long id, final Store store) {
return store.getHearts().stream().anyMatch(heart -> isLiked(id, heart));
private boolean isLiked(final Long userId, final Store store) {
return store.getHearts().stream().anyMatch(heart -> hasSameUserId(userId, heart));
}

private static boolean isLiked(final Long id, final Heart heart) {
private boolean hasSameUserId(final Long id, final Heart heart) {
return heart.getUser().getId().equals(id);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class Store extends BaseTimeEntity {
private Long id;

@OneToMany(mappedBy = "store")
private List<Heart> hearts;
private List<Heart> hearts = new ArrayList<>();

@Embedded
private Point point;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,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")
@Query("select distinct s from Store s left join fetch s.hearts where s.id = :id and s.isDeleted = false")
Optional<Store> findByIdWithHeartAndIsDeletedFalse(Long id);

@Query("select s from Store s where s.point.latitude = :latitude and s.point.longitude = :longitude")
Expand Down

0 comments on commit 844949b

Please sign in to comment.