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 }