Implement settingsservice

This commit is contained in:
2024-08-09 15:16:02 +02:00
parent d6e00316bc
commit 56e0ef49a0
8 changed files with 61 additions and 7 deletions

3
.gitignore vendored
View File

@@ -3,3 +3,6 @@ node_modules
frontend/dist frontend/dist
tmp tmp
food.db food.db
food.db-shm
food.db-wal
main.log

2
db.go
View File

@@ -9,6 +9,7 @@ import (
) )
type DB struct { type DB struct {
Ready bool
path string path string
readConn *sql.DB readConn *sql.DB
writeConn *sql.DB writeConn *sql.DB
@@ -39,6 +40,7 @@ func (db *DB) Open() error {
readConn.SetConnMaxLifetime(30 * time.Second) readConn.SetConnMaxLifetime(30 * time.Second)
db.readConn = readConn db.readConn = readConn
db.Ready = true
return nil return nil
} }

View File

@@ -0,0 +1,5 @@
package main
type FoodService struct {
db *DB
}

View File

@@ -47,6 +47,14 @@ func main() {
os.Exit(1) os.Exit(1)
} }
settingsService := SettingsService{db: &db}
settings, err := settingsService.LoadSettings()
if err != nil {
Error.Printf("%++v", err)
os.Exit(1)
}
log.Println(settings)
log.Println("done") log.Println("done")
wg.Wait() wg.Wait()

View File

@@ -1 +0,0 @@
15:05:28.134465 main.go:50: done

View File

@@ -0,0 +1,37 @@
package main
import (
"fmt"
"log"
)
type (
SettingsService struct {
db *DB
}
)
var Settings map[string]string = map[string]string{
"FoodDaysLookback": "7",
"FoodAggregatedDaysLookback": "7",
"WeightDaysLookback": "7",
"WeightAggregatedDaysLookback": "7",
}
func (s *SettingsService) LoadSettings() (map[string]string, error) {
if s.db == nil || !s.db.Ready {
return Settings, fmt.Errorf("cannot load Settings, db is nil or is not ready")
}
for key := range Settings {
var value string
err := s.db.readConn.QueryRow("SELECT value FROM settings WHERE key = ?", key).Scan(&value)
if err != nil {
Error.Printf("error getting setting for key %s: %++v", key, err)
continue
}
log.Println(key, value)
}
return Settings, nil
}

View File

@@ -3,11 +3,6 @@ package main
import "time" import "time"
type ( type (
Setting struct {
key string
value string
}
Food struct { Food struct {
rowid int rowid int
date time.Time date time.Time

View File

@@ -0,0 +1,5 @@
package main
type WeightService struct {
db *DB
}