Skip to content

Commit

Permalink
fix: modify the type of token models to be text (songquanpeng#1761)
Browse files Browse the repository at this point in the history
* fix: modify the type of token models to be text

* chore: update receiver name

---------

Co-authored-by: JustSong <[email protected]>
  • Loading branch information
2 people authored and mxdlzg committed Oct 15, 2024
1 parent 08a8691 commit e04626f
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions model/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type Token struct {
RemainQuota int64 `json:"remain_quota" gorm:"bigint;default:0"`
UnlimitedQuota bool `json:"unlimited_quota" gorm:"default:false"`
UsedQuota int64 `json:"used_quota" gorm:"bigint;default:0"` // used quota
Models *string `json:"models" gorm:"default:''"` // allowed models
Models *string `json:"models" gorm:"type:text"` // allowed models
Subnet *string `json:"subnet" gorm:"default:''"` // allowed subnet
}

Expand Down Expand Up @@ -121,30 +121,40 @@ func GetTokenById(id int) (*Token, error) {
return &token, err
}

func (token *Token) Insert() error {
func (t *Token) Insert() error {
var err error
err = DB.Create(token).Error
err = DB.Create(t).Error
return err
}

// Update Make sure your token's fields is completed, because this will update non-zero values
func (token *Token) Update() error {
func (t *Token) Update() error {
var err error
err = DB.Model(token).Select("name", "status", "expired_time", "remain_quota", "unlimited_quota", "models", "subnet").Updates(token).Error
err = DB.Model(t).Select("name", "status", "expired_time", "remain_quota", "unlimited_quota", "models", "subnet").Updates(t).Error
return err
}

func (token *Token) SelectUpdate() error {
func (t *Token) SelectUpdate() error {
// This can update zero values
return DB.Model(token).Select("accessed_time", "status").Updates(token).Error
return DB.Model(t).Select("accessed_time", "status").Updates(t).Error
}

func (token *Token) Delete() error {
func (t *Token) Delete() error {
var err error
err = DB.Delete(token).Error
err = DB.Delete(t).Error
return err
}

func (t *Token) GetModels() string {
if t == nil {
return ""
}
if t.Models == nil {
return ""
}
return *t.Models
}

func DeleteTokenById(id int, userId int) (err error) {
// Why we need userId here? In case user want to delete other's token.
if id == 0 || userId == 0 {
Expand Down

0 comments on commit e04626f

Please sign in to comment.