feat: refactor watchdog

This commit is contained in:
2026-04-28 17:18:14 +02:00
parent f589ae4faf
commit 0a98ee455f
11 changed files with 261 additions and 178 deletions
+60 -2
View File
@@ -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
}