docs: data package

This commit is contained in:
agatha 2024-05-12 17:01:17 -04:00
parent 7e9d876297
commit a1103f0974

View File

@ -1,3 +1,7 @@
// Package data manages the loading, saving, and manipulation of application data
// pertaining to health tracking. It provides an abstraction layer over the storage
// mechanism used for persisting records and offers convenient methods to interact
// with the data.
package data package data
import ( import (
@ -6,12 +10,17 @@ import (
"os" "os"
) )
// AppContext is the application data store that holds Records // AppContext represents the application's data context, encapsulating the storage
// and management of health-related records. It provides mechanisms to load and save
// these records from a persistent storage.
type AppContext struct { type AppContext struct {
Records []model.Record Records []model.Record
dataPath string dataPath string
} }
// NewContext initializes a new application data context with the specified data
// path. It attempts to load existing records from the provided data path, or
// initializes an empty context if the records do not exist.
func NewContext(dataPath string) (AppContext, error) { func NewContext(dataPath string) (AppContext, error) {
ctx := AppContext{dataPath: dataPath} ctx := AppContext{dataPath: dataPath}
err := ctx.LoadRecords() err := ctx.LoadRecords()
@ -21,7 +30,9 @@ func NewContext(dataPath string) (AppContext, error) {
return ctx, nil return ctx, nil
} }
// LoadRecords will load records from file // LoadRecords loads the records from the data file specified in the AppContext.
// If the records file does not exist, it initializes an empty record list and
// saves it to create the file.
func (ctx *AppContext) LoadRecords() error { func (ctx *AppContext) LoadRecords() error {
data, err := os.ReadFile(ctx.dataPath) data, err := os.ReadFile(ctx.dataPath)
@ -37,7 +48,9 @@ func (ctx *AppContext) LoadRecords() error {
return json.Unmarshal(data, &ctx.Records) return json.Unmarshal(data, &ctx.Records)
} }
// SaveRecords will save records to a file // SaveRecords serializes the current records to JSON and writes them to the
// data path specified in the AppContext. It ensures data persistence with
// appropriate access permissions.
func (ctx *AppContext) SaveRecords() error { func (ctx *AppContext) SaveRecords() error {
jsonData, err := json.Marshal(ctx.Records) jsonData, err := json.Marshal(ctx.Records)
if err != nil { if err != nil {
@ -46,12 +59,14 @@ func (ctx *AppContext) SaveRecords() error {
return os.WriteFile(ctx.dataPath, jsonData, 0660) return os.WriteFile(ctx.dataPath, jsonData, 0660)
} }
// AddReading will add a Reading to the AppContext // AddReading appends a new Reading record to the AppContext. It updates the
// application context's records with the latest readings data.
func (ctx *AppContext) AddReading(reading model.Reading) { func (ctx *AppContext) AddReading(reading model.Reading) {
ctx.Records = append(ctx.Records, model.Record{Reading: &reading}) ctx.Records = append(ctx.Records, model.Record{Reading: &reading})
} }
// AddNote will a add a Note to the AppContext // AddNote appends a new Note record to the AppContext. It updates the
// application context's records with the newly added note.
func (ctx *AppContext) AddNote(note model.Note) { func (ctx *AppContext) AddNote(note model.Note) {
ctx.Records = append(ctx.Records, model.Record{Note: &note}) ctx.Records = append(ctx.Records, model.Record{Note: &note})
} }