Skip to content

Commit

Permalink
fix height for import and fix height regression when setting config
Browse files Browse the repository at this point in the history
  • Loading branch information
zupzup committed Dec 9, 2022
1 parent 65c7ce8 commit 9ac70e2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
24 changes: 23 additions & 1 deletion datasource/bolt.go
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down
1 change: 1 addition & 0 deletions datasource/datasource.go
Expand Up @@ -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)
Expand Down

0 comments on commit 9ac70e2

Please sign in to comment.