package sql import ( "context" "gorm.io/gorm" "git.sr.ht/~gabrielgio/img/pkg/database/repository" ) type ( Settings struct { gorm.Model ShowMode bool ShowOwner bool PreloadVideoMetadata bool } SettingsRepository struct { db *gorm.DB } ) var _ repository.SettingsRepository = &SettingsRepository{} func NewSettingsRespository(db *gorm.DB) *SettingsRepository { return &SettingsRepository{ db: db, } } func (self *SettingsRepository) ensureSettings(ctx context.Context) (*Settings, error) { var ( db = self.db.WithContext(ctx) s = &Settings{} ) result := db.Limit(1).Find(s) if result.Error != nil { return nil, result.Error } return s, nil } func (self *SettingsRepository) Save(ctx context.Context, toSaveSettings *repository.Settings) error { db := self.db.WithContext(ctx) s, err := self.ensureSettings(ctx) if err != nil { return err } s.ShowMode = toSaveSettings.ShowMode s.ShowOwner = toSaveSettings.ShowOwner s.PreloadVideoMetadata = toSaveSettings.PreloadVideoMetadata result := db.Save(s) return result.Error } func (self *SettingsRepository) Load(ctx context.Context) (*repository.Settings, error) { s, err := self.ensureSettings(ctx) if err != nil { return nil, err } return &repository.Settings{ ShowMode: s.ShowMode, ShowOwner: s.ShowOwner, PreloadVideoMetadata: s.PreloadVideoMetadata, }, nil }