From 42541b349621d3264efc1cb7ef8ce5ffdc80a91b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BF=83=E9=9A=A8=E7=B7=A3=E5=8B=95?= Date: Fri, 19 Sep 2025 19:36:15 +0800 Subject: [PATCH] One-click configuration: Vless Encryption + XHTTP --- database/history.go | 49 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 database/history.go diff --git a/database/history.go b/database/history.go new file mode 100644 index 00000000..9a2c8f75 --- /dev/null +++ b/database/history.go @@ -0,0 +1,49 @@ +package database + +import ( + "time" +) + +// LinkHistory GORM aodel for link_history table +type LinkHistory struct { + Id int `gorm:"primaryKey"` + Type string `gorm:"type:varchar(255);not null"` + Link string `gorm:"type:text;not null"` + CreatedAt time.Time `gorm:"not null"` +} + +// AddLinkHistory adds a new link record and trims old ones +func AddLinkHistory(record *LinkHistory) error { + // 1. Add the new record + if err := db.Create(record).Error; err != nil { + return err + } + + // 2. Trim old records, keeping only the 10 most recent + var count int64 + db.Model(&LinkHistory{}).Count(&count) + if count > 10 { + limit := int(count) - 10 + var recordsToDelete []LinkHistory + if err := db.Order("created_at asc").Limit(limit).Find(&recordsToDelete).Error; err != nil { + return err + } + if len(recordsToDelete) > 0 { + if err := db.Delete(&recordsToDelete).Error; err != nil { + return err + } + } + } + return nil +} + +// GetLinkHistory retrieves the 10 most recent link records +func GetLinkHistory() ([]*LinkHistory, error) { + var histories []*LinkHistory + err := db.Order("created_at desc").Limit(10).Find(&histories).Error + if err != nil { + return nil, err + } + return histories, nil +} +