Skip to content

Commit

Permalink
[FEATURE] 질문 조회 API 스펙 변경 (#89)
Browse files Browse the repository at this point in the history
feat: 질문 조회 API 스펙 변경 (#86)
  • Loading branch information
hyunmin0317 authored Oct 16, 2024
1 parent 415dc70 commit 0741875
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -26,8 +27,8 @@ public class QuestionController {
private final QuestionCommandService questionCommandService;

@GetMapping
public ResponseEntity<Page<QuestionResponseDto>> readQuestions(@ParameterObject @PageableDefault(size = 5, sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable) {
Page<QuestionResponseDto> responseDtoPage = questionQueryService.readQuestions(pageable);
public ResponseEntity<Page<QuestionReadResponseDto>> readQuestions(@AuthMember Long memberId, @ParameterObject @PageableDefault(size = 5, sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable) {
Page<QuestionReadResponseDto> responseDtoPage = questionQueryService.readQuestions(memberId, pageable);
return ResponseEntity.ok(responseDtoPage);
}

Expand All @@ -38,8 +39,8 @@ public ResponseEntity<QuestionResponseDto> createQuestion(@AuthMember Long membe
}

@GetMapping("/{questionId}")
public ResponseEntity<QuestionResponseDto> readQuestion(@PathVariable Long questionId) {
QuestionResponseDto responseDto = questionQueryService.readQuestion(questionId);
public ResponseEntity<QuestionReadResponseDto> readQuestion(@AuthMember Long memberId, @PathVariable Long questionId) {
QuestionReadResponseDto responseDto = questionQueryService.readQuestion(memberId, questionId);
return ResponseEntity.ok(responseDto);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<QuestionReadResponseDto> of(Long memberId, Page<Question> questions) {
return questions.map(question -> of(memberId, question));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -29,14 +27,4 @@ public static QuestionResponseDto from(Question question) {
.updatedAt(question.getUpdatedAt())
.build();
}

public static List<QuestionResponseDto> from(List<Question> questions) {
return questions.stream()
.map(QuestionResponseDto::from)
.toList();
}

public static Page<QuestionResponseDto> from(Page<Question> questions) {
return questions.map(QuestionResponseDto::from);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -18,14 +18,14 @@ public class QuestionQueryService {

private final QuestionRepository questionRepository;

public Page<QuestionResponseDto> readQuestions(Pageable pageable) {
public Page<QuestionReadResponseDto> readQuestions(Long memberId, Pageable pageable) {
Page<Question> 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);
}
}

0 comments on commit 0741875

Please sign in to comment.