add AppContext

This commit is contained in:
agatha 2024-05-12 14:22:04 -04:00
parent e5ab7ec5ef
commit 7128dbd38e

40
main.go
View File

@ -74,16 +74,38 @@ func NewReading(glucose, ketone float64) Reading {
} }
} }
/* // AppContext is the application data store that holds Records
// GKI calculates and returns the Glucose Ketone Index of the reading. type AppContext struct {
// The Glucose Ketone Index helps determine the efficiency of glucose and ketone levels within the body. It is Records []Record
// calculated as (glucose level in mg/dL / 18) divided by the ketone level. }
func (r Reading) GKI() (float64, error) {
if r.Ketone == 0 { // LoadRecords will load records from file
return 0, errors.New("ketone level must not be zero to calculate GKI") 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: &note})
}
func main() { func main() {
// Take a new reading from the user // Take a new reading from the user