diff --git a/src/main/java/com/smunity/server/domain/question/controller/QuestionController.java b/src/main/java/com/smunity/server/domain/question/controller/QuestionController.java index 44a973c..e84ade7 100644 --- a/src/main/java/com/smunity/server/domain/question/controller/QuestionController.java +++ b/src/main/java/com/smunity/server/domain/question/controller/QuestionController.java @@ -1,5 +1,6 @@ package com.smunity.server.domain.question.controller; +import com.smunity.server.domain.question.dto.QuestionReadResponseDto; import com.smunity.server.domain.question.dto.QuestionRequestDto; import com.smunity.server.domain.question.dto.QuestionResponseDto; import com.smunity.server.domain.question.service.QuestionCommandService; @@ -26,8 +27,8 @@ public class QuestionController { private final QuestionCommandService questionCommandService; @GetMapping - public ResponseEntity> readQuestions(@ParameterObject @PageableDefault(size = 5, sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable) { - Page responseDtoPage = questionQueryService.readQuestions(pageable); + public ResponseEntity> readQuestions(@AuthMember Long memberId, @ParameterObject @PageableDefault(size = 5, sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable) { + Page responseDtoPage = questionQueryService.readQuestions(memberId, pageable); return ResponseEntity.ok(responseDtoPage); } @@ -38,8 +39,8 @@ public ResponseEntity createQuestion(@AuthMember Long membe } @GetMapping("/{questionId}") - public ResponseEntity readQuestion(@PathVariable Long questionId) { - QuestionResponseDto responseDto = questionQueryService.readQuestion(questionId); + public ResponseEntity readQuestion(@AuthMember Long memberId, @PathVariable Long questionId) { + QuestionReadResponseDto responseDto = questionQueryService.readQuestion(memberId, questionId); return ResponseEntity.ok(responseDto); } diff --git a/src/main/java/com/smunity/server/domain/question/dto/QuestionReadResponseDto.java b/src/main/java/com/smunity/server/domain/question/dto/QuestionReadResponseDto.java new file mode 100644 index 0000000..8b2d77f --- /dev/null +++ b/src/main/java/com/smunity/server/domain/question/dto/QuestionReadResponseDto.java @@ -0,0 +1,37 @@ +package com.smunity.server.domain.question.dto; + +import com.smunity.server.domain.question.entity.Question; +import lombok.Builder; +import org.springframework.data.domain.Page; + +import java.time.LocalDateTime; + +@Builder +public record QuestionReadResponseDto( + Long id, + String title, + String content, + String author, + boolean isAuthor, + boolean answered, + LocalDateTime createdAt, + LocalDateTime updatedAt +) { + + public static QuestionReadResponseDto of(Long memberId, Question question) { + return QuestionReadResponseDto.builder() + .id(question.getId()) + .title(question.getTitle()) + .content(question.getContent()) + .author(question.getAuthor()) + .isAuthor(question.getIsAuthor(memberId)) + .answered(question.getAnswered()) + .createdAt(question.getCreatedAt()) + .updatedAt(question.getUpdatedAt()) + .build(); + } + + public static Page of(Long memberId, Page questions) { + return questions.map(question -> of(memberId, question)); + } +} diff --git a/src/main/java/com/smunity/server/domain/question/dto/QuestionResponseDto.java b/src/main/java/com/smunity/server/domain/question/dto/QuestionResponseDto.java index e37c3c9..64dce3d 100644 --- a/src/main/java/com/smunity/server/domain/question/dto/QuestionResponseDto.java +++ b/src/main/java/com/smunity/server/domain/question/dto/QuestionResponseDto.java @@ -2,10 +2,8 @@ import com.smunity.server.domain.question.entity.Question; import lombok.Builder; -import org.springframework.data.domain.Page; import java.time.LocalDateTime; -import java.util.List; @Builder public record QuestionResponseDto( @@ -29,14 +27,4 @@ public static QuestionResponseDto from(Question question) { .updatedAt(question.getUpdatedAt()) .build(); } - - public static List from(List questions) { - return questions.stream() - .map(QuestionResponseDto::from) - .toList(); - } - - public static Page from(Page questions) { - return questions.map(QuestionResponseDto::from); - } } diff --git a/src/main/java/com/smunity/server/domain/question/entity/Question.java b/src/main/java/com/smunity/server/domain/question/entity/Question.java index 69a6345..4776a85 100644 --- a/src/main/java/com/smunity/server/domain/question/entity/Question.java +++ b/src/main/java/com/smunity/server/domain/question/entity/Question.java @@ -54,6 +54,10 @@ public String getAuthor() { return anonymous ? "익명" : member.getName(); } + public boolean getIsAuthor(Long memberId) { + return member.getId().equals(memberId); + } + public boolean getAnswered() { return answer != null; } diff --git a/src/main/java/com/smunity/server/domain/question/service/QuestionQueryService.java b/src/main/java/com/smunity/server/domain/question/service/QuestionQueryService.java index 780c40b..b1ddd44 100644 --- a/src/main/java/com/smunity/server/domain/question/service/QuestionQueryService.java +++ b/src/main/java/com/smunity/server/domain/question/service/QuestionQueryService.java @@ -1,6 +1,6 @@ package com.smunity.server.domain.question.service; -import com.smunity.server.domain.question.dto.QuestionResponseDto; +import com.smunity.server.domain.question.dto.QuestionReadResponseDto; import com.smunity.server.domain.question.entity.Question; import com.smunity.server.domain.question.repository.QuestionRepository; import com.smunity.server.global.exception.GeneralException; @@ -18,14 +18,14 @@ public class QuestionQueryService { private final QuestionRepository questionRepository; - public Page readQuestions(Pageable pageable) { + public Page readQuestions(Long memberId, Pageable pageable) { Page questions = questionRepository.findAll(pageable); - return QuestionResponseDto.from(questions); + return QuestionReadResponseDto.of(memberId, questions); } - public QuestionResponseDto readQuestion(Long questionId) { + public QuestionReadResponseDto readQuestion(Long memberId, Long questionId) { Question question = questionRepository.findById(questionId) .orElseThrow(() -> new GeneralException(ErrorCode.QUESTION_NOT_FOUND)); - return QuestionResponseDto.from(question); + return QuestionReadResponseDto.of(memberId, question); } }