From c8e1328164e9ffbd681c3c0e449f1e6b9856b896 Mon Sep 17 00:00:00 2001 From: Gabriel Arakaki Giovanini Date: Sun, 26 Feb 2023 19:54:48 +0100 Subject: feat: Inicial commit It contains rough template for the server and runners. It contains rough template for the server and runners. --- pkg/database/sql/settings.go | 69 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 pkg/database/sql/settings.go (limited to 'pkg/database/sql/settings.go') diff --git a/pkg/database/sql/settings.go b/pkg/database/sql/settings.go new file mode 100644 index 0000000..7ad718b --- /dev/null +++ b/pkg/database/sql/settings.go @@ -0,0 +1,69 @@ +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 +} -- cgit v1.2.3