godose/edit.go
Pratyush Desai d2a253fb24
Initialized
Add basic CRUD operations for the doselogging
and a list command.

Signed-off-by: Pratyush Desai <pratyush.desai@liberta.casa>
2025-06-14 18:16:55 +05:30

63 lines
1.2 KiB
Go

package main
import (
"database/sql"
"flag"
"fmt"
)
func editDose(db *sql.DB, args []string) {
fs := flag.NewFlagSet("edit", flag.ExitOnError)
id := fs.Int("id", -1, "ID of dose to edit")
substance := fs.String("substance", "", "New substance name")
dose := fs.Float64("dose", -1, "New dose")
unit := fs.String("unit", "", "New unit")
roa := fs.String("roa", "", "New route")
notes := fs.String("notes", "", "New notes")
fs.Parse(args)
if *id < 0 {
fmt.Println("Error: --id is required")
return
}
// fetch existing
row := db.QueryRow("SELECT substance, dose, unit, roa, notes FROM doses WHERE id = ?", *id)
var s, u, r, n string
var d float64
err := row.Scan(&s, &d, &u, &r, &n)
if err != nil {
fmt.Println("Dose not found:", err)
return
}
// override if new values provided
if *substance != "" {
s = *substance
}
if *dose >= 0 {
d = *dose
}
if *unit != "" {
u = *unit
}
if *roa != "" {
r = *roa
}
if *notes != "" {
n = *notes
}
_, err = db.Exec("UPDATE doses SET substance = ?, dose = ?, unit = ?, roa = ?, notes = ? WHERE id = ?",
s, d, u, r, n, *id,
)
if err != nil {
fmt.Println("Update failed:", err)
return
}
fmt.Println("Dose updated.")
}