refactor entire structure

This commit is contained in:
agatha 2024-05-12 16:05:05 -04:00
parent 796075d9f2
commit 402bcd35a9
4 changed files with 32 additions and 48 deletions

18
cmd/cli/main.go Normal file
View File

@ -0,0 +1,18 @@
package main
import (
"fmt"
"ketotrack/config"
"ketotrack/data"
"ketotrack/ui/cli"
)
func main() {
appConfig := config.Load()
appCtx, err := data.NewContext(appConfig.DataPath)
if err != nil {
fmt.Printf("Error initializing application context: %s\n", err)
return
}
cli.Run(&appCtx)
}

View File

@ -6,15 +6,9 @@ import (
"os" "os"
) )
// Record holds a pointer to a Reading or Note.
type Record struct {
Reading *model.Reading `json:"reading,omitempty"`
Note *model.Note `json:"note,omitempty"`
}
// AppContext is the application data store that holds Records // AppContext is the application data store that holds Records
type AppContext struct { type AppContext struct {
Records []Record Records []model.Record
dataPath string dataPath string
} }
@ -47,10 +41,10 @@ func (ctx *AppContext) SaveRecords() error {
// AddReading will add a Reading to the AppContext // AddReading will add a Reading to the AppContext
func (ctx *AppContext) AddReading(reading model.Reading) { func (ctx *AppContext) AddReading(reading model.Reading) {
ctx.Records = append(ctx.Records, Record{Reading: &reading}) ctx.Records = append(ctx.Records, model.Record{Reading: &reading})
} }
// AddNote will a add a Note to the AppContext // AddNote will a add a Note to the AppContext
func (ctx *AppContext) AddNote(note model.Note) { func (ctx *AppContext) AddNote(note model.Note) {
ctx.Records = append(ctx.Records, Record{Note: &note}) ctx.Records = append(ctx.Records, model.Record{Note: &note})
} }

View File

@ -41,3 +41,9 @@ func NewReading(glucose, ketone float64) Reading {
GKI: gki, GKI: gki,
} }
} }
// Record holds a pointer to a Reading or Note.
type Record struct {
Reading *Reading `json:"reading,omitempty"`
Note *Note `json:"note,omitempty"`
}

View File

@ -1,51 +1,17 @@
package main package cli
import ( import (
"bufio" "bufio"
"errors" "errors"
"fmt" "fmt"
"os"
"path/filepath"
"strconv"
"strings"
"ketotrack/config"
"ketotrack/data" "ketotrack/data"
"ketotrack/model" "ketotrack/model"
"os"
"strconv"
"strings"
) )
var recordsFilename string func Run(appCtx *data.AppContext) {
func init() {
// Get user's homeDir directory
homeDir, err := os.UserHomeDir()
if err != nil {
fmt.Println("Error fetching user's home directory:", err)
os.Exit(1)
}
// Define the path to the application data directory
ketotrackDir := filepath.Join(homeDir, ".ketotrack")
// Create the directory if it does not exist
err = os.MkdirAll(ketotrackDir, 0770)
if err != nil {
fmt.Println("Error creating .ketotrack directory:", err)
os.Exit(1)
}
// Set data file path for readings
recordsFilename = filepath.Join(ketotrackDir, "records.json")
}
func main() {
appConfig := config.Load()
appCtx, err := data.NewContext(appConfig.DataPath)
if err != nil {
fmt.Printf("Error initializing application context: %s\n", err)
return
}
if err := appCtx.LoadRecords(); err != nil { if err := appCtx.LoadRecords(); err != nil {
fmt.Printf("Error loading records from file: %s\n", err) fmt.Printf("Error loading records from file: %s\n", err)
} }