diff --git a/main.go b/main.go index f0831e8..f39c0c2 100644 --- a/main.go +++ b/main.go @@ -74,16 +74,38 @@ func NewReading(glucose, ketone float64) Reading { } } -/* -// GKI calculates and returns the Glucose Ketone Index of the reading. -// The Glucose Ketone Index helps determine the efficiency of glucose and ketone levels within the body. It is -// calculated as (glucose level in mg/dL / 18) divided by the ketone level. -func (r Reading) GKI() (float64, error) { - if r.Ketone == 0 { - return 0, errors.New("ketone level must not be zero to calculate GKI") +// AppContext is the application data store that holds Records +type AppContext struct { + Records []Record +} + +// LoadRecords will load records from file +func (ctx *AppContext) LoadRecords(filename string) error { + data, err := os.ReadFile(filename) + if err != nil { + return err } - return (r.Glucose / 18) / r.Ketone, nil -}*/ + return json.Unmarshal(data, &ctx.Records) +} + +// SaveRecords will save records to a file +func (ctx *AppContext) SaveRecords(filename string) error { + jsonData, err := json.Marshal(ctx.Records) + if err != nil { + return err + } + return os.WriteFile(filename, jsonData, 0660) +} + +// AddReading will add a Reading to the AppContext +func (ctx *AppContext) AddReading(reading Reading) { + ctx.Records = append(ctx.Records, Record{Reading: &reading}) +} + +// AddNote will a add a Note to the AppContext +func (ctx *AppContext) AddNote(note Note) { + ctx.Records = append(ctx.Records, Record{Note: ¬e}) +} func main() { // Take a new reading from the user