-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathask.go
58 lines (48 loc) · 1.36 KB
/
ask.go
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
package question
import (
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"hduhelp_text/db"
"hduhelp_text/utils"
"net/http"
)
func Ask(c *gin.Context) {
session := sessions.Default(c)
if auth, ok := session.Get("authenticated").(bool); !ok || !auth {
c.JSON(http.StatusUnauthorized, gin.H{"error": "未登录"})
return
}
username, ok := session.Get("username").(string)
if !ok || username == "" {
c.JSON(http.StatusUnauthorized, gin.H{"error": "用户名不存在"})
return
}
questionText := c.PostForm("question")
if questionText == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "问题内容不能为空"})
return
}
newQuestion := db.Question{
QuestionText: questionText,
Questioner: username,
}
if err := db.DB.Create(&newQuestion).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
answerText, err := utils.GenerateAIAnswer(questionText)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "无法生成答案"})
return
}
answer := db.Answer{
QuestionID: newQuestion.ID, // 确保这里是 uint 类型
AnswerText: answerText,
Respondent: "ai",
}
if err := db.DB.Create(&answer).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "答案保存失败"})
return
}
c.JSON(http.StatusOK, gin.H{"message": "提问成功"})
}