Skip to content

Commit

Permalink
fix foreign key
Browse files Browse the repository at this point in the history
  • Loading branch information
zmh-program committed Aug 13, 2023
1 parent 9c6244b commit f07d22b
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 16 deletions.
2 changes: 1 addition & 1 deletion api/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func ChatAPI(c *gin.Context) {
}

db := c.MustGet("db").(*sql.DB)
instance := conversation.NewConversation(db, user.ID)
instance := conversation.NewConversation(db, user.GetID(db))

for {
_, message, err = conn.ReadMessage()
Expand Down
4 changes: 2 additions & 2 deletions api/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ func ChatWithWeb(message []types.ChatGPTMessage, long bool) (string, []types.Cha

data := web.SearchBing(keyword)
if long {
data = data[:6000]
data = utils.GetSegmentString(data, 6000)
} else {
data = data[:3000]
data = utils.GetSegmentString(data, 3000)
}
return keyword, utils.Insert(message, 0, types.ChatGPTMessage{
Role: "system",
Expand Down
2 changes: 1 addition & 1 deletion app/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function toggle(n: boolean) {
<span>GPT-4</span>
</div>
</div>
<a class="donate-container" target="_blank" href="https://zmh-program.site/donate">
<a class="donate-container" target="_blank" href="https://zmh-program.site/donate" v-if="!auth">
<heart />
捐助我们
</a>
Expand Down
10 changes: 10 additions & 0 deletions auth/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ func (u *User) GenerateToken() string {
return token
}

func (u *User) GetID(db *sql.DB) int64 {
if u.ID > 0 {
return u.ID
}
if err := db.QueryRow("SELECT id FROM auth WHERE username = ?", u.Username).Scan(&u.ID); err != nil {
return 0
}
return u.ID
}

func IsUserExist(db *sql.DB, username string) bool {
var count int
if err := db.QueryRow("SELECT COUNT(*) FROM auth WHERE username = ?", username).Scan(&count); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion connection/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func CreateConversationTable(db *sql.DB) {
CREATE TABLE IF NOT EXISTS conversation (
id INT PRIMARY KEY AUTO_INCREMENT,
user_id INT,
conversation_id INT,
conversation_id INT UNIQUE,
conversation_name VARCHAR(255),
data TEXT,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
Expand Down
13 changes: 2 additions & 11 deletions conversation/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,8 @@ import (

func (c *Conversation) SaveConversation(db *sql.DB) bool {
data := utils.ToJson(c.GetMessage())
_, err := db.Exec(`
INSERT INTO conversation (
user_id,
conversation_id,
conversation_name,
data
) VALUES (?, ?, ?, ?)
ON DUPLICATE KEY UPDATE
conversation_name = VALUES(conversation_name),
data = VALUES(data)
`, c.GetUserID(), c.GetId(), c.GetName(), data)
_, err := db.Exec("INSERT INTO conversation (user_id, conversation_id, conversation_name, data) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE conversation_name = ?, data = ?", c.UserID, c.Id, c.Name, data, c.Name, data)

if err != nil {
return false
}
Expand Down
14 changes: 14 additions & 0 deletions utils/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@ func UnmarshalJson[T any](value string) T {
}
}

func GetSegment[T any](arr []T, length int) []T {
if length > len(arr) {
return arr
}
return arr[:length]
}

func GetSegmentString(arr string, length int) string {
if length > len(arr) {
return ""
}
return arr[:length]
}

func GetLatestSegment[T any](arr []T, length int) []T {
if length > len(arr) {
return arr
Expand Down

0 comments on commit f07d22b

Please sign in to comment.