Skip to content

Commit

Permalink
[FIX] 친구 관련 API 수정 (#129)
Browse files Browse the repository at this point in the history
* edit : NeighborCondition 없이도 친구 조회하도록 변경 #128

* edit : NeighborCondition 없이도 친구 조회하도록 변경 #128

* spotless Apply #128

* fix : 친구 관련 API 수정 - 친구 신청 받았을 시 Neighbor 생성 #128
  • Loading branch information
bongsh0112 authored Dec 13, 2023
1 parent ff1a942 commit e6940d1
Show file tree
Hide file tree
Showing 14 changed files with 53 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,15 @@
import tify.server.domain.domains.question.domain.DailyQuestion;
import tify.server.domain.domains.question.dto.condition.AnswerCondition;
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.Neighbor;
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 RetrieveDailyAnswerUseCase {

private final UserAdaptor userAdaptor;
private final AnswerAdaptor answerAdaptor;
private final UserBlockAdaptor userBlockAdaptor;
private final NeighborAdaptor neighborAdaptor;
private final DailyQuestionAdaptor dailyQuestionAdaptor;
private final KnockAdaptor knockAdaptor;
Expand All @@ -56,17 +50,7 @@ public List<RetrieveAnswerVo> execute(Long questionId) {

@Transactional(readOnly = true)
public List<NeighborAnswerInfoDTO> executeNeighborAnswerList(Long questionId, Long userId) {
List<Long> blockedIdList =
userBlockAdaptor.queryAllByFromUserId(userId).stream()
.map(UserBlock::getToUserId)
.toList();
List<Long> friendIdList =
neighborAdaptor.queryAllByFromUserId(userId).stream()
.map(Neighbor::getToUserId)
.toList();
NeighborCondition neighborCondition =
new NeighborCondition(userId, blockedIdList, friendIdList);
List<RetrieveNeighborDTO> neighbors = neighborAdaptor.searchNeighbors(neighborCondition);
List<RetrieveNeighborDTO> neighbors = neighborAdaptor.searchNeighbors(userId);
return neighbors.stream()
.map(
dto -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,25 @@ public void execute(Long neighborApplicationId) {
throw AlreadyExistNeighborRelationshipException.EXCEPTION;
}

List<Neighbor> neighbors = neighborAdaptor.queryAllByFromUserId(currentUserId);
List<Neighbor> fromNeighbors = neighborAdaptor.queryAllByFromUserId(currentUserId);
List<Neighbor> toNeighbors = neighborAdaptor.queryAllByFromUserId(toUserId);

neighborAdaptor.save(
Neighbor.builder()
.fromUserId(currentUserId)
.toUserId(toUserId)
.isView(true)
.isNew(true)
.order((long) neighbors.size() + 1L)
.order((long) fromNeighbors.size() + 1L)
.build());

neighborAdaptor.save(
Neighbor.builder()
.fromUserId(toUserId)
.toUserId(currentUserId)
.isView(true)
.isNew(true)
.order((long) toNeighbors.size() + 1L)
.build());
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package tify.server.api.user.service;


import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import tify.server.api.config.security.SecurityUtils;
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.domain.Neighbor;
import tify.server.domain.domains.user.domain.NeighborApplication;
import tify.server.domain.domains.user.domain.User;
import tify.server.domain.domains.user.exception.AlreadyExistNeighborRelationshipException;
Expand All @@ -33,16 +31,6 @@ public void execute(Long toUserId) {
if (neighborAdaptor.existsNeighbor(currentUserId, toUser.getId())) {
throw AlreadyExistNeighborRelationshipException.EXCEPTION;
}
List<Neighbor> neighbors = neighborAdaptor.queryAllByFromUserId(currentUserId);

neighborAdaptor.save(
Neighbor.builder()
.fromUserId(currentUserId)
.toUserId(toUser.getId())
.isView(true)
.isNew(true)
.order((long) neighbors.size() + 1L)
.build());

// 반대편에서 이미 친구가 되어있다면 신청을 보내지 않음
if (!neighborAdaptor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public void execute(Long neighborApplicationId) {

Neighbor neighbor =
neighborAdaptor
.queryByFromUserIdAndToUserId(neighborApplicationId, currentUserId)
.queryByFromUserIdAndToUserId(
neighborApplication.getFromUserId(), currentUserId)
.orElseThrow(() -> NeighborNotFoundException.EXCEPTION);
neighborAdaptor.delete(neighbor);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import tify.server.api.config.security.SecurityUtils;
import tify.server.core.annotation.UseCase;
import tify.server.domain.domains.user.adaptor.NeighborAdaptor;
import tify.server.domain.domains.user.domain.Neighbor;
import tify.server.domain.domains.user.exception.NeighborNotFoundException;
import tify.server.domain.domains.user.validator.UserValidator;

@UseCase
Expand All @@ -21,17 +19,11 @@ public class RemoveNeighborUseCase {
public void execute(Long toUserId) {
Long userId = SecurityUtils.getCurrentUserId();
userValidator.isNeighbor(userId, toUserId);

Neighbor fromNeighbor =
neighborAdaptor
.queryByFromUserIdAndToUserId(userId, toUserId)
.orElseThrow(() -> NeighborNotFoundException.EXCEPTION);
Neighbor toNeighbor =
neighborAdaptor
.queryByFromUserIdAndToUserId(toUserId, userId)
.orElseThrow(() -> NeighborNotFoundException.EXCEPTION);

neighborAdaptor.delete(fromNeighbor);
neighborAdaptor.delete(toNeighbor);
neighborAdaptor
.queryByFromUserIdAndToUserId(userId, toUserId)
.ifPresent(neighborAdaptor::delete);
neighborAdaptor
.queryByFromUserIdAndToUserId(toUserId, userId)
.ifPresent(neighborAdaptor::delete);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
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.Neighbor;
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;

@UseCase
Expand All @@ -24,16 +21,6 @@ public class RetrieveBirthdayNeighborUseCase {
@Transactional(readOnly = true)
public List<RetrieveNeighborDTO> execute() {
Long currentUserId = userUtils.getUserId();
List<Long> blockedUserList =
userBlockAdaptor.queryAllByFromUserId(currentUserId).stream()
.map(UserBlock::getToUserId)
.toList();
List<Long> friendIdList =
neighborAdaptor.queryAllByToUserId(currentUserId).stream()
.map(Neighbor::getFromUserId)
.toList();
NeighborCondition neighborCondition =
new NeighborCondition(currentUserId, blockedUserList, friendIdList);
return neighborAdaptor.searchBirthdayNeighbors(neighborCondition);
return neighborAdaptor.searchBirthdayNeighbors(currentUserId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
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.Neighbor;
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
Expand All @@ -28,33 +25,13 @@ public class RetrieveNeighborListUseCase {
@Transactional(readOnly = true)
public List<RetrieveNeighborDTO> execute() {
Long currentUserId = userUtils.getUserId();
List<Long> blockedUserList =
userBlockAdaptor.queryAllByFromUserId(currentUserId).stream()
.map(UserBlock::getToUserId)
.toList();
List<Long> friendIdList =
neighborAdaptor.queryAllByToUserId(currentUserId).stream()
.map(Neighbor::getFromUserId)
.toList();
NeighborCondition neighborCondition =
new NeighborCondition(currentUserId, blockedUserList, friendIdList);
return neighborAdaptor.searchNeighbors(neighborCondition);
return neighborAdaptor.searchNeighbors(currentUserId);
}

@Transactional(readOnly = true)
public List<RetrieveNeighborDTO> executeToIsNew() {
Long currentUserId = userUtils.getUserId();
List<Long> blockedUserList =
userBlockAdaptor.queryAllByFromUserId(currentUserId).stream()
.map(UserBlock::getToUserId)
.toList();
List<Long> friendIdList =
neighborAdaptor.queryAllByToUserId(currentUserId).stream()
.map(Neighbor::getFromUserId)
.toList();
NeighborCondition neighborCondition =
new NeighborCondition(currentUserId, blockedUserList, friendIdList);
return neighborAdaptor.searchNeighbors(neighborCondition).stream()
return neighborAdaptor.searchNeighbors(currentUserId).stream()
.filter(RetrieveNeighborDTO::isNew)
.toList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void execute(Long toUserId) {
.optionalQueryByFromUserIdAndToUserId(toUserId, userId)
.ifPresent(neighborAdaptor::deleteNeighborApplication);

// 타겟이 되는 유저와 원래 친구였다면 둘의 친구를 끊음
// 타겟이 되는 유저와 원래 친구였다면 둘의 친구를 끊음 & 두 사람의 친구 목록 업데이트
neighborAdaptor
.queryByFromUserIdAndToUserId(userId, toUserId)
.ifPresent(neighborAdaptor::delete);
Expand Down
2 changes: 2 additions & 0 deletions Api/src/main/java/tify/server/api/utils/UserUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import org.springframework.stereotype.Component;
import tify.server.api.config.security.SecurityUtils;
import tify.server.domain.domains.user.adaptor.UserAdaptor;
import tify.server.domain.domains.user.adaptor.UserBlockAdaptor;
import tify.server.domain.domains.user.domain.User;

@Component
@RequiredArgsConstructor
public class UserUtils {

private final UserAdaptor userAdaptor;
private final UserBlockAdaptor userBlockAdaptor;

public Long getUserId() {
return SecurityUtils.getCurrentUserId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import tify.server.domain.domains.user.domain.Neighbor;
import tify.server.domain.domains.user.domain.NeighborApplication;
import tify.server.domain.domains.user.domain.User;
import tify.server.domain.domains.user.dto.condition.NeighborCondition;
import tify.server.domain.domains.user.dto.model.GetNeighborApplicationDTO;
import tify.server.domain.domains.user.dto.model.RetrieveNeighborDTO;
import tify.server.domain.domains.user.exception.NeighborApplicationNotFoundException;
Expand Down Expand Up @@ -53,13 +52,12 @@ public Optional<Neighbor> queryByFromUserIdAndToUserId(Long userId, Long neighbo
return neighborRepository.findByFromUserIdAndToUserId(userId, neighborId);
}

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

public Slice<RetrieveNeighborDTO> searchNeighbors(
NeighborCondition neighborCondition, Pageable pageable) {
return neighborRepository.searchNeighborsToPage(neighborCondition, pageable);
public Slice<RetrieveNeighborDTO> searchNeighbors(Long userId, Pageable pageable) {
return neighborRepository.searchNeighborsToPage(userId, pageable);
}

public boolean existsNeighbor(Long userId, Long neighborId) {
Expand All @@ -74,8 +72,8 @@ public void delete(Neighbor neighbor) {
neighborRepository.delete(neighbor);
}

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

public void saveNeighborApplication(NeighborApplication neighborApplication) {
Expand Down Expand Up @@ -121,4 +119,8 @@ public Optional<NeighborApplication> optionalQueryByFromUserIdAndToUserId(
public void deleteNeighborApplication(NeighborApplication neighborApplication) {
neighborApplicationRepository.delete(neighborApplication);
}

public List<Neighbor> queryAllByFromUserIdOrderByOrder(Long fromUserId) {
return neighborRepository.findAllByFromUserIdOOrderByOrder(fromUserId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public class RetrieveNeighborDTO {

private Long neighborId;

private Long neighborUserId;

private Long userId;

private String neighborThumbnail;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
import java.util.List;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import tify.server.domain.domains.user.dto.condition.NeighborCondition;
import tify.server.domain.domains.user.dto.model.RetrieveNeighborDTO;

public interface NeighborCustomRepository {

List<RetrieveNeighborDTO> searchNeighbors(NeighborCondition neighborCondition);
List<RetrieveNeighborDTO> searchNeighbors(Long userId);

List<RetrieveNeighborDTO> searchBirthdayNeighbors(NeighborCondition neighborCondition);
List<RetrieveNeighborDTO> searchBirthdayNeighbors(Long userId);

Slice<RetrieveNeighborDTO> searchNeighborsToPage(
NeighborCondition neighborCondition, Pageable pageable);
Slice<RetrieveNeighborDTO> searchNeighborsToPage(Long userId, Pageable pageable);
}
Loading

0 comments on commit e6940d1

Please sign in to comment.