Skip to content

Commit

Permalink
edit : 데일리 질문에 대한 답변 조회시 내 답변 포함시키기
Browse files Browse the repository at this point in the history
  • Loading branch information
bongsh0112 committed Nov 7, 2023
1 parent f77bd2c commit 533dd69
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package tify.server.api.answer.service;


import java.util.ArrayList;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
Expand All @@ -15,6 +17,7 @@
import tify.server.domain.domains.question.dto.condition.AnswerCondition;
import tify.server.domain.domains.question.dto.model.AnswerVo;
import tify.server.domain.domains.user.adaptor.NeighborAdaptor;
import tify.server.domain.domains.user.domain.Neighbor;

@UseCase
@RequiredArgsConstructor
Expand All @@ -29,8 +32,16 @@ public class RetrieveDailyAnswerUseCase {
public SliceResponse<RetrieveAnswerDTO> execute(Long questionId, Pageable pageable) {
DailyQuestion dailyQuestion = dailyQuestionAdaptor.query(questionId);
Long currentUserId = userUtils.getUserId();
AnswerCondition answerCondition = new AnswerCondition(dailyQuestion.getId(), pageable);
List<Long> userIdList =
new ArrayList<>(
neighborAdaptor.queryAllByFromUserIdAndIsView(currentUserId, true).stream()
.map(Neighbor::getToUserId)
.toList());
userIdList.add(currentUserId);
AnswerCondition answerCondition =
new AnswerCondition(dailyQuestion.getId(), userIdList, pageable);
Slice<AnswerVo> answers = answerAdaptor.searchAnswer(currentUserId, answerCondition);
return SliceResponse.of(answers.map(answer -> RetrieveAnswerDTO.of(answer, currentUserId)));
return SliceResponse.of(
answers.map(answerVo -> RetrieveAnswerDTO.of(answerVo, currentUserId)));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tify.server.domain.domains.question.dto.condition;


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

private Long questionId;
private List<Long> userIdList;
private Pageable pageable;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static tify.server.core.consts.Status.N;
import static tify.server.domain.domains.question.domain.QAnswer.answer;
import static tify.server.domain.domains.user.domain.QNeighbor.neighbor;
import static tify.server.domain.domains.user.domain.QUser.user;

import com.querydsl.core.types.Projections;
Expand All @@ -25,17 +24,13 @@ public Slice<AnswerVo> searchToPage(Long userId, AnswerCondition answerCondition
List<AnswerVo> answers =
queryFactory
.select(Projections.constructor(AnswerVo.class, answer, user))
.from(neighbor)
.join(answer)
.on(answer.userId.eq(neighbor.toUserId))
.from(answer)
.join(user)
.on(user.id.eq(neighbor.toUserId))
.on(answer.userId.eq(user.id))
.where(
neighbor.fromUserId.eq(userId),
neighbor.isView.eq(true),
questionIdEq(answerCondition.getQuestionId()),
answer.isDeleted.eq(N))
.orderBy(neighbor.order.asc())
answer.isDeleted.eq(N),
answer.userId.in(answerCondition.getUserIdList()))
.offset(answerCondition.getPageable().getOffset())
.limit(answerCondition.getPageable().getPageSize() + 1)
.fetch();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public List<Neighbor> queryAllByFromUserId(Long fromUserId) {
return neighborRepository.findAllByFromUserId(fromUserId);
}

public List<Neighbor> queryAllByFromUserIdAndIsView(Long fromUserId, boolean isView) {
return neighborRepository.findAllByFromUserIdAndIsView(fromUserId, isView);
}

public Optional<Neighbor> queryByFromUserIdAndToUserId(Long userId, Long neighborId) {
return neighborRepository.findByFromUserIdAndToUserId(userId, neighborId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ public interface NeighborRepository
Optional<Neighbor> findByIdAndFromUserId(Long id, Long fromUserId);

boolean existsByFromUserIdAndToUserId(Long userId, Long neighborId);

List<Neighbor> findAllByFromUserIdAndIsView(Long fromUserId, boolean isView);
}

0 comments on commit 533dd69

Please sign in to comment.