mirror of
https://github.com/coaidev/coai.git
synced 2025-06-01 19:30:19 +09:00
fix multi-user unique fields
This commit is contained in:
parent
f2be2ba82a
commit
dff51128d9
@ -98,10 +98,11 @@ func CreateConversationTable(db *sql.DB) {
|
|||||||
CREATE TABLE IF NOT EXISTS conversation (
|
CREATE TABLE IF NOT EXISTS conversation (
|
||||||
id INT PRIMARY KEY AUTO_INCREMENT,
|
id INT PRIMARY KEY AUTO_INCREMENT,
|
||||||
user_id INT,
|
user_id INT,
|
||||||
conversation_id INT UNIQUE,
|
conversation_id INT,
|
||||||
conversation_name VARCHAR(255),
|
conversation_name VARCHAR(255),
|
||||||
data TEXT,
|
data TEXT,
|
||||||
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
UNIQUE KEY (user_id, conversation_id)
|
||||||
);
|
);
|
||||||
`)
|
`)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -4,18 +4,30 @@ import (
|
|||||||
"chat/types"
|
"chat/types"
|
||||||
"chat/utils"
|
"chat/utils"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (c *Conversation) SaveConversation(db *sql.DB) bool {
|
func (c *Conversation) SaveConversation(db *sql.DB) bool {
|
||||||
data := utils.ToJson(c.GetMessage())
|
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 = ?, data = ?", c.UserID, c.Id, c.Name, data, c.Name, data)
|
query := "INSERT INTO conversation (user_id, conversation_id, conversation_name, data) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE conversation_name = VALUES(conversation_name), data = VALUES(data)"
|
||||||
|
|
||||||
|
stmt, err := db.Prepare(query)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
defer func(stmt *sql.Stmt) {
|
||||||
|
err := stmt.Close()
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
|
}(stmt)
|
||||||
|
|
||||||
|
_, err = stmt.Exec(c.UserID, c.Id, c.Name, data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetConversationLengthByUserID(db *sql.DB, userId int64) int64 {
|
func GetConversationLengthByUserID(db *sql.DB, userId int64) int64 {
|
||||||
var length int64
|
var length int64
|
||||||
err := db.QueryRow("SELECT COUNT(*) FROM conversation WHERE user_id = ?", userId).Scan(&length)
|
err := db.QueryRow("SELECT COUNT(*) FROM conversation WHERE user_id = ?", userId).Scan(&length)
|
||||||
|
Loading…
Reference in New Issue
Block a user