Skip to content

Commit

Permalink
Merge pull request #4 from ne-o-rdinary/refactor/refactoring
Browse files Browse the repository at this point in the history
refactor : uuid 부여 & response 변경
  • Loading branch information
OZIIJIN authored Nov 23, 2024
2 parents a598cf3 + 445f3ea commit 127b930
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ public class AnswerController {

@PostMapping()
@Operation(summary = "답변 저장 API", description = "답변을 저장하는 API")
public ResponseEntity<ApiResponse<AnswerCreateResponseDto>> createAnswer(
public ApiResponse<AnswerCreateResponseDto> createAnswer(
@Valid @RequestBody AnswerRequestDto dto) {

AnswerCreateResponseDto answerCreateResponseDto = answerService.createAnswer(dto);

return ResponseEntity.ok(ApiResponse.onSuccess(answerCreateResponseDto));
return ApiResponse.onSuccess(answerCreateResponseDto);
}

@GetMapping("/{answerId}")
@Operation(summary = "답변 조회 API", description = "답변을 조회하는 API")
public ResponseEntity<ApiResponse<AnswerResponseDto>> getAnswer(@PathVariable Long answerId) {
AnswerResponseDto answerResponseDto = answerService.getAnswer(answerId);
@GetMapping("/{uuid}")
@Operation(summary = "질문/답변 조회 API", description = "질문/답변 조회하는 API")
public ApiResponse<AnswerResponseDto> getAnswer(@PathVariable String uuid) {
AnswerResponseDto answerResponseDto = answerService.getAnswer(uuid);

return ResponseEntity.ok(ApiResponse.onSuccess(answerResponseDto));
return ApiResponse.onSuccess(answerResponseDto);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

public class AnswerCreateResponseDto {

private Long answerId;
private String uuid;

public AnswerCreateResponseDto(Answer answer) {
this.answerId = answer.getId();
public AnswerCreateResponseDto(String uuid) {
this.uuid = uuid;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

public class AnswerResponseDto {

private String question;

private String answer;

public AnswerResponseDto(String answer) {
public AnswerResponseDto(String question, String answer) {
this.question = question;
this.answer = answer;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.example.demo.domain.entity.answer.Answer;
import com.example.demo.domain.entity.question.Question;
import jakarta.persistence.*;
import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -27,8 +28,12 @@ public class QuestionAnswer extends BaseEntity {
@JoinColumn(name = "answer_id")
private Answer answer;

public QuestionAnswer(Question question, Answer answer) {
@Column
private String uuid;

public QuestionAnswer(Question question, Answer answer, String uuid) {
this.question = question;
this.answer = answer;
this.uuid = uuid;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.example.demo.repository.question_answer;

import com.example.demo.domain.entity.question_answer.QuestionAnswer;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;

public interface QuestionAnswerRepository extends JpaRepository<QuestionAnswer, Long> {

Optional<QuestionAnswer> findBy(String uuid);
}
14 changes: 9 additions & 5 deletions src/main/java/com/example/demo/service/answer/AnswerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.example.demo.repository.answer.AnswerRepository;
import com.example.demo.repository.question.QuestionRepository;
import com.example.demo.repository.question_answer.QuestionAnswerRepository;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
Expand All @@ -34,17 +35,20 @@ public AnswerCreateResponseDto createAnswer(AnswerRequestDto dto) {
Answer answer = new Answer(dto.getAnswer());
answerRepository.save(answer);

QuestionAnswer questionAnswer = new QuestionAnswer(question, answer);
String uuid = UUID.randomUUID().toString();

QuestionAnswer questionAnswer = new QuestionAnswer(question, answer, uuid);
questionAnswerRepository.save(questionAnswer);

return new AnswerCreateResponseDto(answer);
return new AnswerCreateResponseDto(questionAnswer.getUuid());
}

@Transactional(readOnly = true)
public AnswerResponseDto getAnswer(Long answerId) {
Answer answer = answerRepository.findById(answerId).orElseThrow(
public AnswerResponseDto getAnswer(String uuid) {
QuestionAnswer questionAnswer = questionAnswerRepository.findBy(uuid).orElseThrow(
() -> new GeneralException(ErrorStatus.ANSWER_NOT_FOUND.getReasonHttpStatus()));

return new AnswerResponseDto(answer.getAnswer());
return new AnswerResponseDto(questionAnswer.getQuestion().getQuestion(),
questionAnswer.getAnswer().getAnswer());
}
}

0 comments on commit 127b930

Please sign in to comment.