Skip to content

Commit

Permalink
feat: 사용자 정답 현황 (#63)
Browse files Browse the repository at this point in the history
* feat: 검색 기능 확장

* fix: nginx maxfilesize 변경

* feat: content type 반환

* feat: 사용자 정답 여부 반환
  • Loading branch information
Martin0o0 authored Nov 20, 2023
1 parent f3d8e09 commit 0b25304
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,6 +19,7 @@

@RestController
@RequiredArgsConstructor
@Slf4j
@RequestMapping("/api")
public class LearningMaterialController {

Expand Down Expand Up @@ -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);
}
}

26 changes: 26 additions & 0 deletions src/main/java/com/teamh/khumon/domain/MyAnswer.java
Original file line number Diff line number Diff line change
@@ -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;
//}
10 changes: 10 additions & 0 deletions src/main/java/com/teamh/khumon/domain/Question.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/teamh/khumon/dto/MyAnswerAIResponse.java
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions src/main/java/com/teamh/khumon/dto/MyAnswerRequest.java
Original file line number Diff line number Diff line change
@@ -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;
}
3 changes: 3 additions & 0 deletions src/main/java/com/teamh/khumon/dto/QuestionInformation.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
@@ -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<MyAnswer, Long> {
//
//}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Question, Long> {
Optional<Question> findById(Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -41,7 +42,7 @@ public class LearningMaterialService {

private final QuestionAnswerService questionAnswerService;


private final QuestionRepository questionRepository;
private final MediaUtil mediaUtil;

private final AmazonS3Util amazonS3Util;
Expand Down Expand Up @@ -103,6 +104,9 @@ public ResponseEntity<?> getLearningMaterial(Long id, Principal principal) {
List<QuestionInformation> 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();

Expand Down Expand Up @@ -197,4 +201,20 @@ private Specification<LearningMaterial> 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<String, Long> response = new HashMap<>();
response.put("questionId", id);
return new ResponseEntity<>(response, HttpStatus.CREATED);
}

}
20 changes: 16 additions & 4 deletions src/main/java/com/teamh/khumon/service/QuestionAnswerService.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -26,6 +26,8 @@ public class QuestionAnswerService {

private final AnswerRepository answerRepository;

// private final MyAnswerRepository myAnswerRepository;

@Transactional
public List<Question> saveQuestionAndAnswer(AIResponse aiResponse, Member member, LearningMaterial learningMaterial){
List<Problem> problems = aiResponse.getProblems();
Expand All @@ -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;
}
}
22 changes: 22 additions & 0 deletions src/main/java/com/teamh/khumon/util/MediaUtil.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<String> 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);
Expand Down

0 comments on commit 0b25304

Please sign in to comment.