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
- [ ] Create `Record` and `Note` struct types
- [X] Create `Record` and `Note` struct types
- [X] Add `GKI` field to `Reading` struct type
- [ ] Encapsulate application data with an `AppContext` struct that holds a `Records` slice
- [ ] `LoadRecords`, `SaveRecords`, `AddReading`, `AddNote` receiver methods
- [X] Encapsulate application data with an `AppContext` struct that holds a `Records` slice
- [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`

100
main.go
View File

@ -12,7 +12,7 @@ import (
"time"
)
var readingsFilename string
var recordsFilename string
func init() {
// Get user's homeDir directory
@ -33,7 +33,7 @@ func init() {
}
// 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.
@ -108,66 +108,54 @@ func (ctx *AppContext) AddNote(note Note) {
}
func main() {
// Take a new reading from the user
reading, err := getReading()
var appCtx AppContext
err := appCtx.LoadRecords(recordsFilename)
if err != nil {
fmt.Println(err.Error())
return
fmt.Printf("Error loading records: %s\n", err)
}
// Calculate GKI
//gki, err := reading.GKI()
//if err != nil {
// fmt.Println(err.Error())
// fmt.Println("You are not in ketosis.")
// return
//}
// Display GKI and level of ketosis
fmt.Printf("\nYour GKI is: %0.2f\n", reading.GKI)
switch {
case reading.GKI <= 1:
fmt.Println("You're in the highest level of ketosis.")
case reading.GKI < 3:
fmt.Println("You're in a high therapeutic level of ketosis.")
case reading.GKI < 6:
fmt.Println("You're in a moderate level of ketosis.")
case reading.GKI <= 9:
fmt.Println("You're in a low level of ketosis.")
// Display menu
choice, err := getUserInput("1. Enter new reading\n2. Enter new note\n3. Exit\nYour choice")
switch choice {
case "1":
// Take a new reading from the user
reading, err := getReading()
if err != nil {
fmt.Printf("Error getting reading: %s\n", err)
} else {
// Display GKI and level of ketosis
fmt.Printf("\nYour GKI is: %0.2f\n", reading.GKI)
switch {
case reading.GKI <= 1:
fmt.Println("You're in the highest level of ketosis.")
case reading.GKI < 3:
fmt.Println("You're in a high therapeutic level of ketosis.")
case reading.GKI < 6:
fmt.Println("You're in a moderate 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:
fmt.Println("You are not in ketosis.")
fmt.Println("Quitting...")
}
// Save latest reading to readings file
readings, _ := loadReadingsFromFile(readingsFilename)
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
// Save Records before exiting
err = appCtx.SaveRecords(recordsFilename)
}
func getReading() (Reading, error) {