aboutsummaryrefslogtreecommitdiff
path: root/pkg/database/sql/settings.go
diff options
context:
space:
mode:
authorGabriel Arakaki Giovanini <mail@gabrielgio.me>2023-02-26 19:54:48 +0100
committerGabriel Arakaki Giovanini <mail@gabrielgio.me>2023-06-18 16:30:36 +0200
commitc8e1328164e9ffbd681c3c0e449f1e6b9856b896 (patch)
treefaee639a4c55c5dc3bfc59a5400026822c40221d /pkg/database/sql/settings.go
downloadlens-c8e1328164e9ffbd681c3c0e449f1e6b9856b896.tar.gz
lens-c8e1328164e9ffbd681c3c0e449f1e6b9856b896.tar.bz2
lens-c8e1328164e9ffbd681c3c0e449f1e6b9856b896.zip
feat: Inicial commit
It contains rough template for the server and runners. It contains rough template for the server and runners.
Diffstat (limited to 'pkg/database/sql/settings.go')
-rw-r--r--pkg/database/sql/settings.go69
1 files changed, 69 insertions, 0 deletions
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
+}