tags and special cakes: models, migrations, store layer
Add Tag, SpecialCake, Cake.Tags, Order.SpecialCakes types. Add migrations for tag, cake_tag, special_cake, special_cake_tag tables. Add store methods: GetTags, GetCakeTags, SaveCakeTags, CreateTag, GetSpecialCakes, GetSpecialCakeTags, SaveSpecialCakes, saveSpecialCakeTags. Load tags in GetCake/GetCakes, cascade tag saves in SaveCake. Remove debug log calls from ParsePrice.
This commit is contained in:
parent
06231cc490
commit
9aafbb94ec
8 changed files with 297 additions and 23 deletions
|
|
@ -18,7 +18,8 @@ func (s *Store) GetCake(id int) (models.Cake, error) {
|
|||
if err != nil {
|
||||
return models.Cake{}, errors.New("cake not found")
|
||||
}
|
||||
return out, nil
|
||||
out.Tags, err = s.GetCakeTags(id)
|
||||
return out, err
|
||||
}
|
||||
|
||||
func (s *Store) GetCakes() ([]models.Cake, error) {
|
||||
|
|
@ -34,6 +35,11 @@ func (s *Store) GetCakes() ([]models.Cake, error) {
|
|||
if err := rows.Scan(&cake.ID, &cake.Name, &cake.Price); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tags, err := s.GetCakeTags(cake.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cake.Tags = tags
|
||||
out = append(out, cake)
|
||||
}
|
||||
return out, rows.Err()
|
||||
|
|
@ -47,7 +53,7 @@ func (s *Store) SaveCake(newCake models.Cake) (int, error) {
|
|||
return 0, err
|
||||
}
|
||||
log.Printf("UPDATE cake id=%d name=%q price=%d", newCake.ID, newCake.Name, newCake.Price)
|
||||
return newCake.ID, nil
|
||||
return newCake.ID, s.SaveCakeTags(newCake.ID, tagIDs(newCake.Tags))
|
||||
}
|
||||
|
||||
q := "insert into cake(name, price) values (?, ?);"
|
||||
|
|
@ -62,5 +68,13 @@ func (s *Store) SaveCake(newCake models.Cake) (int, error) {
|
|||
}
|
||||
newCake.ID = int(id)
|
||||
log.Printf("INSERT cake id=%d name=%q price=%d", newCake.ID, newCake.Name, newCake.Price)
|
||||
return newCake.ID, nil
|
||||
return newCake.ID, s.SaveCakeTags(newCake.ID, tagIDs(newCake.Tags))
|
||||
}
|
||||
|
||||
func tagIDs(tags []models.Tag) []int {
|
||||
ids := make([]int, len(tags))
|
||||
for i, t := range tags {
|
||||
ids[i] = t.ID
|
||||
}
|
||||
return ids
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue