Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚑 hotfix(api): cannot get like issue fix #257

Merged
merged 1 commit into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading