From e5c5603ebdb5fe80d185ef668c1ec06a084be014 Mon Sep 17 00:00:00 2001 From: bronku Date: Sun, 23 Mar 2025 21:36:10 +0100 Subject: [PATCH] Return error if cake not found in GetCake --- internal/store/cake.go | 24 ++++++++++++++++-------- internal/store/migrations/3.sql | 7 +++++++ 2 files changed, 23 insertions(+), 8 deletions(-) create mode 100644 internal/store/migrations/3.sql diff --git a/internal/store/cake.go b/internal/store/cake.go index 0d40ef1..9dc7fc1 100644 --- a/internal/store/cake.go +++ b/internal/store/cake.go @@ -6,21 +6,29 @@ import ( ) type Cake struct { - Name string - ID int - Price int - Amount int + Name string + ID int + Price int + Amount int + Category string + Availability string } - func (s *Store) GetCake(id int) (Cake, error) { - out := Cake{ID: id} - row, err := s.db.Query("select name, price from cake where id = ?;", id) + var out Cake + query := "select name, price from cake where id = ?;" + + row, err := s.db.Query(query, id) if err != nil { return out, err } defer row.Close() - row.Next() + + if !row.Next() { + return out, errors.New("cake not found") + } + + out.ID = id err = row.Scan(&out.Name, &out.Price) return out, err } diff --git a/internal/store/migrations/3.sql b/internal/store/migrations/3.sql new file mode 100644 index 0000000..b0c5616 --- /dev/null +++ b/internal/store/migrations/3.sql @@ -0,0 +1,7 @@ +alter table cake +add column category text; + +alter table cake +add column availability text; + +PRAGMA user_version = 3;