Files
server/database/client.go
饺子w (Yumechi) 496c166981 chore: Migrate github.com/jinzhu/gorm to gorm.io/gorm (#863)
* chore: Migrate github.com/jinzhu/gorm to gorm.io/gorm

Signed-off-by: eternal-flame-AD <yume@yumechi.jp>

* chore: drop singleton connection limit on sqlite3 backend

Signed-off-by: eternal-flame-AD <yume@yumechi.jp>

* enhance: database logging

Signed-off-by: eternal-flame-AD <yume@yumechi.jp>

* Revert "chore: drop singleton connection limit on sqlite3 backend"

This reverts commit b494a3bd1f.

Signed-off-by: eternal-flame-AD <yume@yumechi.jp>

* typo

Signed-off-by: eternal-flame-AD <yume@yumechi.jp>

* rename unique_index -> uniqueIndex

Signed-off-by: eternal-flame-AD <yume@yumechi.jp>

* drop uniqueIndex on primary key

Signed-off-by: eternal-flame-AD <yume@yumechi.jp>

* migrate fully to new gorm tag format

Signed-off-by: eternal-flame-AD <yume@yumechi.jp>

* specify unique index name

Signed-off-by: eternal-flame-AD <yume@yumechi.jp>

* remove pluginConf duplicate index

Signed-off-by: eternal-flame-AD <yume@yumechi.jp>

* disable auto migrate FK

Signed-off-by: eternal-flame-AD <yume@yumechi.jp>

---------

Signed-off-by: eternal-flame-AD <yume@yumechi.jp>
2025-10-29 22:23:10 +00:00

65 lines
1.7 KiB
Go

package database
import (
"time"
"github.com/gotify/server/v2/model"
"gorm.io/gorm"
)
// GetClientByID returns the client for the given id or nil.
func (d *GormDatabase) GetClientByID(id uint) (*model.Client, error) {
client := new(model.Client)
err := d.DB.Where("id = ?", id).Find(client).Error
if err == gorm.ErrRecordNotFound {
err = nil
}
if client.ID == id {
return client, err
}
return nil, err
}
// GetClientByToken returns the client for the given token or nil.
func (d *GormDatabase) GetClientByToken(token string) (*model.Client, error) {
client := new(model.Client)
err := d.DB.Where("token = ?", token).Find(client).Error
if err == gorm.ErrRecordNotFound {
err = nil
}
if client.Token == token {
return client, err
}
return nil, err
}
// CreateClient creates a client.
func (d *GormDatabase) CreateClient(client *model.Client) error {
return d.DB.Create(client).Error
}
// GetClientsByUser returns all clients from a user.
func (d *GormDatabase) GetClientsByUser(userID uint) ([]*model.Client, error) {
var clients []*model.Client
err := d.DB.Where("user_id = ?", userID).Find(&clients).Error
if err == gorm.ErrRecordNotFound {
err = nil
}
return clients, err
}
// DeleteClientByID deletes a client by its id.
func (d *GormDatabase) DeleteClientByID(id uint) error {
return d.DB.Where("id = ?", id).Delete(&model.Client{}).Error
}
// UpdateClient updates a client.
func (d *GormDatabase) UpdateClient(client *model.Client) error {
return d.DB.Save(client).Error
}
// UpdateClientTokensLastUsed updates the last used timestamp of clients.
func (d *GormDatabase) UpdateClientTokensLastUsed(tokens []string, t *time.Time) error {
return d.DB.Model(&model.Client{}).Where("token IN (?)", tokens).Update("last_used", t).Error
}