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`

70
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,21 +108,22 @@ func (ctx *AppContext) AddNote(note Note) {
} }
func main() { func main() {
var appCtx AppContext
err := appCtx.LoadRecords(recordsFilename)
if err != nil {
fmt.Printf("Error loading records: %s\n", err)
}
// 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 // Take a new reading from the user
reading, err := getReading() reading, err := getReading()
if err != nil { if err != nil {
fmt.Println(err.Error()) fmt.Printf("Error getting reading: %s\n", err)
return } else {
}
// 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 // Display GKI and level of ketosis
fmt.Printf("\nYour GKI is: %0.2f\n", reading.GKI) fmt.Printf("\nYour GKI is: %0.2f\n", reading.GKI)
switch { switch {
@ -137,37 +138,24 @@ func main() {
default: default:
fmt.Println("You are not in ketosis.") fmt.Println("You are not in ketosis.")
} }
appCtx.AddReading(reading)
// Save latest reading to readings file }
readings, _ := loadReadingsFromFile(readingsFilename) case "2":
readings = append(readings, reading) noteText, err := getUserInput("Enter your note text: ")
err = saveReadingsToFile(readingsFilename, readings)
if err != nil { if err != nil {
fmt.Printf("error saving data: %s\n", err.Error()) fmt.Println("Error getting note: %s\n", err)
} else {
appCtx.AddNote(Note{
Time: time.Now(),
Text: noteText,
})
} }
default:
fmt.Println("Quitting...")
} }
func saveReadingsToFile(filename string, data []Reading) error { // Save Records before exiting
jsonData, err := json.Marshal(data) err = appCtx.SaveRecords(recordsFilename)
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) {