Add a delete method for medication

This commit is contained in:
Tony Blyler 2021-05-18 21:40:27 -04:00
parent a8c39d066b
commit 97ed0cd797
Signed by: tblyler
GPG key ID: 7F13D9A60C0D678E
2 changed files with 37 additions and 0 deletions

View file

@ -148,6 +148,13 @@ func (b *Badger) AddMedication(medication *Medication) error {
})
}
// RemoveMedication from the database
func (b *Badger) RemoveMedication(medication *Medication) error {
return b.db.Update(func(tx *badger.Txn) error {
return tx.Delete(medication.badgerKey())
})
}
// ListMedicationsForUser from the database
func (b *Badger) ListMedicationsForUser(user *User) (medications []*Medication, err error) {
err = b.db.View(func(tx *badger.Txn) error {

30
main.go
View file

@ -270,6 +270,36 @@ func main() {
log(medication)
case "remove":
fmt.Print("username: ")
inputScanner.Scan()
username := string(bytes.TrimSpace(inputScanner.Bytes()))
if username == "" {
return fmt.Errorf("failed to get username from STDIN prompt: %w", inputScanner.Err())
}
user, err := b.GetUser(username)
if err != nil {
return fmt.Errorf("failed to lookup username %s: %w", username, err)
}
if user == nil {
return fmt.Errorf("username %s doesn't exist", username)
}
fmt.Print("medication id: ")
inputScanner.Scan()
medicationID := uuid.MustParse(string(bytes.TrimSpace(inputScanner.Bytes())))
err = b.RemoveMedication(&db.Medication{
IDUser: user.ID,
ID: medicationID,
})
if err != nil {
return err
}
case "list":
fmt.Print("username: ")
inputScanner.Scan()