diff --git a/src/main/java/com/teamh/khumon/controller/LearningMaterialController.java b/src/main/java/com/teamh/khumon/controller/LearningMaterialController.java index 798c7ee..92f2f02 100644 --- a/src/main/java/com/teamh/khumon/controller/LearningMaterialController.java +++ b/src/main/java/com/teamh/khumon/controller/LearningMaterialController.java @@ -1,8 +1,10 @@ package com.teamh.khumon.controller; import com.teamh.khumon.dto.ModifyLearningMaterialRequest; +import com.teamh.khumon.dto.MyAnswerRequest; import com.teamh.khumon.service.LearningMaterialService; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.data.web.PageableDefault; @@ -17,6 +19,7 @@ @RestController @RequiredArgsConstructor +@Slf4j @RequestMapping("/api") public class LearningMaterialController { @@ -54,5 +57,12 @@ public ResponseEntity modifyLearningMaterials(Principal principal, @PathVaria public ResponseEntity deleteLearning(Principal principal,@PathVariable(name ="learning-material-id")Long id){ return learningMaterialService.delete(principal, id); } + + @PreAuthorize("isAuthenticated()") + @PostMapping("/learning-material/{learning-material-id}/question/{question-id}") + public ResponseEntity postMyAnswer(Principal principal, @PathVariable(name="learning-material-id") Long learningMaterialId, @PathVariable(name="question-id") Long myanswerId, @RequestBody MyAnswerRequest myAnswerRequest) throws Exception { + log.info("엔드포인트 지남"); + return learningMaterialService.postMyAnswer(principal, learningMaterialId, myanswerId, myAnswerRequest); + } } diff --git a/src/main/java/com/teamh/khumon/domain/MyAnswer.java b/src/main/java/com/teamh/khumon/domain/MyAnswer.java new file mode 100644 index 0000000..98d3d23 --- /dev/null +++ b/src/main/java/com/teamh/khumon/domain/MyAnswer.java @@ -0,0 +1,26 @@ +//package com.teamh.khumon.domain; +// +// +//import jakarta.persistence.*; +//import lombok.*; +// +//@Entity +//@Getter +//@Setter +//@AllArgsConstructor +//@NoArgsConstructor +//@Builder +//public class MyAnswer { +// @Id +// @GeneratedValue(strategy = GenerationType.IDENTITY) +// private Long id; +// +// @Column +// private String myAnswer; +// +// @Column +// private Boolean isCorrect; +// +// @OneToOne(mappedBy = "answer") +// private Question question; +//} diff --git a/src/main/java/com/teamh/khumon/domain/Question.java b/src/main/java/com/teamh/khumon/domain/Question.java index f1fc632..a4c4947 100644 --- a/src/main/java/com/teamh/khumon/domain/Question.java +++ b/src/main/java/com/teamh/khumon/domain/Question.java @@ -27,6 +27,16 @@ public class Question extends BaseEntity { @JoinColumn(name = "answer_id") private Answer answer; +// @OneToOne(orphanRemoval = true, cascade = CascadeType.REMOVE) +// @JoinColumn(name = "my_answer_id") +// private MyAnswer myAnswer; + + private String myAnswer; + private String whatWrong; + private Boolean isCorrect; + + + @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "member_id") @ToString.Exclude diff --git a/src/main/java/com/teamh/khumon/dto/MyAnswerAIResponse.java b/src/main/java/com/teamh/khumon/dto/MyAnswerAIResponse.java new file mode 100644 index 0000000..8d057ab --- /dev/null +++ b/src/main/java/com/teamh/khumon/dto/MyAnswerAIResponse.java @@ -0,0 +1,19 @@ +package com.teamh.khumon.dto; + + +import lombok.*; + +@Builder +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +@ToString +public class MyAnswerAIResponse { + private String assessment; + private Boolean correct; +} + + +//question : STring +//answer: STring diff --git a/src/main/java/com/teamh/khumon/dto/MyAnswerRequest.java b/src/main/java/com/teamh/khumon/dto/MyAnswerRequest.java new file mode 100644 index 0000000..8c84f2f --- /dev/null +++ b/src/main/java/com/teamh/khumon/dto/MyAnswerRequest.java @@ -0,0 +1,16 @@ +package com.teamh.khumon.dto; + + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@NoArgsConstructor +@AllArgsConstructor +@Getter +@Setter +public class MyAnswerRequest { + + private String content; +} diff --git a/src/main/java/com/teamh/khumon/dto/QuestionInformation.java b/src/main/java/com/teamh/khumon/dto/QuestionInformation.java index 2cae044..df50e6f 100644 --- a/src/main/java/com/teamh/khumon/dto/QuestionInformation.java +++ b/src/main/java/com/teamh/khumon/dto/QuestionInformation.java @@ -10,5 +10,8 @@ public class QuestionInformation { private Long id; private String content; + private String myAnswer; + private boolean isCorrect; + private String whatWrong; private String answer; } diff --git a/src/main/java/com/teamh/khumon/repository/MyAnswerRepository.java b/src/main/java/com/teamh/khumon/repository/MyAnswerRepository.java new file mode 100644 index 0000000..c9c3aca --- /dev/null +++ b/src/main/java/com/teamh/khumon/repository/MyAnswerRepository.java @@ -0,0 +1,8 @@ +//package com.teamh.khumon.repository; +// +//import com.teamh.khumon.domain.MyAnswer; +//import org.springframework.data.jpa.repository.JpaRepository; +// +//public interface MyAnswerRepository extends JpaRepository { +// +//} diff --git a/src/main/java/com/teamh/khumon/repository/QuestionRepository.java b/src/main/java/com/teamh/khumon/repository/QuestionRepository.java index 4ad0480..edebe3b 100644 --- a/src/main/java/com/teamh/khumon/repository/QuestionRepository.java +++ b/src/main/java/com/teamh/khumon/repository/QuestionRepository.java @@ -3,5 +3,8 @@ import com.teamh.khumon.domain.Question; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.Optional; + public interface QuestionRepository extends JpaRepository { + Optional findById(Long id); } diff --git a/src/main/java/com/teamh/khumon/service/LearningMaterialService.java b/src/main/java/com/teamh/khumon/service/LearningMaterialService.java index 3da3bda..8aa6490 100644 --- a/src/main/java/com/teamh/khumon/service/LearningMaterialService.java +++ b/src/main/java/com/teamh/khumon/service/LearningMaterialService.java @@ -8,6 +8,7 @@ import com.teamh.khumon.dto.*; import com.teamh.khumon.repository.LearningMaterialRepository; import com.teamh.khumon.repository.MemberRepository; +import com.teamh.khumon.repository.QuestionRepository; import com.teamh.khumon.util.AmazonS3Util; import com.teamh.khumon.util.MediaUtil; import com.teamh.khumon.util.ObjectToDtoUtil; @@ -41,7 +42,7 @@ public class LearningMaterialService { private final QuestionAnswerService questionAnswerService; - + private final QuestionRepository questionRepository; private final MediaUtil mediaUtil; private final AmazonS3Util amazonS3Util; @@ -103,6 +104,9 @@ public ResponseEntity getLearningMaterial(Long id, Principal principal) { List responses = learningMaterial.getQuestions().stream().map(question-> QuestionInformation.builder() .id(question.getId()) .content(question.getContent()) + .myAnswer(question.getMyAnswer() == null ? null : question.getMyAnswer()) + .isCorrect(question.getIsCorrect() != null && question.getIsCorrect()) + .whatWrong(question.getWhatWrong() == null ? null : question.getWhatWrong()) .answer(question.getAnswer().getAnswer()) .build()).toList(); @@ -197,4 +201,20 @@ private Specification search(String kw, Long memberId) { }; } + public ResponseEntity postMyAnswer(Principal principal, Long learningMaterialId, Long questionId, MyAnswerRequest myAnswerRequest) throws Exception { + LearningMaterial learningMaterial = learningMaterialRepository.findById(learningMaterialId).orElseThrow(); + if(!principal.getName().equals(learningMaterial.getMember().getUsername())){ + throw new RuntimeException("작성자가 아님"); + } + Question question = questionRepository.findById(questionId).orElseThrow(); + String myanswerResponse = mediaUtil.postToLLMforUserAnswer(myAnswerRequest, question.getContent()); + MyAnswerAIResponse myAnswerAIResponse = (MyAnswerAIResponse) new ObjectToDtoUtil().jsonStrToObj(myanswerResponse, MyAnswerAIResponse.class); + log.info(myAnswerAIResponse.toString()); + + Long id = questionAnswerService.postMyAnswer(questionId, myAnswerRequest, myAnswerAIResponse); + Map response = new HashMap<>(); + response.put("questionId", id); + return new ResponseEntity<>(response, HttpStatus.CREATED); + } + } \ No newline at end of file diff --git a/src/main/java/com/teamh/khumon/service/QuestionAnswerService.java b/src/main/java/com/teamh/khumon/service/QuestionAnswerService.java index 60f572e..9be6ef7 100644 --- a/src/main/java/com/teamh/khumon/service/QuestionAnswerService.java +++ b/src/main/java/com/teamh/khumon/service/QuestionAnswerService.java @@ -1,12 +1,12 @@ package com.teamh.khumon.service; -import com.teamh.khumon.domain.Answer; -import com.teamh.khumon.domain.LearningMaterial; -import com.teamh.khumon.domain.Member; -import com.teamh.khumon.domain.Question; +import com.teamh.khumon.domain.*; import com.teamh.khumon.dto.AIResponse; +import com.teamh.khumon.dto.MyAnswerAIResponse; +import com.teamh.khumon.dto.MyAnswerRequest; import com.teamh.khumon.dto.Problem; import com.teamh.khumon.repository.AnswerRepository; + import com.teamh.khumon.repository.QuestionRepository; import lombok.Builder; import lombok.RequiredArgsConstructor; @@ -26,6 +26,8 @@ public class QuestionAnswerService { private final AnswerRepository answerRepository; +// private final MyAnswerRepository myAnswerRepository; + @Transactional public List saveQuestionAndAnswer(AIResponse aiResponse, Member member, LearningMaterial learningMaterial){ List problems = aiResponse.getProblems(); @@ -52,4 +54,14 @@ void deleteQuestionAndAnswer(LearningMaterial learningMaterial){ questionRepository.deleteAll(questionList); } + + @Transactional + public Long postMyAnswer(Long questionId, MyAnswerRequest myAnswerRequest, MyAnswerAIResponse myAnswerAIResponse) { + Question question = questionRepository.findById(questionId).orElseThrow(); + question.setMyAnswer(myAnswerRequest.getContent()); + question.setIsCorrect(myAnswerAIResponse.getCorrect()); + question.setWhatWrong(myAnswerAIResponse.getAssessment()); + questionRepository.save(question); + return questionId; + } } diff --git a/src/main/java/com/teamh/khumon/util/MediaUtil.java b/src/main/java/com/teamh/khumon/util/MediaUtil.java index d9fa13e..85b96cd 100644 --- a/src/main/java/com/teamh/khumon/util/MediaUtil.java +++ b/src/main/java/com/teamh/khumon/util/MediaUtil.java @@ -1,6 +1,8 @@ package com.teamh.khumon.util; import com.teamh.khumon.domain.MediaFileType; +import com.teamh.khumon.domain.Question; +import com.teamh.khumon.dto.MyAnswerRequest; import lombok.extern.slf4j.Slf4j; import org.apache.commons.io.FileUtils; import org.json.JSONException; @@ -153,6 +155,26 @@ public String postToLLMforVideo(MultipartFile multipartFile) throws Exception { } + public String postToLLMforUserAnswer(MyAnswerRequest myAnswerRequest, String question) throws Exception { + String postUrl = "http://facerain-dev.iptime.org:5000/api/v1/generation/assessment"; + RestTemplate restTemplate = new RestTemplate(); + + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.setContentType(MediaType.APPLICATION_JSON); + JSONObject jsonObject = new JSONObject(); + jsonObject.put("question", question); + jsonObject.put("answer", myAnswerRequest.getContent()); + +//question : STring +//answer: STring + HttpEntity requestHttpEntity = + new HttpEntity<>(jsonObject.toString(), httpHeaders); + String response = restTemplate.postForObject(postUrl, requestHttpEntity, String.class); + log.info(response); + return response; + } + + public void deleteMediaFile(String filePath) { File savedFile = new File(filePath);