2024-05-12 20:05:05 +00:00
|
|
|
package cli
|
2024-05-12 02:06:45 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2024-05-12 20:05:05 +00:00
|
|
|
"ketotrack/data"
|
|
|
|
"ketotrack/model"
|
2024-05-12 02:06:45 +00:00
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2024-05-12 20:05:05 +00:00
|
|
|
func Run(appCtx *data.AppContext) {
|
2024-05-12 19:54:36 +00:00
|
|
|
if err := appCtx.LoadRecords(); err != nil {
|
2024-05-12 18:58:23 +00:00
|
|
|
fmt.Printf("Error loading records from file: %s\n", err)
|
2024-05-12 02:06:45 +00:00
|
|
|
}
|
|
|
|
|
2024-05-12 18:58:23 +00:00
|
|
|
for {
|
|
|
|
choice, err := getUserInput("1. Enter new reading\n2. Enter new note\n3. Exit\nYour choice")
|
2024-05-12 18:38:55 +00:00
|
|
|
if err != nil {
|
2024-05-12 18:58:23 +00:00
|
|
|
fmt.Printf("Error reading choice: %s\n", err)
|
|
|
|
continue
|
2024-05-12 18:38:55 +00:00
|
|
|
}
|
2024-05-12 18:58:23 +00:00
|
|
|
|
|
|
|
if choice == "3" {
|
|
|
|
fmt.Println("Exiting...")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
switch choice {
|
|
|
|
case "1":
|
|
|
|
// Get a new Reading from the user
|
|
|
|
reading, err := getReading()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error getting reading: %s\n", err)
|
|
|
|
} else {
|
|
|
|
appCtx.AddReading(reading)
|
|
|
|
}
|
|
|
|
case "2":
|
|
|
|
// Get a new Note from the user
|
2024-05-12 19:09:32 +00:00
|
|
|
note, err := getNote()
|
2024-05-12 18:58:23 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error getting note: %s\n", err)
|
|
|
|
} else {
|
|
|
|
appCtx.AddNote(note)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
fmt.Printf("Invalid choice, please try again.\n\n")
|
2024-05-12 18:38:55 +00:00
|
|
|
}
|
2024-05-12 02:06:45 +00:00
|
|
|
}
|
|
|
|
|
2024-05-12 18:38:55 +00:00
|
|
|
// Save Records before exiting
|
2024-05-12 19:54:36 +00:00
|
|
|
if err := appCtx.SaveRecords(); err != nil {
|
2024-05-12 18:45:04 +00:00
|
|
|
fmt.Printf("Error saving records to file: %s\n", err)
|
|
|
|
}
|
2024-05-12 02:06:45 +00:00
|
|
|
}
|
|
|
|
|
2024-05-12 19:46:23 +00:00
|
|
|
func getNote() (model.Note, error) {
|
2024-05-12 19:09:32 +00:00
|
|
|
text, err := getUserInput("Enter note text")
|
|
|
|
if err != nil {
|
2024-05-12 19:46:23 +00:00
|
|
|
return model.Note{}, err
|
2024-05-12 19:09:32 +00:00
|
|
|
}
|
2024-05-12 19:46:23 +00:00
|
|
|
return model.NewNote(text), nil
|
2024-05-12 19:09:32 +00:00
|
|
|
}
|
|
|
|
|
2024-05-12 19:46:23 +00:00
|
|
|
func getReading() (model.Reading, error) {
|
2024-05-12 02:06:45 +00:00
|
|
|
// Get glucose reading
|
|
|
|
glucose, err := getUserFloat("Enter glucose reading (mg/dL)")
|
|
|
|
if err != nil {
|
2024-05-12 19:46:23 +00:00
|
|
|
return model.Reading{}, fmt.Errorf("error getting glucose reading: %w", err)
|
2024-05-12 02:06:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate glucose reading
|
|
|
|
if glucose <= 0 {
|
2024-05-12 19:46:23 +00:00
|
|
|
return model.Reading{}, errors.New("glucose reading cannot be less than or equal to 0")
|
2024-05-12 02:06:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get ketone reading
|
|
|
|
ketone, err := getUserFloat("Enter ketone reading (mmol/L)")
|
|
|
|
if err != nil {
|
2024-05-12 19:46:23 +00:00
|
|
|
return model.Reading{}, fmt.Errorf("error getting ketone reading: %w", err)
|
2024-05-12 02:06:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate ketone reading
|
|
|
|
if ketone < 0 {
|
|
|
|
fmt.Println("")
|
2024-05-12 19:46:23 +00:00
|
|
|
return model.Reading{}, errors.New("ketone reading cannot be less than 0")
|
2024-05-12 02:06:45 +00:00
|
|
|
}
|
|
|
|
|
2024-05-12 19:46:23 +00:00
|
|
|
reading := model.NewReading(glucose, ketone)
|
2024-05-12 19:03:10 +00:00
|
|
|
|
|
|
|
// 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.")
|
|
|
|
}
|
|
|
|
|
|
|
|
return reading, nil
|
2024-05-12 02:06:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getUserInput(prompt string) (string, error) {
|
|
|
|
fmt.Printf("%s: ", prompt)
|
|
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
|
input, err := reader.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return strings.TrimSpace(input), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getUserFloat(prompt string) (float64, error) {
|
|
|
|
// Get input as a string
|
|
|
|
s, err := getUserInput(prompt)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert string to float
|
|
|
|
f, err := strconv.ParseFloat(s, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return f, nil
|
|
|
|
}
|