@@ -46,26 +46,49 @@ async def grade_answers(
46
46
scores = outputs ["scores" ].tolist () # [창의, 논리, 사고, 설득, 깊이]
47
47
reviews = outputs ["explanations" ] # ["창의성 설명", "논리 설명", ...]
48
48
49
- # 5. DB에 저장
50
- new_answer = Answer (
51
- user_id = user_id ,
52
- question_id = question_id ,
53
- content = input_text ,
54
- creativity = scores [0 ],
55
- logic = scores [1 ],
56
- thinking = scores [2 ],
57
- persuasion = scores [3 ],
58
- depth = scores [4 ],
59
- creativity_review = reviews [0 ],
60
- logic_review = reviews [1 ],
61
- thinking_review = reviews [2 ],
62
- persuasion_review = reviews [3 ],
63
- depth_review = reviews [4 ],
64
- total_score = (creativity + logic + thinking + persuasion + depth )/ 5
65
- )
49
+ total_score = sum (scores ) / len (scores ) if scores else 0
66
50
67
- db .add (new_answer )
51
+ # 5. DB에 저장 : 기존 답변 O (값 update), X (db에 새로 add)
52
+ existing_answer = db .query (Answer ).filter (
53
+ Answer .user_id == user_id ,
54
+ Answer .question_id == question_id
55
+ ).first ()
56
+
57
+ if existing_answer :
58
+ # 기존 데이터가 있으면 업데이트
59
+ existing_answer .content = input_text
60
+ existing_answer .creativity = scores [0 ]
61
+ existing_answer .logic = scores [1 ]
62
+ existing_answer .thinking = scores [2 ]
63
+ existing_answer .persuasion = scores [3 ]
64
+ existing_answer .depth = scores [4 ]
65
+ existing_answer .creativity_review = reviews [0 ]
66
+ existing_answer .logic_review = reviews [1 ]
67
+ existing_answer .thinking_review = reviews [2 ]
68
+ existing_answer .persuasion_review = reviews [3 ]
69
+ existing_answer .depth_review = reviews [4 ]
70
+ existing_answer .total_score = total_score
71
+ else :
72
+ # 기존 데이터가 없으면 새로 추가
73
+ new_answer = Answer (
74
+ user_id = user_id ,
75
+ question_id = question_id ,
76
+ content = input_text ,
77
+ creativity = scores [0 ],
78
+ logic = scores [1 ],
79
+ thinking = scores [2 ],
80
+ persuasion = scores [3 ],
81
+ depth = scores [4 ],
82
+ creativity_review = reviews [0 ],
83
+ logic_review = reviews [1 ],
84
+ thinking_review = reviews [2 ],
85
+ persuasion_review = reviews [3 ],
86
+ depth_review = reviews [4 ],
87
+ total_score = total_score
88
+ )
89
+ db .add (new_answer )
90
+
91
+ # DB 반영
68
92
db .commit ()
69
- db .refresh (new_answer )
70
93
71
94
return # 응답 없이 종료 / FastAPI는 자동으로 200 OK 반환
0 commit comments