-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQnAController.java
More file actions
72 lines (65 loc) · 2.98 KB
/
QnAController.java
File metadata and controls
72 lines (65 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.example.web;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
import java.util.Random;
@RestController
@RequestMapping("/api/questions") // 질문 관련 API 경로
public class QnAController {
@Autowired
private QuestionRepository questionRepository;
@Autowired
private AnswerRepository answerRepository;
// 랜덤 질문 제공 API
@Operation(
summary = "랜덤 질문 가져오기",
description = "데이터베이스에서 무작위로 선택된 질문을 제공합니다."
)
@ApiResponses({
@ApiResponse(responseCode = "200", description = "성공적으로 랜덤 질문 반환",
content = @Content(schema = @Schema(implementation = Question.class)))),
@ApiResponse(responseCode = "404", description = "질문 데이터 없음",
content = @Content(schema = @Schema(example = "{\"status\": 404, \"message\": \"질문 데이터가 없습니다.\"}"))))
})
@GetMapping("/") // /questions 경로로 수정
public ResponseEntity<Object> getRandomQuestion() {
List<Question> questions = questionRepository.findAll();
if (questions.isEmpty()) {
return ResponseEntity.status(404).body(Map.of(
"status", 404,
"message", "질문 데이터가 없습니다."
));
}
Question randomQuestion = questions.get(new Random().nextInt(questions.size()));
return ResponseEntity.ok(randomQuestion);
}
// 답변 저장 API
@Operation(
summary = "답변 저장하기",
description = "사용자가 작성한 답변을 저장합니다."
)
@ApiResponses({
@ApiResponse(responseCode = "200", description = "답변 저장 완료",
content = @Content(schema = @Schema(implementation = Answer.class)))),
@ApiResponse(responseCode = "400", description = "잘못된 요청",
content = @Content(schema = @Schema(example = "{\"status\": 400, \"message\": \"잘못된 요청입니다.\"}"))))
})
@PostMapping("/responses") // /responses 경로로 수정
public ResponseEntity<Object> saveAnswer(@RequestBody Answer answer) {
if (answer == null || answer.getContent() == null || answer.getContent().isEmpty()) {
return ResponseEntity.badRequest().body(Map.of(
"status", 400,
"message", "잘못된 요청입니다. 답변 내용을 입력해주세요."
));
}
Answer savedAnswer = answerRepository.save(answer);
return ResponseEntity.ok(savedAnswer);
}
}