Fix SQL casing and improve error handling

This commit is contained in:
bronku 2025-03-26 13:26:26 +01:00
parent c0227e4e2d
commit 72ed1ae61b
6 changed files with 20 additions and 15 deletions

View file

@ -19,7 +19,7 @@ func (s *Store) cakeCount() int {
if err != nil { if err != nil {
return out return out
} }
rows.Close() defer rows.Close()
_ = rows.Next() _ = rows.Next()
_ = rows.Scan(&out) _ = rows.Scan(&out)
return out return out
@ -53,23 +53,19 @@ func (s *Store) searchCakes(id int) (int, error) {
} }
return i, nil return i, nil
} }
return -1, errors.New("cake not found") return 0, errors.New("cake not found")
} }
func (s *Store) GetCake(id int) (Cake, error) { func (s *Store) GetCake(id int) (Cake, error) {
if id <= 0 { if id <= 0 {
return Cake{}, errors.New("Invalid cake id") return Cake{}, errors.New("Invalid cake id")
} }
if len(s.cakes) < id {
i, err := s.searchCakes(id)
return s.cakes[i], err
}
if s.cakes[id-1].ID != id {
i, err := s.searchCakes(id)
return s.cakes[i], err
}
return s.cakes[id-1], nil i, err := s.searchCakes(id)
if err != nil {
return Cake{}, err
}
return s.cakes[i], err
} }
func (s *Store) GetCakes() ([]Cake, error) { func (s *Store) GetCakes() ([]Cake, error) {

View file

@ -24,7 +24,7 @@ func (s *Store) loadFile(file string) {
query, _ := migrations.ReadFile("migrations/" + file) query, _ := migrations.ReadFile("migrations/" + file)
_, err = s.db.Exec(string(query)) _, err = s.db.Exec(string(query))
if err != nil { if err != nil {
log.Fatal("error executing migration: ", file) log.Fatal("error executing migration: ", file, err)
} }
} }

View file

@ -23,4 +23,4 @@ create table ordered_cake (
primary key (customer_order, cake) primary key (customer_order, cake)
); );
PRAGMA user_version = 1; pragma user_version = 1;

View file

@ -5,4 +5,4 @@ create table session (
lastAccess text lastAccess text
); );
PRAGMA user_version = 2; pragma user_version = 2;

View file

@ -4,4 +4,4 @@ add column category text;
alter table cake alter table cake
add column availability text; add column availability text;
PRAGMA user_version = 3; pragma user_version = 3;

View file

@ -0,0 +1,9 @@
update cake
set
category = "common";
update cake
set
availability = "avaiable";
pragma user_version = 4;