package sql import ( "context" "gorm.io/gorm" "git.sr.ht/~gabrielgio/img/pkg/components/settings" ) type ( Settings struct { gorm.Model ShowMode bool ShowOwner bool } SettingsRepository struct { db *gorm.DB } ) var _ settings.Repository = &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 *settings.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 result := db.Save(s) return result.Error } func (self *SettingsRepository) Load(ctx context.Context) (*settings.Settings, error) { s, err := self.ensureSettings(ctx) if err != nil { return nil, err } return &settings.Settings{ ShowMode: s.ShowMode, ShowOwner: s.ShowOwner, }, nil }