Return error if cake not found in GetCake
This commit is contained in:
parent
922710a2cd
commit
e5c5603ebd
2 changed files with 23 additions and 8 deletions
|
|
@ -6,21 +6,29 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Cake struct {
|
type Cake struct {
|
||||||
Name string
|
Name string
|
||||||
ID int
|
ID int
|
||||||
Price int
|
Price int
|
||||||
Amount int
|
Amount int
|
||||||
|
Category string
|
||||||
|
Availability string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (s *Store) GetCake(id int) (Cake, error) {
|
func (s *Store) GetCake(id int) (Cake, error) {
|
||||||
out := Cake{ID: id}
|
var out Cake
|
||||||
row, err := s.db.Query("select name, price from cake where id = ?;", id)
|
query := "select name, price from cake where id = ?;"
|
||||||
|
|
||||||
|
row, err := s.db.Query(query, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return out, err
|
return out, err
|
||||||
}
|
}
|
||||||
defer row.Close()
|
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)
|
err = row.Scan(&out.Name, &out.Price)
|
||||||
return out, err
|
return out, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
7
internal/store/migrations/3.sql
Normal file
7
internal/store/migrations/3.sql
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
alter table cake
|
||||||
|
add column category text;
|
||||||
|
|
||||||
|
alter table cake
|
||||||
|
add column availability text;
|
||||||
|
|
||||||
|
PRAGMA user_version = 3;
|
||||||
Loading…
Add table
Add a link
Reference in a new issue