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

[feat] 특정 질문의 주변 (이전, 다음) 질문 절보 조회 API 구현 #191

Merged
merged 3 commits into from
Feb 19, 2024
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
19 changes: 19 additions & 0 deletions src/main/java/qp/official/qp/converter/QuestionConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,23 @@ public static UserQuestionAlarmResponseDTO.UserQuestionAlarmDTO toUserQuestionAl
.createdAt(userQuestionAlarm.getCreatedAt())
.build();
}

public static QuestionResponseDTO.QuestionAdjacentDTO.QuestionAdjacentPreviewDTO toQuestionAdjacentPreviewDTO(Question question) {

if (question == null) return null;

return QuestionResponseDTO.QuestionAdjacentDTO.QuestionAdjacentPreviewDTO.builder()
.questionId(question.getQuestionId())
.title(question.getTitle())
.build();
}

public static QuestionResponseDTO.QuestionAdjacentDTO toQuestionAdjacentDTO(Question.QuestionAdjacent adjacent) {
return QuestionResponseDTO.QuestionAdjacentDTO.builder()
.hasOlder(adjacent.getOlderQuestion() != null)
.hasLater(adjacent.getLaterQuestion() != null)
.olderQuestion(toQuestionAdjacentPreviewDTO(adjacent.getOlderQuestion()))
.laterQuestion(toQuestionAdjacentPreviewDTO(adjacent.getLaterQuestion()))
.build();
}
}
9 changes: 9 additions & 0 deletions src/main/java/qp/official/qp/domain/Question.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,13 @@ public void delete() {
this.user.getQuestionList().remove(this);
this.getQuestionHashTagList().forEach(questionHashTag -> questionHashTag.getHashtag().getQuestionHashTagList().remove(questionHashTag));
}

@Builder
@Getter
@AllArgsConstructor
@NoArgsConstructor
public static class QuestionAdjacent{
Question laterQuestion;
Question olderQuestion;
}
}
11 changes: 11 additions & 0 deletions src/main/java/qp/official/qp/repository/QuestionRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,21 @@
import qp.official.qp.domain.Question;
import qp.official.qp.domain.User;

import java.util.Optional;

@Repository
public interface QuestionRepository extends JpaRepository<Question, Long> {
Page<Question> findAllByOrderByCreatedAtDescQuestionIdDesc(Pageable pageable);
Page<Question> findAllByTitleContainingIgnoreCaseOrContentContainingIgnoreCaseOrderByCreatedAtDescQuestionIdDesc(String title,String content, Pageable pageable);
Page<Question> findByUserOrderByCreatedAtDescQuestionIdDesc(User user, Pageable pageable);
boolean existsByUser(User user);
/**
* OlderQuestionsByQuestionId
*/
Optional<Question> findTopByQuestionIdLessThanOrderByCreatedAtDescQuestionIdDesc(Long questionId);

/**
* LaterQuestionsByQuestionId
* */
Optional<Question> findTopByQuestionIdGreaterThanOrderByCreatedAtAscQuestionIdAsc(Long questionId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import org.springframework.data.domain.Page;
import qp.official.qp.domain.Question;
import qp.official.qp.domain.mapping.UserQuestionAlarm;

import java.util.List;
import java.util.Optional;
import qp.official.qp.domain.mapping.UserQuestionAlarm;

public interface QuestionQueryService {
Question findById(Long questionId);
Expand All @@ -18,4 +18,5 @@ public interface QuestionQueryService {
Page<Question> findAlarmedQuestions(Long userId, int page, int size);
Integer countExpertCountByQuestion(Question question);

Question.QuestionAdjacent findAdjacentQuestions(Long questionId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import qp.official.qp.domain.mapping.UserQuestionAlarm;
import qp.official.qp.repository.AnswerRepository;
import qp.official.qp.repository.QuestionRepository;
import qp.official.qp.repository.UserQuestionAlarmRepository;

import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -107,4 +108,12 @@ public Integer countExpertCountByQuestion(Question question) {
return answerRepository.countByQuestionAndUserRole(question, Role.EXPERT);
}

@Override
public Question.QuestionAdjacent findAdjacentQuestions(Long questionId) {
return Question.QuestionAdjacent.builder()
.olderQuestion(questionRepository.findTopByQuestionIdLessThanOrderByCreatedAtDescQuestionIdDesc(questionId).orElse(null))
.laterQuestion(questionRepository.findTopByQuestionIdGreaterThanOrderByCreatedAtAscQuestionIdAsc(questionId).orElse(null))
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ public ApiResponse<QuestionResponseDTO.QuestionDTO> findQuestion(
);
}

// 특정 질문의 이전, 다음 질문 조회하는 API
@GetMapping("/{questionId}/adjacent")
@Operation(summary = "특정 질문 의 주변 Question 조회 API",description = "# path variable로 조회 할 questionId를 입력하세요.")
public ApiResponse<QuestionResponseDTO.QuestionAdjacentDTO> findAdjacentQuestions(
@PathVariable @ExistQuestion Long questionId
) {
return ApiResponse.onSuccess(
SuccessStatus.Question_OK,
QuestionConverter.toQuestionAdjacentDTO(
questionQueryService.findAdjacentQuestions(questionId)
)
);
}

// 전체 질문 페이징 조회 (검색 가능)
@GetMapping
@Operation(summary = "전체 질문 조회 API",description = "# RequestParam으로 페이징 조회를 위한 page와 size를 입력하세요. 검색을 원할 경우 search를 입력하세요. \n " +
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/qp/official/qp/web/dto/QuestionResponseDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,26 @@ public static class QuestionPreviewListDTO {
boolean isLast;
}

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class QuestionAdjacentDTO {
Boolean hasLater;
Boolean hasOlder;
QuestionAdjacentPreviewDTO laterQuestion;
QuestionAdjacentPreviewDTO olderQuestion;

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class QuestionAdjacentPreviewDTO {
Long questionId;
String title;
}
}

@Builder
@Getter
@NoArgsConstructor
Expand Down
Loading