42 lines
667 B
Go
42 lines
667 B
Go
package db
|
|
|
|
import "gorm.io/gorm"
|
|
|
|
type Key struct {
|
|
gorm.Model
|
|
ApiKey string
|
|
UserId string
|
|
}
|
|
|
|
// 添加记录
|
|
func AddKey(apiKey string, userId string) error {
|
|
key := Key{
|
|
ApiKey: apiKey,
|
|
UserId: userId,
|
|
}
|
|
if err := db.Create(&key).Error; err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// 删除记录
|
|
func DeleteKey(id uint) error {
|
|
if err := db.Delete(&Key{}, id).Error; err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// 更新记录
|
|
func UpdateKey(id uint, apiKey string, userId string) error {
|
|
key := Key{
|
|
ApiKey: apiKey,
|
|
UserId: userId,
|
|
}
|
|
if err := db.Model(&Key{}).Where("id = ?", id).Updates(key).Error; err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|