diff --git a/datasource/bolt.go b/datasource/bolt.go index e5666a4..32476aa 100644 --- a/datasource/bolt.go +++ b/datasource/bolt.go @@ -28,6 +28,28 @@ func (ds *BoltDataSource) Setup(connection string) (func() error, error) { // SetConfig overrides the current config with the given values // by deleting the old config and adding a new one func (ds *BoltDataSource) SetConfig(c *model.Config) error { + ds.DB.Drop(&model.Config{}) + if c.UnitSystem != "metric" && c.UnitSystem != "imperial" { + return fmt.Errorf("unit system needs to be either metric or imperial: %s", c.UnitSystem) + } + height := c.Height + if c.UnitSystem == util.Imperial { + height = util.ToCm(height) + } + config := model.Config{ + Height: height, + Activity: c.Activity, + Birthday: c.Birthday, + Gender: c.Gender, + UnitSystem: c.UnitSystem, + } + err := ds.DB.Save(&config) + return err +} + +// SetConfigFromImport overrides the current config with the given values +// by deleting the old config and adding a new one +func (ds *BoltDataSource) SetConfigFromImport(c *model.Config) error { ds.DB.Drop(&model.Config{}) if c.UnitSystem != "metric" && c.UnitSystem != "imperial" { return fmt.Errorf("unit system needs to be either metric or imperial: %s", c.UnitSystem) @@ -168,7 +190,7 @@ func (ds *BoltDataSource) RemoveEntry(entryDate string, id int) error { // data func (ds *BoltDataSource) Import(data *model.ImpEx) error { var zeroID int - err := ds.SetConfig(data.Config) + err := ds.SetConfigFromImport(data.Config) if err != nil { return fmt.Errorf("could not replace config, %v", err) } diff --git a/datasource/datasource.go b/datasource/datasource.go index 79d106b..8d400bb 100644 --- a/datasource/datasource.go +++ b/datasource/datasource.go @@ -8,6 +8,7 @@ import ( type DataSource interface { Setup(connection string) (func() error, error) SetConfig(*model.Config) error + SetConfigFromImport(*model.Config) error FetchConfig() (*model.Config, error) AddWeight(weight float64) error CurrentWeight() (*model.Weight, error)