feat: refactor watchdog
This commit is contained in:
@@ -4,12 +4,60 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
/*
|
||||
State functions
|
||||
*/
|
||||
|
||||
func LatestState(db *gorm.DB) (Command, error) {
|
||||
var state Command
|
||||
err := db.Last(&state).Error
|
||||
return state, err
|
||||
}
|
||||
|
||||
/*
|
||||
Key functions
|
||||
*/
|
||||
|
||||
func CountKeys(db *gorm.DB) (int64, error) {
|
||||
var count int64
|
||||
err := db.Model(&AccessKey{}).Count(&count).Error
|
||||
return count, err
|
||||
}
|
||||
|
||||
func ListKeys(db *gorm.DB) ([]AccessKey, error) {
|
||||
var keys []AccessKey
|
||||
err := db.Find(&keys).Error
|
||||
return keys, err
|
||||
}
|
||||
|
||||
func CreateKey(db *gorm.DB, k AccessKey) error {
|
||||
return db.Create(&k).Error
|
||||
}
|
||||
|
||||
func DeleteKeyByID(db *gorm.DB, id int) error {
|
||||
res := db.Delete(&AccessKey{}, id)
|
||||
|
||||
if res.Error != nil {
|
||||
return res.Error
|
||||
}
|
||||
|
||||
if res.RowsAffected == 0 {
|
||||
return gorm.ErrRecordNotFound
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
/*
|
||||
File functions
|
||||
*/
|
||||
|
||||
func CountFiles(db *gorm.DB) (int64, error) {
|
||||
var count int64
|
||||
err := db.Model(&File{}).Count(&count).Error
|
||||
return count, err
|
||||
}
|
||||
|
||||
func ListFiles(db *gorm.DB) ([]File, error) {
|
||||
var files []File
|
||||
err := db.Find(&files).Error
|
||||
@@ -26,6 +74,16 @@ func CreateFile(db *gorm.DB, f File) error {
|
||||
return db.Create(&f).Error
|
||||
}
|
||||
|
||||
func DeleteFile(db *gorm.DB, f File) error {
|
||||
return db.Delete(&f).Error
|
||||
func DeleteFileByID(db *gorm.DB, id int) error {
|
||||
res := db.Delete(&File{}, id)
|
||||
|
||||
if res.Error != nil {
|
||||
return res.Error
|
||||
}
|
||||
|
||||
if res.RowsAffected == 0 {
|
||||
return gorm.ErrRecordNotFound
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user