Skip to content

Commit

Permalink
🚑 hotfix(api): cannot get like issue fix (#257)
Browse files Browse the repository at this point in the history
  • Loading branch information
seonghun-dev committed Jul 20, 2023
1 parent 84fda50 commit 71dda1c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ public record ItemDetailDto(
@Schema(description = "아이템 좋아요 개수", example = "100")
int itemLikeCount
) {
public ItemDetailDto(Item item) {
public ItemDetailDto(Item item, Boolean isLiked) {
this(
item.getId(),
new UserResponseDto(item.getUser()),
new ItemLocationResponseDto(item.getItemLocation().getName()),
new MusicResponseDto(item),
item.getContent(),
item.getCreatedAt(),
item.isLiked(item.getUser()),
isLiked,
item.getItemLikeCount()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public interface ItemRepository extends JpaRepository<Item, Long> {

@Query("SELECT i FROM Item i " +
"JOIN ItemLocation il on il.item.id = i.id " +
"JOIN FETCH i.itemLocation il " +
"WHERE ST_Distance_Sphere(il.point, :point) <= :distance")
List<Item> findNearItemsByDistance(@Param("point") Point point, @Param("distance") Double distance);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,24 +103,20 @@ public ItemDetailResponseDto findOneItem(User user, Long itemId) {

@Transactional(readOnly = true)
public ItemsResponseDto findNearItems(User user, NearItemRequestDto nearItemRequestDto) {
Point point = GeomUtil.createPoint(nearItemRequestDto.getLongitude(), nearItemRequestDto.getLatitude());
var items = itemRepository.findNearItemsByDistance(point, nearItemRequestDto.getDistance());

if (user != null) {
final List<Long> blockedUserIds = getBlockedUserIds(user);
var itemDetailDtoList = items.stream()
.filter((item) -> !blockedUserIds.contains(item.getUser().getId()))
.map(ItemsResponseDto.ItemDetailDto::new)
.toList();
return new ItemsResponseDto(itemDetailDtoList);
} else {
final List<Long> blockedUserIds = new ArrayList<>();
var itemDetailDtoList = items.stream()
.map(ItemsResponseDto.ItemDetailDto::new)
.toList();
return new ItemsResponseDto(itemDetailDtoList);
}
}
Point point = GeomUtil.createPoint(nearItemRequestDto.getLongitude(), nearItemRequestDto.getLatitude());
var items = itemRepository.findNearItemsByDistance(point, nearItemRequestDto.getDistance());

final List<Long> blockedUserIds = getBlockedUserIds(user);
var itemDetailDtoList = items.stream()
.filter((item) -> !blockedUserIds.contains(item.getUser().getId()))
.map(item ->
{
Boolean isLiked = itemLikeRepository.existsByUserIdAndItemId(user.getId(), item.getId());
return new ItemsResponseDto.ItemDetailDto(item, isLiked);
})
.toList();
return new ItemsResponseDto(itemDetailDtoList);
}

private List<Long> getBlockedUserIds(User user) {
return blockUserRepository.findBlockedIdsByBlockerId(user.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ void findNearItemsTestSuccess2() {
var result = itemService.findNearItems(null, nearItemRequestDto);
var expected = new ItemsResponseDto(
List.of(
new ItemsResponseDto.ItemDetailDto(item1),
new ItemsResponseDto.ItemDetailDto(item2)
new ItemsResponseDto.ItemDetailDto(item1, true),
new ItemsResponseDto.ItemDetailDto(item2, true)
)
);
assertThat(result).isEqualTo(expected);
Expand Down

0 comments on commit 71dda1c

Please sign in to comment.