Relay done but not working

This commit is contained in:
JustSong
2023-04-23 18:24:11 +08:00
parent 9fc375c604
commit 852af57c03
12 changed files with 225 additions and 70 deletions

View File

@@ -2,12 +2,13 @@ package model
import (
_ "gorm.io/driver/sqlite"
"one-api/common"
)
type Channel struct {
Id int `json:"id"`
Type int `json:"type" gorm:"default:0"`
Key string `json:"key"`
Key string `json:"key" gorm:"not null"`
Status int `json:"status" gorm:"default:1"`
Name string `json:"name" gorm:"index"`
Weight int `json:"weight"`
@@ -27,10 +28,26 @@ func SearchChannels(keyword string) (channels []*Channel, err error) {
return channels, err
}
func GetChannelById(id int) (*Channel, error) {
func GetChannelById(id int, selectAll bool) (*Channel, error) {
channel := Channel{Id: id}
var err error = nil
err = DB.Omit("key").First(&channel, "id = ?", id).Error
if selectAll {
err = DB.First(&channel, "id = ?", id).Error
} else {
err = DB.Omit("key").First(&channel, "id = ?", id).Error
}
return &channel, err
}
func GetRandomChannel() (*Channel, error) {
// TODO: consider weight
channel := Channel{}
var err error = nil
if common.UsingSQLite {
err = DB.Where("status = ?", common.ChannelStatusEnabled).Order("RANDOM()").Limit(1).First(&channel).Error
} else {
err = DB.Where("status = ?", common.ChannelStatusEnabled).Order("RAND()").Limit(1).First(&channel).Error
}
return &channel, err
}