Skip to content

Commit

Permalink
[FEATURE] 친구 목록 조회 API 수정 (#91)
Browse files Browse the repository at this point in the history
* feat : User 관련 기능 API 작성 #83

* spotlessApply #83

* feat : User 차단 관련 검색, 조회 API 로직 변경 #83

* feat : User 신고 정보 조회 API 작성 #83

* spotless Apply #83

* edit : @transactional 추가 #83

* feat : 유저 검색 시 친구 여부 보여지도록 수정, 검색 로직 수정, 신고 api에서 request body 삭제 #83

* spotless Apply #83

* feat : 유저 검색 시 친구, 차단 여부 표시 #83

* edit : 유저 차단 시 로직 변경 #83

* edit : 친구 조회 시 페이지네이션 삭제, 쿼리 로직 변경 #83
  • Loading branch information
bongsh0112 authored Nov 8, 2023
1 parent 533dd69 commit 57bd177
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public OnBoardingStatusResponse getOnBoardingStatus(

@Operation(summary = "친구 목록 조회")
@GetMapping("/neighbors")
public SliceResponse<RetrieveNeighborDTO> getNeighbors(
public List<RetrieveNeighborDTO> getNeighbors(
@ParameterObject @PageableDefault(size = 10) Pageable pageable) {
return retrieveNeighborListUseCase.execute(pageable);
}
Expand All @@ -149,7 +149,7 @@ public void postNeighbor(@PathVariable Long toUserId) {

@Operation(summary = "생일인 친구 조회")
@GetMapping("/neighbors/birthday")
public SliceResponse<RetrieveNeighborDTO> getBirthdayNeighbor(
public List<RetrieveNeighborDTO> getBirthdayNeighbor(
@ParameterObject @PageableDefault Pageable pageable) {
return retrieveBirthdayNeighborUseCase.execute(pageable);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package tify.server.api.user.service;


import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.transaction.annotation.Transactional;
import tify.server.api.common.slice.SliceResponse;
import tify.server.api.utils.UserUtils;
import tify.server.core.annotation.UseCase;
import tify.server.domain.domains.user.adaptor.NeighborAdaptor;
import tify.server.domain.domains.user.adaptor.UserBlockAdaptor;
import tify.server.domain.domains.user.domain.UserBlock;
import tify.server.domain.domains.user.dto.condition.NeighborCondition;
import tify.server.domain.domains.user.dto.model.RetrieveNeighborDTO;

Expand All @@ -17,14 +18,18 @@
public class RetrieveBirthdayNeighborUseCase {

private final NeighborAdaptor neighborAdaptor;
private final UserBlockAdaptor userBlockAdaptor;
private final UserUtils userUtils;

@Transactional(readOnly = true)
public SliceResponse<RetrieveNeighborDTO> execute(Pageable pageable) {
public List<RetrieveNeighborDTO> execute(Pageable pageable) {
Long currentUserId = userUtils.getUserId();
NeighborCondition neighborCondition = new NeighborCondition(currentUserId, pageable);
Slice<RetrieveNeighborDTO> neighborDTOS =
neighborAdaptor.searchBirthdayNeighbors(neighborCondition);
return SliceResponse.of(neighborDTOS);
List<Long> blockedUserList =
userBlockAdaptor.queryAllByFromUserId(currentUserId).stream()
.map(UserBlock::getToUserId)
.toList();
NeighborCondition neighborCondition =
new NeighborCondition(currentUserId, blockedUserList, pageable);
return neighborAdaptor.searchBirthdayNeighbors(neighborCondition);
}
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,39 @@
package tify.server.api.user.service;


import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.transaction.annotation.Transactional;
import tify.server.api.common.slice.SliceResponse;
import tify.server.api.utils.UserUtils;
import tify.server.core.annotation.UseCase;
import tify.server.domain.domains.user.adaptor.NeighborAdaptor;
import tify.server.domain.domains.user.adaptor.UserAdaptor;
import tify.server.domain.domains.user.adaptor.UserBlockAdaptor;
import tify.server.domain.domains.user.domain.UserBlock;
import tify.server.domain.domains.user.dto.condition.NeighborCondition;
import tify.server.domain.domains.user.dto.model.RetrieveNeighborDTO;

@Slf4j
@UseCase
@RequiredArgsConstructor
public class RetrieveNeighborListUseCase {

private final NeighborAdaptor neighborAdaptor;
private final UserAdaptor userAdaptor;
private final UserBlockAdaptor userBlockAdaptor;
private final UserUtils userUtils;

@Transactional(readOnly = true)
public SliceResponse<RetrieveNeighborDTO> execute(Pageable pageable) {
public List<RetrieveNeighborDTO> execute(Pageable pageable) {
Long currentUserId = userUtils.getUserId();
NeighborCondition neighborCondition = new NeighborCondition(currentUserId, pageable);
Slice<RetrieveNeighborDTO> neighborDTOS =
neighborAdaptor.searchNeighbors(neighborCondition);
return SliceResponse.of(neighborDTOS);
List<Long> blockedUserList =
userBlockAdaptor.queryAllByFromUserId(currentUserId).stream()
.map(UserBlock::getToUserId)
.toList();
NeighborCondition neighborCondition =
new NeighborCondition(currentUserId, blockedUserList, pageable);
return neighborAdaptor.searchNeighbors(neighborCondition);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public Optional<Neighbor> queryByFromUserIdAndToUserId(Long userId, Long neighbo
return neighborRepository.findByFromUserIdAndToUserId(userId, neighborId);
}

public Slice<RetrieveNeighborDTO> searchNeighbors(NeighborCondition neighborCondition) {
public List<RetrieveNeighborDTO> searchNeighbors(NeighborCondition neighborCondition) {
return neighborRepository.searchToPage(neighborCondition);
}

Expand All @@ -65,7 +65,7 @@ public void delete(Neighbor neighbor) {
neighborRepository.delete(neighbor);
}

public Slice<RetrieveNeighborDTO> searchBirthdayNeighbors(NeighborCondition neighborCondition) {
public List<RetrieveNeighborDTO> searchBirthdayNeighbors(NeighborCondition neighborCondition) {
return neighborRepository.searchBirthToPage(neighborCondition);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tify.server.domain.domains.user.adaptor;


import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import tify.server.core.annotation.Adaptor;
Expand Down Expand Up @@ -28,6 +29,10 @@ public Optional<UserBlock> queryByFromUserIdAndToUserId(Long fromUserId, Long to
return userBlockRepository.findByFromUserIdAndToUserId(fromUserId, toUserId);
}

public List<UserBlock> queryAllByFromUserId(Long fromUserId) {
return userBlockRepository.findAllByFromUserId(fromUserId);
}

public void save(Long fromUserId, Long toUserId) {
userBlockRepository.save(
UserBlock.builder().fromUserId(fromUserId).toUserId(toUserId).build());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tify.server.domain.domains.user.dto.condition;


import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.data.domain.Pageable;
Expand All @@ -9,6 +10,7 @@
@AllArgsConstructor
public class NeighborCondition {

Long currentUserId;
private Long currentUserId;
private List<Long> blockedUserIdList;
private Pageable pageable;
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package tify.server.domain.domains.user.repository;


import org.springframework.data.domain.Slice;
import java.util.List;
import tify.server.domain.domains.user.dto.condition.NeighborCondition;
import tify.server.domain.domains.user.dto.model.RetrieveNeighborDTO;

public interface NeighborCustomRepository {

Slice<RetrieveNeighborDTO> searchToPage(NeighborCondition neighborCondition);
List<RetrieveNeighborDTO> searchToPage(NeighborCondition neighborCondition);

Slice<RetrieveNeighborDTO> searchBirthToPage(NeighborCondition neighborCondition);
List<RetrieveNeighborDTO> searchBirthToPage(NeighborCondition neighborCondition);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@

import static tify.server.domain.domains.user.domain.QNeighbor.neighbor;
import static tify.server.domain.domains.user.domain.QUser.user;
import static tify.server.domain.domains.user.domain.QUserBlock.userBlock;

import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQueryFactory;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Slice;
import tify.server.domain.common.util.SliceUtil;
import tify.server.domain.domains.user.dto.condition.NeighborCondition;
import tify.server.domain.domains.user.dto.model.RetrieveNeighborDTO;

Expand All @@ -20,72 +17,62 @@ public class NeighborCustomRepositoryImpl implements NeighborCustomRepository {
private final JPAQueryFactory queryFactory;

@Override
public Slice<RetrieveNeighborDTO> searchToPage(NeighborCondition neighborCondition) {
List<RetrieveNeighborDTO> neighbors =
queryFactory
.select(
Projections.constructor(
RetrieveNeighborDTO.class,
neighbor.toUserId,
neighbor.fromUserId,
user.profile.thumbNail,
user.profile.userName,
user.profile.birth,
user.onBoardingStatus.name,
neighbor.order,
neighbor.isView,
user.updatedAt,
neighbor.viewedAt))
.from(neighbor)
.join(user)
.on(user.id.eq(neighbor.toUserId))
.join(userBlock)
.on(userBlock.fromUserId.eq(neighbor.fromUserId))
.where(
neighbor.fromUserId.eq(neighborCondition.getCurrentUserId()),
neighbor.toUserId.ne(userBlock.toUserId))
.orderBy(neighbor.order.asc())
.offset(neighborCondition.getPageable().getOffset())
.limit(neighborCondition.getPageable().getPageSize() + 1)
.fetch();

return SliceUtil.valueOf(neighbors, neighborCondition.getPageable());
public List<RetrieveNeighborDTO> searchToPage(NeighborCondition neighborCondition) {
return queryFactory
.select(
Projections.constructor(
RetrieveNeighborDTO.class,
neighbor.toUserId,
neighbor.fromUserId,
user.profile.thumbNail,
user.profile.userName,
user.profile.birth,
user.onBoardingStatus.name,
neighbor.order,
neighbor.isView,
user.updatedAt,
neighbor.viewedAt))
.from(neighbor)
.join(user)
.on(user.id.eq(neighbor.toUserId))
.where(
neighbor.fromUserId.eq(neighborCondition.getCurrentUserId()),
neighbor.toUserId.notIn(neighborCondition.getBlockedUserIdList()))
.orderBy(neighbor.order.asc())
.offset(neighborCondition.getPageable().getOffset())
.limit(neighborCondition.getPageable().getPageSize() + 1)
.fetch();
}

@Override
public Slice<RetrieveNeighborDTO> searchBirthToPage(NeighborCondition neighborCondition) {
public List<RetrieveNeighborDTO> searchBirthToPage(NeighborCondition neighborCondition) {
LocalDateTime today = LocalDateTime.now(ZoneId.of("Asia/Seoul"));
String monthAndYear =
String.format("%02d%02d", today.getMonth().getValue(), today.getDayOfMonth());
List<RetrieveNeighborDTO> neighbors =
queryFactory
.select(
Projections.constructor(
RetrieveNeighborDTO.class,
neighbor.toUserId,
neighbor.fromUserId,
user.profile.thumbNail,
user.profile.userName,
user.profile.birth,
user.onBoardingStatus.name,
neighbor.order,
neighbor.isView,
user.updatedAt,
neighbor.viewedAt))
.from(neighbor)
.join(user)
.on(user.id.eq(neighbor.toUserId))
.join(userBlock)
.on(userBlock.fromUserId.eq(neighbor.fromUserId))
.where(
neighbor.fromUserId.eq(neighborCondition.getCurrentUserId()),
neighbor.toUserId.ne(userBlock.toUserId),
user.profile.birth.contains(monthAndYear))
.orderBy(neighbor.order.asc())
.offset(neighborCondition.getPageable().getOffset())
.limit(neighborCondition.getPageable().getPageSize() + 1)
.fetch();

return SliceUtil.valueOf(neighbors, neighborCondition.getPageable());
return queryFactory
.select(
Projections.constructor(
RetrieveNeighborDTO.class,
neighbor.toUserId,
neighbor.fromUserId,
user.profile.thumbNail,
user.profile.userName,
user.profile.birth,
user.onBoardingStatus.name,
neighbor.order,
neighbor.isView,
user.updatedAt,
neighbor.viewedAt))
.from(neighbor)
.join(user)
.on(user.id.eq(neighbor.toUserId))
.where(
neighbor.fromUserId.eq(neighborCondition.getCurrentUserId()),
neighbor.toUserId.notIn(neighborCondition.getBlockedUserIdList()),
user.profile.birth.contains(monthAndYear))
.orderBy(neighbor.order.asc())
.offset(neighborCondition.getPageable().getOffset())
.limit(neighborCondition.getPageable().getPageSize() + 1)
.fetch();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tify.server.domain.domains.user.repository;


import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import tify.server.domain.domains.user.domain.UserBlock;
Expand All @@ -10,4 +11,6 @@ public interface UserBlockRepository extends JpaRepository<UserBlock, Long> {
public Optional<UserBlock> findByFromUserIdAndToUserId(Long fromUserId, Long toUserId);

public boolean existsByFromUserIdAndToUserId(Long fromUserId, Long toUserId);

public List<UserBlock> findAllByFromUserId(Long fromUserId);
}

0 comments on commit 57bd177

Please sign in to comment.