implement AppContext, add Note type, add menu

This commit is contained in:
agatha 2024-05-12 14:38:55 -04:00
parent 7128dbd38e
commit 18436e050f
2 changed files with 48 additions and 59 deletions

View File

@ -24,8 +24,9 @@ mv ketotrack $HOME/.local/bin
``` ```
## Refactoring Notes ## Refactoring Notes
- [ ] Create `Record` and `Note` struct types - [X] Create `Record` and `Note` struct types
- [X] Add `GKI` field to `Reading` struct type - [X] Add `GKI` field to `Reading` struct type
- [ ] Encapsulate application data with an `AppContext` struct that holds a `Records` slice - [X] Encapsulate application data with an `AppContext` struct that holds a `Records` slice
- [ ] `LoadRecords`, `SaveRecords`, `AddReading`, `AddNote` receiver methods - [X] `LoadRecords`, `SaveRecords`, `AddReading`, `AddNote` receiver methods
- [X] Add interactive menu for adding either a `Note` or a `Record`
- [ ] Standardize method names for getting user input to `handleNewNote` and `handleNewReading` - [ ] Standardize method names for getting user input to `handleNewNote` and `handleNewReading`

100
main.go
View File

@ -12,7 +12,7 @@ import (
"time" "time"
) )
var readingsFilename string var recordsFilename string
func init() { func init() {
// Get user's homeDir directory // Get user's homeDir directory
@ -33,7 +33,7 @@ func init() {
} }
// Set data file path for readings // Set data file path for readings
readingsFilename = filepath.Join(ketotrackDir, "readings.json") recordsFilename = filepath.Join(ketotrackDir, "records.json")
} }
// Record holds a pointer to a Reading or Note. // Record holds a pointer to a Reading or Note.
@ -108,66 +108,54 @@ func (ctx *AppContext) AddNote(note Note) {
} }
func main() { func main() {
// Take a new reading from the user var appCtx AppContext
reading, err := getReading()
err := appCtx.LoadRecords(recordsFilename)
if err != nil { if err != nil {
fmt.Println(err.Error()) fmt.Printf("Error loading records: %s\n", err)
return
} }
// Calculate GKI // Display menu
//gki, err := reading.GKI() choice, err := getUserInput("1. Enter new reading\n2. Enter new note\n3. Exit\nYour choice")
//if err != nil { switch choice {
// fmt.Println(err.Error()) case "1":
// fmt.Println("You are not in ketosis.") // Take a new reading from the user
// return reading, err := getReading()
//} if err != nil {
fmt.Printf("Error getting reading: %s\n", err)
// Display GKI and level of ketosis } else {
fmt.Printf("\nYour GKI is: %0.2f\n", reading.GKI) // Display GKI and level of ketosis
switch { fmt.Printf("\nYour GKI is: %0.2f\n", reading.GKI)
case reading.GKI <= 1: switch {
fmt.Println("You're in the highest level of ketosis.") case reading.GKI <= 1:
case reading.GKI < 3: fmt.Println("You're in the highest level of ketosis.")
fmt.Println("You're in a high therapeutic level of ketosis.") case reading.GKI < 3:
case reading.GKI < 6: fmt.Println("You're in a high therapeutic level of ketosis.")
fmt.Println("You're in a moderate level of ketosis.") case reading.GKI < 6:
case reading.GKI <= 9: fmt.Println("You're in a moderate level of ketosis.")
fmt.Println("You're in a low level of ketosis.") case reading.GKI <= 9:
fmt.Println("You're in a low level of ketosis.")
default:
fmt.Println("You are not in ketosis.")
}
appCtx.AddReading(reading)
}
case "2":
noteText, err := getUserInput("Enter your note text: ")
if err != nil {
fmt.Println("Error getting note: %s\n", err)
} else {
appCtx.AddNote(Note{
Time: time.Now(),
Text: noteText,
})
}
default: default:
fmt.Println("You are not in ketosis.") fmt.Println("Quitting...")
} }
// Save latest reading to readings file // Save Records before exiting
readings, _ := loadReadingsFromFile(readingsFilename) err = appCtx.SaveRecords(recordsFilename)
readings = append(readings, reading)
err = saveReadingsToFile(readingsFilename, readings)
if err != nil {
fmt.Printf("error saving data: %s\n", err.Error())
}
}
func saveReadingsToFile(filename string, data []Reading) error {
jsonData, err := json.Marshal(data)
if err != nil {
return err
}
return os.WriteFile(filename, jsonData, 0660)
}
func loadReadingsFromFile(filename string) ([]Reading, error) {
data, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
var readings []Reading
err = json.Unmarshal(data, &readings)
if err != nil {
return nil, err
}
return readings, nil
} }
func getReading() (Reading, error) { func getReading() (Reading, error) {