diff --git a/src/main/java/blacktokkies/toquiz/domain/question/api/QuestionApi.java b/src/main/java/blacktokkies/toquiz/domain/question/api/QuestionApi.java index 4cb1d76..85a9b27 100644 --- a/src/main/java/blacktokkies/toquiz/domain/question/api/QuestionApi.java +++ b/src/main/java/blacktokkies/toquiz/domain/question/api/QuestionApi.java @@ -2,24 +2,41 @@ import blacktokkies.toquiz.domain.question.application.QuestionService; import blacktokkies.toquiz.domain.question.dto.request.CreateQuestionRequest; -import blacktokkies.toquiz.domain.question.dto.response.CreateQuestionResponse; +import blacktokkies.toquiz.domain.question.dto.response.GetQuestionsResponse; +import blacktokkies.toquiz.domain.question.dto.response.QuestionResponse; import blacktokkies.toquiz.global.common.response.SuccessResponse; import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; +import org.springframework.data.web.PageableDefault; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; +import java.util.List; + @RestController @RequiredArgsConstructor public class QuestionApi { private final QuestionService questionService; @PostMapping("api/panels/{panelId}/question") - public ResponseEntity> createQuestion( + public ResponseEntity> createQuestion( @RequestBody @Valid CreateQuestionRequest request, @CookieValue("active_info_id") String activeInfoId, @PathVariable Long panelId ){ - CreateQuestionResponse response = questionService.createQuestion(request, activeInfoId, panelId); + QuestionResponse response = questionService.createQuestion(request, activeInfoId, panelId); + + return ResponseEntity.ok(new SuccessResponse<>(response)); + } + + @GetMapping("api/panels/{panelId}/questions") + public ResponseEntity> getQuestions( + @PageableDefault(size = 30, sort = {"likeNum", "createdDate"}, direction = Sort.Direction.DESC) Pageable pageable, + @PathVariable Long panelId + ){ + List questionResponses = questionService.getQuestions(panelId, pageable); + GetQuestionsResponse response = GetQuestionsResponse.toDto(questionResponses, pageable); return ResponseEntity.ok(new SuccessResponse<>(response)); } diff --git a/src/main/java/blacktokkies/toquiz/domain/question/application/QuestionService.java b/src/main/java/blacktokkies/toquiz/domain/question/application/QuestionService.java index e0100a5..e8ac379 100644 --- a/src/main/java/blacktokkies/toquiz/domain/question/application/QuestionService.java +++ b/src/main/java/blacktokkies/toquiz/domain/question/application/QuestionService.java @@ -5,12 +5,15 @@ import blacktokkies.toquiz.domain.question.dao.QuestionRepository; import blacktokkies.toquiz.domain.question.domain.Question; import blacktokkies.toquiz.domain.question.dto.request.CreateQuestionRequest; -import blacktokkies.toquiz.domain.question.dto.response.CreateQuestionResponse; +import blacktokkies.toquiz.domain.question.dto.response.QuestionResponse; import blacktokkies.toquiz.global.common.error.RestApiException; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.List; + import static blacktokkies.toquiz.domain.panel.exception.PanelErrorCode.NOT_EXIST_PANEL; @Service @@ -21,18 +24,24 @@ public class QuestionService { private final PanelRepository panelRepository; @Transactional - public CreateQuestionResponse createQuestion( + public QuestionResponse createQuestion( CreateQuestionRequest createQuestionRequest, String activeInfoId, Long panelId) { Question question = questionRepository.save(new Question(createQuestionRequest.getContent(), getPanel(panelId), activeInfoId)); - return CreateQuestionResponse.toDto(question); + return QuestionResponse.toDto(question); } private Panel getPanel(Long panelId){ return panelRepository.findById(panelId).orElseThrow( () -> new RestApiException(NOT_EXIST_PANEL)); } + + public List getQuestions(Long panelId, Pageable pageable) { + List questions = questionRepository.findAllByPanel(getPanel(panelId), pageable); + + return questions.stream().map(QuestionResponse::toDto).toList(); + } } diff --git a/src/main/java/blacktokkies/toquiz/domain/question/dao/QuestionRepository.java b/src/main/java/blacktokkies/toquiz/domain/question/dao/QuestionRepository.java index bd90c16..937ea85 100644 --- a/src/main/java/blacktokkies/toquiz/domain/question/dao/QuestionRepository.java +++ b/src/main/java/blacktokkies/toquiz/domain/question/dao/QuestionRepository.java @@ -1,9 +1,14 @@ package blacktokkies.toquiz.domain.question.dao; +import blacktokkies.toquiz.domain.panel.domain.Panel; import blacktokkies.toquiz.domain.question.domain.Question; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; +import java.util.List; + @Repository public interface QuestionRepository extends JpaRepository { + List findAllByPanel(Panel panel, Pageable pageable); }